-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
82 lines (68 loc) · 1.94 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
var express = require('express');
var passport = require('passport');
var Strategy = require('passport-facebook').Strategy;
var config = require('./config/OAuth').facebook;
var PORT = require('./config/env').PORT;
passport.use(
new Strategy(
{
clientID: config.clientID,
clientSecret: config.clientSecret,
callbackURL: config.callbackURL
},
function(accessToken, refreshToken, profile, cb) {
return cb(null, profile);
}
)
);
passport.serializeUser(function(user, cb) {
cb(null, user);
});
passport.deserializeUser(function(obj, cb) {
cb(null, obj);
});
// Create a new Express application.
var app = express();
// Configure view engine to render EJS templates.
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
// Use application-level middleware for common functionality, including
// logging, parsing, and session handling.
app.use(require('morgan')('combined'));
app.use(require('cookie-parser')());
app.use(require('body-parser').urlencoded({ extended: true }));
app.use(
require('express-session')({
secret: 'keyboard cat',
resave: true,
saveUninitialized: true
})
);
// Initialize Passport and restore authentication state, if any, from the
// session.
app.use(passport.initialize());
app.use(passport.session());
// Define routes.
app.get('/', function(req, res) {
res.render('home', { user: req.user });
});
app.get('/login', function(req, res) {
res.render('login');
});
app.get('/login/facebook', passport.authenticate('facebook'));
app.get(
'/login/facebook/return',
passport.authenticate('facebook', {
failureRedirect: '/login',
scope: ['user_likes']
}),
function(req, res) {
res.redirect('/');
}
);
app.get('/profile', require('connect-ensure-login').ensureLoggedIn(), function(req,res) {
res.render('profile', { user: req.user, app_id: config.clientID, PORT: PORT });
});
app.listen(PORT, () =>{
console.log(`Listening on PORT ${PORT}`)
});