This repository has been archived by the owner on Mar 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
144 lines (123 loc) · 3.69 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var dotenv = require('dotenv');
var passport = require('passport');
var OAuth2Strategy = require('passport-oauth2');
var flash = require('connect-flash');
var userInViews = require('./lib/middleware/userInViews');
var authRouter = require('./routes/auth');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
dotenv.config();
let strategy = new OAuth2Strategy({
authorizationURL: process.env.AUTHORIZATION_URL,
tokenURL: process.env.TOKEN_URL,
clientID: process.env.OAUTH_CLIENT_ID,
clientSecret: process.env.OAUTH_CLIENT_SECRET,
callbackURL: process.env.CALLBACK_URL,
passReqToCallback: true,
},
function ( params,accessToken, refreshToken,userProfile, cb) {
console.log(accessToken, refreshToken);
return cb(null, userProfile);
}
);
strategy.userProfile = function (accesstoken, done) {
// choose your own adventure, or use the Strategy's oauth client
const headers = {
'User-Agent': 'request',
'Authorization': 'Bearer ' + accesstoken,
};
this._oauth2._request("GET", process.env.USER_INFO_URL, headers, null, null, (err, data) => {
if (err) { return done(err); }
try {
data = JSON.parse(data);
}
catch (e) {
return done(e);
}
done(null, data);
});
};
passport.use(strategy);
// You can use this section to keep a smaller payload
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (user, done) {
done(null, user);
});
const app = express();
// View engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(cookieParser());
// config express-session
var sess = {
secret: 'CHANGE THIS SECRET',
cookie: {},
resave: false,
saveUninitialized: true
};
if (app.get('env') === 'production') {
// If you are using a hosting provider which uses a proxy (eg. Heroku),
// comment in the following app.set configuration command
//
// Trust first proxy, to prevent "Unable to verify authorization request state."
// errors with passport-auth0.
// Ref: https://github.com/auth0/passport-auth0/issues/70#issuecomment-480771614
// Ref: https://www.npmjs.com/package/express-session#cookiesecure
// app.set('trust proxy', 1);
sess.cookie.secure = true; // serve secure cookies, requires https
}
app.use(session(sess));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));
app.use(flash());
// Handle auth failure error messages
app.use(function (req, res, next) {
if (req && req.query && req.query.error) {
req.flash('error', req.query.error);
}
if (req && req.query && req.query.error_description) {
req.flash('error_description', req.query.error_description);
}
next();
});
app.use(userInViews());
app.use('/', authRouter);
app.use('/', indexRouter);
app.use('/', usersRouter);
// Catch 404 and forward to error handler
app.use(function (req, res, next) {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
// Error handlers
// Development error handler
// Will print stacktrace
if (app.get('env') === 'development') {
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// Production error handler
// No stacktraces leaked to user
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;