sails-generate-auth
Advanced tools
Comparing version 0.1.4 to 0.1.5
{ | ||
"name": "sails-generate-auth", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "Generate a Passport.js authentication layer for your Sails app that will Rock Your Socks™.", | ||
@@ -5,0 +5,0 @@ "author": "Kasper Isager <kasperisager@gmail.com>", |
@@ -73,3 +73,3 @@ ![image_squidhome@2x.png](http://i.imgur.com/RIvu9.png) | ||
The only requirements, besides running the generator and adding some providers in `config/passport.js`, is having a model named "User" in your application as well as a set of routes that exposes the authentication endpoints. You'll also need to load the Passport.js middleware for all your controllers and install the required NPM packages. | ||
The only requirements, besides running the generator and adding some providers in `config/passport.js`, is having a model named "User" in your application as well as a set of routes that exposes the authentication endpoints. You'll also need to load the Passport.js middleware for all your controllers and install the required NPM packages. Lastly, you need to add a line to `config/bootstrap.js` to load your Passport providers on startup. | ||
@@ -106,2 +106,8 @@ At the very least, your User model needs to look like this: | ||
Next, change your `config/bootstrap.js` to load your Passport providers on startup by adding the following line: | ||
```javascript | ||
sails.services.passport.loadStrategies(); | ||
``` | ||
All required Passport.js middleware is contained within the `passport` policy so all you need to do is load it before your controllers in `config/policies.js`: | ||
@@ -108,0 +114,0 @@ |
@@ -121,14 +121,18 @@ /** | ||
callback: function (req, res) { | ||
function tryAgain () { | ||
// If an error was thrown, redirect the user to the login which should | ||
// take care of rendering the error messages. | ||
req.flash('form', req.body); | ||
res.redirect(req.param('action') === 'register' ? '/register' : '/login'); | ||
} | ||
passport.callback(req, res, function (err, user) { | ||
req.login(user, function (err) { | ||
// If an error was thrown, redirect the user to the login which should | ||
// take care of rendering the error messages. | ||
if (err) { | ||
res.redirect(req.param('action') === 'register' ? '/register' : '/login'); | ||
} | ||
if (err) return tryAgain(); | ||
req.login(user, function (loginErr) { | ||
if (loginErr) return tryAgain(); | ||
// Upon successful login, send the user to the homepage were req.user | ||
// will available. | ||
else { | ||
res.redirect('/'); | ||
} | ||
res.redirect('/'); | ||
}); | ||
@@ -135,0 +139,0 @@ }); |
@@ -1,4 +0,4 @@ | ||
var passport = require('passport') | ||
, path = require('path') | ||
, url = require('url'); | ||
var path = require('path') | ||
, url = require('url') | ||
, passport = require('passport'); | ||
@@ -86,4 +86,4 @@ /** | ||
// whoever's next in the line take care of it. | ||
if (!Object.keys(user).length) { | ||
return next(new Error('Neither a username or email was available', null)); | ||
if (!user.username && !user.email) { | ||
return next(new Error('Neither a username nor email was available')); | ||
} | ||
@@ -103,4 +103,10 @@ | ||
User.create(user, function (err, user) { | ||
if (err) return next(err); | ||
if (err) { | ||
if(err.code === "E_VALIDATION"){ | ||
req.flash('error', err.invalidAttributes.email ? | ||
'Error.Passport.Email.Exists' : 'Error.Passport.User.Exists'); | ||
} | ||
return next(err); | ||
} | ||
query.user = user.id; | ||
@@ -181,5 +187,2 @@ | ||
// Load authentication strategies | ||
this.loadStrategies(req); | ||
// Redirect the user to the provider for authentication. When complete, | ||
@@ -218,4 +221,2 @@ // the provider will redirect the user back to the application at | ||
} else { | ||
// Load authentication strategies | ||
this.loadStrategies(req); | ||
@@ -250,5 +251,4 @@ // The provider will redirect the user to this URL after approval. Finish | ||
* | ||
* @param {Object} req | ||
*/ | ||
passport.loadStrategies = function (req) { | ||
passport.loadStrategies = function () { | ||
var self = this | ||
@@ -280,12 +280,14 @@ , strategies = sails.config.passport; | ||
Strategy = strategies[key].strategy; | ||
var baseUrl = sails.getBaseurl(); | ||
switch (protocol) { | ||
case 'oauth': | ||
case 'oauth2': | ||
options.callbackURL = url.resolve(req.baseUrl, callback); | ||
options.callbackURL = url.resolve(baseUrl, callback); | ||
break; | ||
case 'openid': | ||
options.returnURL = url.resolve(req.baseUrl, callback); | ||
options.realm = req.baseUrl; | ||
options.returnURL = url.resolve(baseUrl, callback); | ||
options.realm = baseUrl; | ||
options.profile = true; | ||
@@ -292,0 +294,0 @@ break; |
@@ -50,3 +50,10 @@ var validator = require('validator'); | ||
if (err) { | ||
req.flash('error', 'Error.Passport.User.Exists'); | ||
if (err.code === 'E_VALIDATION') { | ||
if (err.invalidAttributes.email) { | ||
req.flash('error', 'Error.Passport.Email.Exists'); | ||
} else { | ||
req.flash('error', 'Error.Passport.User.Exists'); | ||
} | ||
} | ||
return next(err); | ||
@@ -60,3 +67,13 @@ } | ||
}, function (err, passport) { | ||
next(err, user); | ||
if (err) { | ||
if (err.code === 'E_VALIDATION') { | ||
req.flash('error', 'Error.Passport.Password.Invalid'); | ||
} | ||
return user.destroy(function (destroyErr) { | ||
next(destroyErr || err); | ||
}); | ||
} | ||
next(null, user); | ||
}); | ||
@@ -85,3 +102,5 @@ }); | ||
}, function (err, passport) { | ||
if (err) return next(err); | ||
if (err) { | ||
return next(err); | ||
} | ||
@@ -127,3 +146,5 @@ if (!passport) { | ||
User.findOne(query, function (err, user) { | ||
if (err) return next(err); | ||
if (err) { | ||
return next(err); | ||
} | ||
@@ -146,3 +167,5 @@ if (!user) { | ||
passport.validatePassword(password, function (err, res) { | ||
if (err) return next(err); | ||
if (err) { | ||
return next(err); | ||
} | ||
@@ -149,0 +172,0 @@ if (!res) { |
{ | ||
"Error.Passport.Password.Wrong": "Whoa, that password wasn't quite right!", | ||
"Error.Passport.Password.NotSet": "Oh no, you haven't set a password yet!", | ||
"Error.Passport.Username.NotFound": "Uhm, what's your name again?", | ||
"Error.Passport.Email.NotFound": "That email doesn't seem right" | ||
"Error.Passport.Password.Invalid": "The provided password is invalid!", | ||
"Error.Passport.Password.Wrong": "Whoa, that password wasn't quite right!", | ||
"Error.Passport.Password.NotSet": "Oh no, you haven't set a password yet!", | ||
"Error.Passport.Username.NotFound": "Uhm, what's your name again?", | ||
"Error.Passport.User.Exists": "This username is already taken.", | ||
"Error.Passport.Email.NotFound": "That email doesn't seem right", | ||
"Error.Passport.Email.Missing": "You need to supply an email-address for verification", | ||
"Error.Passport.Email.Exists": "This email already exists. So try logging in.", | ||
"Error.Passport.Username.Missing": "You need to supply a username", | ||
"Error.Passport.Password.Missing": "Oh no, you haven't set a password yet!" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45789
978
164