Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d4d1239
commit ce702e2
Showing
8 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,122 @@ | |||
var express = require('express') | |||
, passport = require('passport') | |||
, util = require('util') | |||
, FacebookStrategy = require('passport-facebook').Strategy; | |||
|
|||
var FACEBOOK_APP_ID = "--insert-facebook-app-id-here--" | |||
var FACEBOOK_APP_SECRET = "--insert-facebook-app-secret-here--"; | |||
|
|||
|
|||
// Passport session setup. | |||
// To support persistent login sessions, Passport needs to be able to | |||
// serialize users into and deserialize users out of the session. Typically, | |||
// this will be as simple as storing the user ID when serializing, and finding | |||
// the user by ID when deserializing. However, since this example does not | |||
// have a database of user records, the complete Facebook profile is serialized | |||
// and deserialized. | |||
passport.serializeUser(function(user, done) { | |||
done(null, user); | |||
}); | |||
|
|||
passport.deserializeUser(function(obj, done) { | |||
done(null, obj); | |||
}); | |||
|
|||
|
|||
// Use the FacebookStrategy within Passport. | |||
// Strategies in Passport require a `verify` function, which accept | |||
// credentials (in this case, an accessToken, refreshToken, and Facebook | |||
// profile), and invoke a callback with a user object. | |||
passport.use(new FacebookStrategy({ | |||
clientID: FACEBOOK_APP_ID, | |||
clientSecret: FACEBOOK_APP_SECRET, | |||
callbackURL: "http://localhost:3000/auth/facebook/callback" | |||
}, | |||
function(accessToken, refreshToken, profile, done) { | |||
// asynchronous verification, for effect... | |||
process.nextTick(function () { | |||
|
|||
// To keep the example simple, the user's Facebook profile is returned to | |||
// represent the logged-in user. In a typical application, you would want | |||
// to associate the Facebook account with a user record in your database, | |||
// and return that user instead. | |||
return done(null, profile); | |||
}); | |||
} | |||
)); | |||
|
|||
|
|||
|
|||
|
|||
var app = express.createServer(); | |||
|
|||
// configure Express | |||
app.configure(function() { | |||
app.set('views', __dirname + '/views'); | |||
app.set('view engine', 'ejs'); | |||
app.use(express.logger()); | |||
app.use(express.cookieParser()); | |||
app.use(express.bodyParser()); | |||
app.use(express.methodOverride()); | |||
app.use(express.session({ secret: 'keyboard cat' })); | |||
// Initialize Passport! Also use passport.session() middleware, to support | |||
// persistent login sessions (recommended). | |||
app.use(passport.initialize()); | |||
app.use(passport.session()); | |||
app.use(app.router); | |||
app.use(express.static(__dirname + '/public')); | |||
}); | |||
|
|||
|
|||
app.get('/', function(req, res){ | |||
res.render('index', { user: req.user }); | |||
}); | |||
|
|||
app.get('/account', ensureAuthenticated, function(req, res){ | |||
res.render('account', { user: req.user }); | |||
}); | |||
|
|||
app.get('/login', function(req, res){ | |||
res.render('login', { user: req.user }); | |||
}); | |||
|
|||
// GET /auth/facebook | |||
// Use passport.authenticate() as route middleware to authenticate the | |||
// request. The first step in Facebook authentication will involve | |||
// redirecting the user to facebook.com. After authorization, Facebook will | |||
// redirect the user back to this application at /auth/facebook/callback | |||
app.get('/auth/facebook', | |||
passport.authenticate('facebook'), | |||
function(req, res){ | |||
// The request will be redirected to Facebook for authentication, so this | |||
// function will not be called. | |||
}); | |||
|
|||
// GET /auth/facebook/callback | |||
// Use passport.authenticate() as route middleware to authenticate the | |||
// request. If authentication fails, the user will be redirected back to the | |||
// login page. Otherwise, the primary route function function will be called, | |||
// which, in this example, will redirect the user to the home page. | |||
app.get('/auth/facebook/callback', | |||
passport.authenticate('facebook', { failureRedirect: '/login' }), | |||
function(req, res) { | |||
res.redirect('/'); | |||
}); | |||
|
|||
app.get('/logout', function(req, res){ | |||
req.logout(); | |||
res.redirect('/'); | |||
}); | |||
|
|||
app.listen(3000); | |||
|
|||
|
|||
// 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 ensureAuthenticated(req, res, next) { | |||
if (req.isAuthenticated()) { return next(); } | |||
res.redirect('/login') | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,10 @@ | |||
{ | |||
"name": "passport-facebook-examples-login", | |||
"version": "0.0.0", | |||
"dependencies": { | |||
"express": ">= 0.0.0", | |||
"ejs": ">= 0.0.0", | |||
"passport": ">= 0.0.0", | |||
"passport-facebook": ">= 0.0.0" | |||
} | |||
} |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,3 @@ | |||
<p>ID: <%= user.id %></p> | |||
<p>Username: <%= user.username %></p> | |||
<p>Name: <%= user.displayName %></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,5 @@ | |||
<% if (!user) { %> | |||
<h2>Welcome! Please log in.</h2> | |||
<% } else { %> | |||
<h2>Hello, <%= user.username %>.</h2> | |||
<% } %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,21 @@ | |||
<!DOCTYPE html> | |||
<html> | |||
<head> | |||
<title>Passport-Facebook Example</title> | |||
</head> | |||
<body> | |||
<% if (!user) { %> | |||
<p> | |||
<a href="/">Home</a> | | |||
<a href="/login">Log In</a> | |||
</p> | |||
<% } else { %> | |||
<p> | |||
<a href="/">Home</a> | | |||
<a href="/account">Account</a> | | |||
<a href="/logout">Log Out</a> | |||
</p> | |||
<% } %> | |||
<%- body %> | |||
</body> | |||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
<a href="/auth/facebook"><img src="login-with-facebook.png" width="154" height="22"></a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ce702e2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's not a vk example