-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblogger_example.js
119 lines (101 loc) · 3.36 KB
/
blogger_example.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
var express = require('express'),
app = express(),
passport = require('passport'),
FacebookStrategy = require('passport-facebook').Strategy,
session = require('express-session');
var facebookAuth = {
'clientID' : 'some client id', // facebook App ID
'clientSecret' : 'some secret', // facebook App Secret
'callbackURL' : 'http://localhost:8080/auth/facebook/callback'
};
// hardcoded users, ideally the users should be stored in a database
var users = [
{"id":111, "username":"amy", "password":"amyspassword"},
{
"id" : "222",
"email" : "[email protected]",
"name" : "Ben",
"token" : "adsfsdfsdfdsfd"}
];
function findUser(id) {
for(var i=0; i<users.length; i++) {
if(id === users[i].id) {
return users[i]
}
}
return null;
}
// passport needs ability to serialize and unserialize users out of session
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (id, done) {
done(null, id);
});
// passport facebook strategy
passport.use(new FacebookStrategy({
"clientID" : facebookAuth.clientID,
"clientSecret" : facebookAuth.clientSecret,
"callbackURL" : facebookAuth.callbackURL
},
function (token, refreshToken, profile, done) {
var user = findUser(profile.id);
if (user) {
console.log(users);
return done(null, user);
} else {
var newUser = {
"id": profile.id,
"name": profile.displayName,
"token": token
};
users.push(newUser);
console.log(users);
return done(null, newUser);
}
}));
// initialize passposrt and and session for persistent login sessions
app.use(session({
secret: "tHiSiSasEcRetStr",
resave: true,
saveUninitialized: true }));
app.use(passport.initialize());
app.use(passport.session());
// route middleware to ensure user is logged in, if it's not send 401 status
function isLoggedIn(req, res, next) {
if (req.isAuthenticated())
return next();
res.sendStatus(401);
}
// home page
app.get("/", function (req, res) {
res.send("Hello!");
});
// login page
app.get("/login", function (req, res) {
res.send("<a href='/auth/facebook'>login through facebook</a>");
});
// send to facebook to do the authentication
app.get("/auth/facebook", passport.authenticate("facebook", { scope : "email" }));
// handle the callback after facebook has authenticated the user
app.get("/auth/facebook/callback",
passport.authenticate("facebook", {
successRedirect : "/content",
failureRedirect : "/"
}));
// content page, it calls the isLoggedIn function defined above first
// if the user is logged in, then proceed to the request handler function,
// else the isLoggedIn will send 401 status instead
app.get("/content", isLoggedIn, function (req, res) {
res.send("Congratulations! you've successfully logged in.");
});
// logout request handler, passport attaches a logout() function to the req object,
// and we call this to logout the user, same as destroying the data in the session.
app.get("/logout", function(req, res) {
req.logout();
res.send("logout success!");
});
// launch the app
app.listen(8080);
console.log("App running at localhost:8080");
console.log(users);