-
Notifications
You must be signed in to change notification settings - Fork 46
Local Strategy passport #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,4 +5,3 @@ config.js | |
| public/lib | ||
| .c9revisions | ||
| .settings | ||
| public/css/* | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Route middleware to ensure user is authenticated. | ||
| */ | ||
| exports.ensureAuthenticated = function ensureAuthenticated(req, res, next) { | ||
| if (req.isAuthenticated()) { return next(); } | ||
| res.send(401); | ||
| } | ||
|
|
||
| /** | ||
| * Blog authorizations routing middleware | ||
| */ | ||
| exports.blog = { | ||
| hasAuthorization: function(req, res, next) { | ||
| if (req.blog.creator._id.toString() !== req.user._id.toString()) { | ||
| return res.send(403); | ||
| } | ||
| next(); | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| /** | ||
| * Created by sreekanth on 1/3/15. | ||
| */ | ||
| // config/database.js | ||
| module.exports = { | ||
|
|
||
| 'url': 'mongodb://localhost/autherization' // looks like mongodb://<user>:<pass>@mongo.onmodulus.net:27017/Mikha4ot | ||
|
|
||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // config/auth.js | ||
|
|
||
| // expose our config directly to our application using module.exports | ||
| module.exports = { | ||
|
|
||
| 'facebookAuth' : { | ||
| 'clientID' : '1511163462453285', // your App ID | ||
| 'clientSecret' : 'a469ebd9b3ee882cd1578d26ee91b491', // your App Secret | ||
| 'callbackURL' : 'http://localhost:8080/auth/facebook/callback' | ||
| }, | ||
|
|
||
| 'twitterAuth' : { | ||
| 'consumerKey' : 'I9YLv8c0FJIYPACU5eYGRbcGW', | ||
| 'consumerSecret' : 'j9330GuivKIuwC3c8r3RfRNLycrDyZ2OfHFQEGW4h2zrLkdElY', | ||
| 'callbackURL' : 'http://localhost:8080/auth/twitter/callback' | ||
| }, | ||
|
|
||
| 'googleAuth' : { | ||
| 'clientID' : '233449258545-tura73svarjsatjmc13v4q6oojqknhbg.apps.googleusercontent.com', | ||
| 'clientSecret' : 'Gmt7k6MzSWJ3ZSANiqU7OCAG', | ||
| 'callbackURL' : 'http://localhost:8080/auth/google/callback' | ||
| } | ||
|
|
||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /** | ||
| * Created by sreekanth on 1/3/15. | ||
| */ | ||
| // load all the things we need | ||
| var LocalStrategy = require('passport-local').Strategy; | ||
| // load up the user model | ||
| var User = require('../models/user'); | ||
|
|
||
| // load the auth variables | ||
| var configAuth = require('./auth'); // use this one for testing | ||
|
|
||
| module.exports = function(passport) { | ||
|
|
||
| // ========================================================================= | ||
| // LOCAL LOGIN ============================================================= | ||
| // ========================================================================= | ||
| passport.use('local-login', new LocalStrategy({ | ||
| // by default, local strategy uses username and password, we will override with email | ||
| usernameField : 'email', | ||
| passwordField : 'password', | ||
| passReqToCallback : true // allows us to pass in the req from our route (lets us check if a user is logged in or not) | ||
| }, | ||
| function(req, email, password, done) { | ||
| if (email) | ||
| email = email.toLowerCase(); // Use lower-case e-mails to avoid case-sensitive e-mail matching | ||
|
|
||
| // asynchronous | ||
| process.nextTick(function() { | ||
| User.findOne({ 'local.email' : email }, function(err, user) { | ||
| // if there are any errors, return the error | ||
| if (err) | ||
| return done(err); | ||
|
|
||
| // if no user is found, return the message | ||
| if (!user) | ||
| return done(null, false, req.flash('loginMessage', 'No user found.')); | ||
|
|
||
| if (!user.validPassword(password)) | ||
| return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); | ||
|
|
||
| // all is well, return user | ||
| else | ||
| return done(null, user); | ||
| }); | ||
| }); | ||
|
|
||
| })); | ||
|
|
||
| // ========================================================================= | ||
| // LOCAL SIGNUP ============================================================ | ||
| // ========================================================================= | ||
| passport.use('local-signup', new LocalStrategy({ | ||
| // by default, local strategy uses username and password, we will override with email | ||
| usernameField : 'email', | ||
| passwordField : 'password', | ||
| passReqToCallback : true // allows us to pass in the req from our route (lets us check if a user is logged in or not) | ||
| }, | ||
| function(req, email, password, done) { | ||
| if (email) | ||
| email = email.toLowerCase(); // Use lower-case e-mails to avoid case-sensitive e-mail matching | ||
|
|
||
| // asynchronous | ||
| process.nextTick(function() { | ||
| // if the user is not already logged in: | ||
| if (!req.user) { | ||
| User.findOne({ 'local.email' : email }, function(err, user) { | ||
| // if there are any errors, return the error | ||
| if (err) | ||
| return done(err); | ||
|
|
||
| // check to see if theres already a user with that email | ||
| if (user) { | ||
| return done(null, false, req.flash('signupMessage', 'That email is already taken.')); | ||
| } else { | ||
|
|
||
| // create the user | ||
| var newUser = new User(); | ||
|
|
||
| newUser.local.email = email; | ||
| newUser.local.password = newUser.generateHash(password); | ||
|
|
||
| newUser.save(function(err) { | ||
| if (err) | ||
| throw err; | ||
|
|
||
| return done(null, newUser); | ||
| }); | ||
| } | ||
|
|
||
| }); | ||
| // if the user is logged in but has no local account... | ||
| } else if ( !req.user.local.email ) { | ||
| // ...presumably they're trying to connect a local account | ||
| var user = req.user; | ||
| user.local.email = email; | ||
| user.local.password = user.generateHash(password); | ||
| user.save(function(err) { | ||
| if (err) | ||
| throw err; | ||
| return done(null, user); | ||
| }); | ||
| } else { | ||
| // user is logged in and already has a local account. Ignore signup. (You should log out before trying to create a new account, user!) | ||
| return done(null, req.user); | ||
| } | ||
|
|
||
| }); | ||
|
|
||
| })); | ||
| }; | ||
|
|
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.
facbook, twitter, google may not be required.