-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
80 lines (66 loc) · 1.79 KB
/
index.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
// List module dependancies
var express = require('express');
var session = require('express-session');
var bodyParser = require('body-parser');
var http = require('http');
var passport = require('passport');
var db = require('./models');
// Init express app
var app = express();
// Config on express-session
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
cookie: {
secure: false
}
}))
// Init all dependencies used by the app
app.use(passport.initialize());
app.use(passport.session());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
// Init Databases
// test authentification
db.sequelize
.authenticate()
.then(function(success) {
console.log('Connection has been established successfully.');
})
.catch(function(err) {
console.log('Unable to connect to the database:', err);
});
// sync DB
db.sequelize.sync();
// Init oauth middleware
require('./oauth')(passport);
// Basic oauth routes
app.get('/auth', passport.authenticate('oauth2'));
app.get('/auth/callback', passport.authenticate('oauth2', {
successRedirect: '/auth/profile',
failureRedirect: '/auth/failed'
}));
app.get('/auth/profile', function(req,res){
console.log(req.user);
if(typeof req.user === 'undefined'){
res.status(204).send('No user profile');
} else {
res.status(200).json(req.user);
};
});
app.get('/auth/failed', function(req,res){
res.status(403).send('Authorization denied by user');
});
app.get('/auth/logout', function(req,res){
req.logout();
res.redirect('/auth');
// NOTE: Clear the app session, but not the ORCID session.
});
// start server
var port = process.env.PORT_MANGAL_OAUTH || 3000;
server = http.createServer(app);
server.listen(port);
module.exports = server;