-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auth with Passport. Username attr added.
- Loading branch information
1 parent
b45f9e7
commit 0c201f9
Showing
11 changed files
with
121 additions
and
56 deletions.
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
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
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
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
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
var passport = require('passport'), | ||
LocalStrategy = require('passport-local').Strategy, | ||
bcrypt = require('bcrypt'); | ||
|
||
// 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. | ||
passport.serializeUser(function(user, done) { | ||
done(null, user.id); | ||
}); | ||
|
||
passport.deserializeUser(function(id, done) { | ||
User.findOne(id).done(function (err, user) { | ||
done(err, user); | ||
}); | ||
}); | ||
|
||
passport.use(new LocalStrategy( | ||
function(username, password, done) { | ||
// asynchronous verification, for effect... | ||
process.nextTick(function () { | ||
// Find the user by username or email. | ||
// If there is no user with the given username, | ||
// or the password is not correct, | ||
// set the user to `false` to indicate failure | ||
// and set a flash message. | ||
// Otherwise, return the authenticated `user`. | ||
User.findOne().where({ | ||
or: [ | ||
{ username: username }, | ||
{ email: username } | ||
] | ||
}).done(function(err, user) { | ||
if (err) { return done(null, err); } | ||
if (!user) { return done(null, false, { message: 'Unknown user ' + username }); } | ||
bcrypt.compare(password, user.password, function(err, res) { | ||
if (!res) return done(null, false, { message: 'Invalid Password'}); | ||
return done(null, user, { message: 'Logged In Successfully'} ); | ||
}); | ||
}) | ||
}); | ||
} | ||
)); | ||
|
||
module.exports = { | ||
express: { | ||
customMiddleware: function(app){ | ||
console.log('Express midleware for passport'); | ||
app.use(passport.initialize()); | ||
app.use(passport.session()); | ||
app.use(function(req,res,next){ | ||
// Set the loggedUser in locals | ||
// to get it from the view | ||
res.locals.loggedUser = req.user; | ||
next(); | ||
}); | ||
} | ||
} | ||
}; |
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 | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,24 @@ | ||
{ | ||
"name": "building-realtime-webapp", | ||
"private": true, | ||
"version": "0.0.0", | ||
"description": "a Sails application", | ||
"dependencies": { | ||
"sails": "0.9.8", | ||
"grunt": "0.4.1", | ||
"ejs": "0.8.4", | ||
"optimist": "0.3.4", | ||
"sails-mongo": "~0.9.6", | ||
"bcrypt": "~0.7.7" | ||
}, | ||
"scripts": { | ||
"start": "node app.js", | ||
"debug": "node debug app.js" | ||
}, | ||
"main": "app.js", | ||
"repository": "", | ||
"author": "", | ||
"license": "" | ||
"name": "building-realtime-webapp", | ||
"private": true, | ||
"version": "0.0.0", | ||
"description": "a Sails application", | ||
"dependencies": { | ||
"sails": "0.9.8", | ||
"grunt": "0.4.1", | ||
"ejs": "0.8.4", | ||
"optimist": "0.3.4", | ||
"sails-mongo": "~0.9.6", | ||
"bcrypt": "~0.7.7", | ||
"passport": "~0.1.18", | ||
"passport-local": "~0.1.6" | ||
}, | ||
"scripts": { | ||
"start": "node app.js", | ||
"debug": "node debug app.js" | ||
}, | ||
"main": "app.js", | ||
"repository": "", | ||
"author": "", | ||
"license": "" | ||
} |
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
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
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 | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,30 @@ | ||
<ol class="breadcrumb"> | ||
<li><a href="/"><%= __('Home') %></a></li> | ||
<li><a href="/user/"><%= __('Users') %></a></li> | ||
<li><a href="/user/<%= user.id %>"><%= user.email %></a></li> | ||
<li><a href="/user/<%= user.id %>"><%= user.username %></a></li> | ||
<li class="active"><%= __('Edit') %></li> | ||
</ol> | ||
<form action="/user/update/<%= user.id %>" method="POST" class="form-center" id="sign-up-form"> | ||
<h2 class="form-signin-heading"><%= __('Edit account') %></h2> | ||
<div class="form-group"> | ||
<label for="username"><%= __('Username') %></label> | ||
<input type="text" class="form-control" placeholder="<%= __('Username') %>" value="<%= user.username || user.id %>" name="username" id="username" required /> | ||
</div> | ||
<div class="form-group"> | ||
<label for="email"><%= __('Email') %></label> | ||
<input type="email" class="form-control" placeholder="<%= __('someone@somewhere.com') %>" value="<%= user.email %>" name="email" id="email" required /> | ||
</div> | ||
<div class="form-group"> | ||
<label for="password"><%= __('Old password') %></label> | ||
<input type="password" class="form-control" placeholder="<%= __('Old password') %>" name="password" id="password" required minlength="6" /> | ||
<input type="password" class="form-control" placeholder="<%= __('Old password') %>" name="password" id="password" minlength="6" /> | ||
</div> | ||
<div class="form-group"> | ||
<label for="new_password"><%= __('New password') %></label> | ||
<input type="password" class="form-control" placeholder="<%= __('New password') %>" name="new_password" id="new_password" required minlength="6" /> | ||
<input type="password" class="form-control" placeholder="<%= __('New password') %>" name="new_password" id="new_password" minlength="6" /> | ||
</div> | ||
<div class="form-group"> | ||
<label for="confirm_password"><%= __('Confirm password') %></label> | ||
<input type="password" class="form-control" placeholder="<%= __('Confirm password') %>" name="confirm_password" id="confirm_password" required minlength="6" /> | ||
<input type="password" class="form-control" placeholder="<%= __('Confirm password') %>" name="confirm_password" id="confirm_password" minlength="6" /> | ||
</div> | ||
<input type="submit" class="btn btn-primary btn-block" value="<%= __('Update account') %>" /> | ||
</form> |
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
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