-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
120 lines (96 loc) · 3.14 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
/**
* Created by maghidini on 4/12/15.
*/
var https = require('https');
var fs = require('fs');
var express = require('express');
var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy;
var passportLocal = require('passport-local');
var passportHttp = require('passport-http');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var expressSession = require('express-session');
var app = express();
var server = https.createServer({
cert: fs.readFileSync(__dirname + '/my.crt'),
key: fs.readFileSync(__dirname + '/my.key')
}, app);
app.set('view engine','ejs');
app.use(bodyParser.urlencoded({extended:false}));
app.use(cookieParser());
app.use(expressSession(
{
secret: process.env.SESSION_SECRET || 'secret',
saveUninitialized:false,
resave:false
}));
app.use(passport.initialize());
app.use(passport.session());
function verifyCredentials(username, password, done) {
if(username === password ){
done(null, {id: username, name: username});
}else{
done(null, null);
}
}
passport.use(new passportLocal.Strategy(verifyCredentials));
passport.use(new passportHttp.BasicStrategy(verifyCredentials));
passport.use(new FacebookStrategy({
clientID: 'FacebookAppIdHere', //Change this
clientSecret: 'FacebookAppSecretHere', //Change this
callbackURL: "https://127.0.0.1:1337/auth/facebook/callback" //Should be registered as Valid OAuth redirect URIs
//on yours Facebook App Advanced Setting
},
function(accessToken, refreshToken, profile, done) {
done(null, profile)
}
));
passport.serializeUser(function (user, done) {
done(null,user.id);
});
passport.deserializeUser(function (id, done) {
//Query to database or cache here!
done(null, {id: id, name: id});
});
app.get('/auth/facebook', passport.authenticate('facebook'));
app.get('/auth/facebook/callback',
passport.authenticate('facebook', { successRedirect: '/',
failureRedirect: '/login' }));
app.get('/', function (req, res) {
res.render('index',{
isAuthenticated: req.isAuthenticated(),
user: req.user
})
});
app.get('/login', function (req, res) {
res.render('login')
});
//passport.authenticate('local') returns a function that will be called by Express as a middleware
app.post('/login', passport.authenticate('local'), function (req, res) {
res.redirect('/');
});
app.get('/logout', function (req, res) {
req.logout();
res.redirect('/');
});
function ensureAuthenticated(req,res,next){
if (req.isAuthenticated()){
next();
}else{
res.sendStatus(403);
}
}
app.use('/api', passport.authenticate('basic',{session:false}));
app.get('/api/data',ensureAuthenticated, function (req, res) {
res.json([
{value: 'foo'},
{value: 'bar'},
{value: 'baz'}
])
});
var port = process.env.PORT || 1337;
server.listen(port, function () {
console.log('https://127.0.0.1:' + port + '/');
});
// openssl req -x509 -nodes -days 365 -newkey rsa:1024 -out my.crt -keyout my.key