-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
127 lines (104 loc) · 3.36 KB
/
server.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
var express = require('express')
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
methodOverride = require('method-override'),
session = require('express-session'),
swig = require('swig'),
morgan = require('morgan'),
util = require('util')
passport = require('passport'),
PixelPinStrategy = require('Passport-PixelPin-OpenIDConnect').Strategy;
passport.use(new PixelPinStrategy({
clientID: "16ZLQYMHQ5MLLP0I06KUZ8R52QELB5",
clientSecret: "HX5BRN@B7p9n2R~Mx7KjRw5@RAVw+9",
responseType: "code",
callbackURL: "http://node.local.com:8888/auth/pixelpin/callback"
},
function(accessToken, refreshToken, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
return done(null,profile);
});
passport.serializeUser(function(user, done) {
//Use for storing data in your database.
var userData = {
id: user.id,
email: user.email,
firstName: user.firstName,
lastName: user.lastName,
street_address: user.street_address,
locality: user.locality,
region: user.region,
postal_code: user.postal_code,
country: user.country,
dob: user.dob,
gender: user.gender,
phone: user.phone
};
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
}));
var app = express();
// configure Express
app.configure(function() {
app.set('views', __dirname + '/view');
app.set('view engine', 'ejs');
app.use(require('morgan')('combined'));
app.use(express.logger());
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.session({ secret: 'keyboard cat', resave: true, saveUninitialized: true }));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
var isAuthenticated = function (req, res, next) {
if (req.isAuthenticated())
return next();
res.redirect('/account');
}
app.get('/', function(req,res,profile){
res.render('index', { user: req.user});
});
app.get('/account', isAuthenticated, function(req, res, next){
res.render('account', {
user: req.user //gets the user data out of session and pass to template
});
});
app.get('/login', function(req,res){
res.render('login', { user: req.user});
});
app.get('/register', function(req,res){
res.render('register', { user: req.user});
});
app.get('/layout', function(req,res){
res.render('layout', { user: req.user});
});
app.get('/logout', function(req,res){
req.logout();
res.redirect('/');
});
app.get('/auth/pixelpin',
passport.authenticate('pixelpin', {scope: ['profile','email','phone','address']}),
function(req,res){
});
app.get('/auth/pixelpin/callback',
passport.authenticate('pixelpin', { failureRedirect: '/login'}),
function(req,res) {
res.redirect('/layout');
});
app.listen(8888);
// Simple route middleware to ensure user is authenticated.
// Use this route middleware on any resource that needs to be protected. If
// the request is authenticated (typically via a persistent login session),
// the request will proceed. Otherwise, the user will be redirected to the
// login page.
function ensureAuthenticate(req, res, next) {
if (req.isAuthenticated()) { return next();}
res.redirect('/login')
}