Skip to content

Commit

Permalink
Allow Google Single Sign On using Passport
Browse files Browse the repository at this point in the history
package.json: Add [passport-google-oauth](https://www.npmjs.com/package/passport-google-oauth)
api/models/User.js:  Allow create user via a google (with GoogleID) or local (with Password)
config/passport.js: Configure GoogleStrategy
config/route.js: Shortcut to access googleLogin via '/loginGoogle'
api/controllers/AuthController.js: Create 'loginGoogle' to handle passport google authentication
views/static/login.ejs: Add button
  • Loading branch information
makah committed Apr 15, 2016
1 parent 6adbdf7 commit 49545f8
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 8 deletions.
29 changes: 25 additions & 4 deletions api/controllers/AuthController.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@ module.exports = {

})(req, res);
},

loginGoogle: function(req, res) {
sails.log.info('AuthController.loginGoogle: ', 'Params', req.params.all());
var permissions = ['profile','email'];

passport.authenticate('google', {failureRedirect: '/loginGoogle', scope: permissions}, function(err, user, info) {
if ((err) || (!user)) {
return res.send({
err: err,
info: info,
user: user
});
}
req.logIn(user, function(err) {
if (err)
return res.send(err);

return res.redirect('/private');
});
})(req, res);
},

logout: function(req, res) {
req.logout();
Expand Down Expand Up @@ -79,7 +100,7 @@ module.exports = {
recipientName: user.name,
senderMail: user.email,
siteName: sails.config.siteName,
siteAddrs: sails.config.appUrl,
siteAddrs: process.env.APP_URL || 'https://www.myFirst-rateSite.com',
token: token
};

Expand Down Expand Up @@ -155,7 +176,7 @@ module.exports = {
resetPasswordExpires: {'>': Date.now()}
};

sails.log.silly(findParam);
sails.log.silly('AuthController.updatePassword',findParam);

User.findOne(findParam, function(err, user) {
if (!user) {
Expand All @@ -166,8 +187,8 @@ module.exports = {
}

user.password = params.password;
user.resetPasswordToken = undefined;
user.resetPasswordExpires = undefined;
user.resetPasswordToken = null;
user.resetPasswordExpires = null;

user.save(function(err) {
req.logIn(user, function(err) {
Expand Down
6 changes: 6 additions & 0 deletions api/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ module.exports = {
minLength: 6,
},

//Google Signin ID
googleId: 'string',

//Access token from the Google Authorization Server
googleAccessToken: 'string',

resetPasswordToken: String,
resetPasswordExpires: Date,

Expand Down
54 changes: 54 additions & 0 deletions config/passport.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var passport = require('passport'),
LocalStrategy = require('passport-local').Strategy,
GoogleStrategy = require('passport-google-oauth').OAuth2Strategy,
bcrypt = require('bcrypt');

/////////////////////////
Expand All @@ -13,6 +14,11 @@ if(typeof locals === 'undefined'){
return;
}

if(!locals.googleApiConfig){
console.log('[ERROR]', "MISSING: 'sails.config.googleApiConfig'");
return;
}

/////////////////////////
/// CORE
/////////////////////////
Expand Down Expand Up @@ -60,3 +66,51 @@ passport.use(new LocalStrategy(localOptions, function(email, password, done) {
});
}
));

/////////////////////////
/// Google
/////////////////////////
var googleOptions = {
clientID: locals.googleApiConfig.clientID,
clientSecret: locals.googleApiConfig.clientSecret,
callbackURL: locals.googleApiConfig.callbackURL.replace('{URL}', appUrl),
};

passport.use(new GoogleStrategy(googleOptions, function(accessToken, refreshToken, profile, done) {
sails.log.silly("Passport.Google: Found profile", {profile: profile});

if(!profile.emails || profile.length == 0){
var err = 'Passport.Google: Email Missing';
sails.log.error(err);
return done(err);
}

var findParam = {
googleId: profile.id
};

var createParam = {
name: profile.displayName,
email: profile.emails[0].value,
googleId: profile.id,
};

User.findOrCreate(findParam, createParam, function(err, user) {
if (err) {
sails.log.error('Passport.Google', err);
return done(err);
}

user.googleAccessToken = refreshToken ? refreshToken : accessToken;
user.save(function(err, usr) {
if(err){
sails.log.error('Passport.Google: Saving User Model', err);
return done(err);
}

sails.log.verbose('Passport.Google: Updating UserID', usr);
return done(null, user, { message: '(Google) Logged In Successfully' });
});
});
}
));
3 changes: 2 additions & 1 deletion config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ module.exports.routes = {

'get /login': { view: 'static/login' },
'post /login': 'AuthController.login',

'/loginGoogle': 'AuthController.loginGoogle',

'/logout': 'AuthController.logout',

'/forgotPassword': { view: 'static/forgotPassword' },
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"bcrypt": "~0.8.0",
"passport": "~0.2.1",
"passport-local": "~1.0.0",
"passport-google-oauth": "^0.1.5",
"sails-hook-email": "^0.12.1"
},
"scripts": {
Expand All @@ -39,4 +40,4 @@
},
"author": "ubuntu",
"license": ""
}
}
2 changes: 1 addition & 1 deletion views/emailTemplates/forgotPassword/html.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
<br/>
<p>You have requested a new password for your account registered with <%=siteName%> Email.</p>
<p>Click the following button to change your password</p></p>
<a style="display:block;color:#ffffff;background-color:#357df4;margin:48px 0 40px 0;min-height:48px;line-height:48px;padding:0 1em;font-weight:bold;" href="<%=siteAddrs%>/password_resets/<%=token%>" target="_blank">Change Password</a>
<a style="display:block;color:#ffffff;background-color:#357df4;margin:48px 0 40px 0;min-height:48px;line-height:48px;padding:0 1em;font-weight:bold;" href="<%=siteAddrs%>/resetPassword/<%=token%>" target="_blank">Change Password</a>
<p>Thanks for using us!</p>
6 changes: 5 additions & 1 deletion views/static/login.ejs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<h1>Login</h1>
<form method="POST" action="/login">
<form method="POST" action="/login">
<input type="email" name="email">
<input type="password" name="password">
<input type="submit" value="submit">
</form>

<form method="POST" action="/loginGoogle">
<input type="submit" value="Google Login">
</form>
1 change: 1 addition & 0 deletions views/static/signup.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<h1>Signup</h1>
<form method="POST" action="/user">
<input type="text" name="name">
<input type="email" name="email">
<input type="password" name="password">
<input type="submit" value="submit">
Expand Down

0 comments on commit 49545f8

Please sign in to comment.