Created
September 22, 2021 03:45
-
-
Save S-codes14/dc11cae74f52c22b564453bf67266c16 to your computer and use it in GitHub Desktop.
facebook passport auth
This file contains hidden or 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
<!-- views/index.ejs --> | |
<!doctype html> | |
<html> | |
<head> | |
<title>Node Authentication</title> | |
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css"> <!-- load bootstrap css --> | |
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css"> <!-- load fontawesome --> | |
<style> | |
body { padding-top:80px; } | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="jumbotron text-center"> | |
<h1><span class="fa fa-lock"></span> Node Authentication</h1> | |
<p>Login or Register with:</p> | |
<a href="/auth/facebook" class="btn btn-primary"><span class="fa fa-facebook"></span> Facebook</a> | |
</div> | |
</div> | |
</body> | |
</html> |
This file contains hidden or 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
const express = require('express') | |
const app = express() | |
const passport = require('passport') | |
const session = require('express-session') | |
const User = require('./User') | |
const facebookStrategy = require('passport-facebook').Strategy | |
app.set("view engine","ejs") | |
app.use(session({ secret: 'ilovescotchscotchyscotchscotch' })); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
passport.use(new facebookStrategy({ | |
// pull in our app id and secret from our auth.js file | |
clientID : "########yourclientid############", | |
clientSecret : "#############yourclientsecret##########", | |
callbackURL : "http://localhost:5000/facebook/callback", | |
profileFields: ['id', 'displayName', 'name', 'gender', 'picture.type(large)','email'] | |
},// facebook will send back the token and profile | |
function(token, refreshToken, profile, done) { | |
// asynchronous | |
process.nextTick(function() { | |
// find the user in the database based on their facebook id | |
User.findOne({ 'uid' : profile.id }, function(err, user) { | |
// if there is an error, stop everything and return that | |
// ie an error connecting to the database | |
if (err) | |
return done(err); | |
// if the user is found, then log them in | |
if (user) { | |
console.log("user found") | |
console.log(user) | |
return done(null, user); // user found, return that user | |
} else { | |
// if there is no user found with that facebook id, create them | |
var newUser = new User(); | |
// set all of the facebook information in our user model | |
newUser.uid = profile.id; // set the users facebook id | |
newUser.token = token; // we will save the token that facebook provides to the user | |
newUser.name = profile.name.givenName + ' ' + profile.name.familyName; // look at the passport user profile to see how names are returned | |
newUser.email = profile.emails[0].value; // facebook can return multiple emails so we'll take the first | |
newUser.gender = profile.gender | |
newUser.pic = profile.photos[0].value | |
// save our user to the database | |
newUser.save(function(err) { | |
if (err) | |
throw err; | |
// if successful, return the new user | |
return done(null, newUser); | |
}); | |
} | |
}); | |
}) | |
})); | |
passport.serializeUser(function(user, done) { | |
done(null, user.id); | |
}); | |
// used to deserialize the user | |
passport.deserializeUser(function(id, done) { | |
User.findById(id, function(err, user) { | |
done(err, user); | |
}); | |
}); | |
app.get('/profile', isLoggedIn, function(req, res) { | |
console.log(req.user) | |
res.render('profile', { | |
user : req.user // get the user out of session and pass to template | |
}); | |
}); | |
app.get('/logout', function(req, res) { | |
req.logout(); | |
res.redirect('/'); | |
}); | |
// route middleware to make sure | |
function isLoggedIn(req, res, next) { | |
// if user is authenticated in the session, carry on | |
if (req.isAuthenticated()) | |
return next(); | |
// if they aren't redirect them to the home page | |
res.redirect('/'); | |
} | |
app.get('/auth/facebook', passport.authenticate('facebook', { scope : 'email' })); | |
app.get('/facebook/callback', | |
passport.authenticate('facebook', { | |
successRedirect : '/profile', | |
failureRedirect : '/' | |
})); | |
app.get('/',(req,res) => { | |
res.render("index") | |
}) | |
app.listen(5000,() => { | |
console.log("App is listening on Port 5000") | |
}) |
This file contains hidden or 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
{ | |
"name": "passportfacebook", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"start": "nodemon index.js" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"cookie-parser": "^1.4.5", | |
"ejs": "^3.1.3", | |
"express": "^4.17.1", | |
"express-session": "^1.17.1", | |
"mongoose": "^5.9.22", | |
"nodemon": "^2.0.4", | |
"passport": "^0.4.1", | |
"passport-facebook": "^3.0.0" | |
} | |
} |
This file contains hidden or 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
<!-- views/profile.ejs --> | |
<!doctype html> | |
<html> | |
<head> | |
<title>Node Authentication</title> | |
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css"> | |
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css"> | |
<style> | |
body { padding-top:80px; word-wrap:break-word; } | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="page-header text-center"> | |
<h1><span class="fa fa-anchor"></span> Profile Page</h1> | |
<a href="/logout" class="btn btn-default btn-sm">Logout</a> | |
</div> | |
<div class="row"> | |
<!-- FACEBOOK INFORMATION --> | |
<div class="col-sm-6"> | |
<div class="well"> | |
<h3 class="text-primary"><span class="fa fa-facebook"></span> Facebook</h3> | |
<p> | |
<strong>id</strong>: <%= user.uid %><br> | |
<strong>token</strong>: <%= user.token %><br> | |
<strong>email</strong>: <%= user.email %><br> | |
<strong>name</strong>: <%= user.name %><br> | |
<strong>gender</strong>: <%= user.gender %><br> | |
<img src="<%=user.pic%>" width="200" height="200" alt=""> | |
</p> | |
</div> | |
</div> | |
</div> | |
</div> | |
</body> | |
</html> |
This file contains hidden or 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
const mongoose = require("mongoose"); | |
mongoose.connect("mongodb://localhost:27017/facebookauth", { | |
useNewUrlParser: true, | |
useUnifiedTopology: true, | |
}); | |
var userSchema = mongoose.Schema({ | |
uid: String, | |
token: String, | |
email: String, | |
name: String, | |
gender: String, | |
pic: String | |
}); | |
module.exports = mongoose.model('User', userSchema); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment