-
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
Open
MezzLabs
wants to merge
4
commits into
AVGP:master
Choose a base branch
from
MezzLabs:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| 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(); | ||
| } | ||
| }; |
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
| 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 | ||
|
|
||
| }; |
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
| 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' | ||
| } | ||
|
|
||
| }; | ||
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
| 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); | ||
| } | ||
|
|
||
| }); | ||
|
|
||
| })); | ||
| }; | ||
|
|
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // load the things we need | ||
| var mongoose = require('mongoose'); | ||
| var bcrypt = require('bcrypt-nodejs'); | ||
|
|
||
| // define the schema for our user model | ||
| var userSchema = mongoose.Schema({ | ||
|
|
||
| local : { | ||
| email : String, | ||
| password : String, | ||
| }, | ||
| facebook : { | ||
| id : String, | ||
| token : String, | ||
| email : String, | ||
| name : String | ||
| }, | ||
| twitter : { | ||
| id : String, | ||
| token : String, | ||
| displayName : String, | ||
| username : String | ||
| }, | ||
| google : { | ||
| id : String, | ||
| token : String, | ||
| email : String, | ||
| name : String | ||
| } | ||
|
|
||
| }); | ||
|
|
||
| // generating a hash | ||
| userSchema.methods.generateHash = function(password) { | ||
| return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null); | ||
| }; | ||
|
|
||
| // checking if password is valid | ||
| userSchema.methods.validPassword = function(password) { | ||
| return bcrypt.compareSync(password, this.local.password); | ||
| }; | ||
|
|
||
| // create the model for users and expose it to our app | ||
| module.exports = mongoose.model('User', userSchema); |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| angular.module('c9hub', ['workspace', 'ngRoute']).config(function($routeProvider) { | ||
| $routeProvider.when('/', {templateUrl: "/partials/login.html"}); | ||
| $routeProvider.when('/', {templateUrl: "/partials/login.ejs"}); | ||
| $routeProvider.when('/dashboard', {controller: WorkspaceCtrl, templateUrl: "/partials/workspace.html"}); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.