forked from meanjs/mean
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): JWT Authentication simplified
Implements JWT Authentication, and removes dependency of session storage. Closes meanjs#389
- Loading branch information
Showing
19 changed files
with
202 additions
and
123 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
|
||
var config = require('../config'), | ||
jwt = require('jsonwebtoken'), | ||
lodash = require('lodash'); | ||
|
||
var auth = { | ||
signToken: signToken | ||
}; | ||
|
||
// Export the token auth service | ||
module.exports = auth; | ||
|
||
// Sign the Token | ||
function signToken(user, options) { | ||
var payload, | ||
token, | ||
jwtOptions; | ||
|
||
if (!user || !user._id) { | ||
return null; | ||
} | ||
|
||
options = options || {}; | ||
|
||
payload = { | ||
user: user._id.toString() | ||
}; | ||
|
||
jwtOptions = lodash.merge(config.jwt.options, options); | ||
|
||
token = jwt.sign(payload, config.jwt.secret, jwtOptions); | ||
|
||
return token; | ||
} |
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
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
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,32 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Module dependencies | ||
*/ | ||
var passport = require('passport'), | ||
JwtStrategy = require('passport-jwt').Strategy, | ||
ExtractJwt = require('passport-jwt').ExtractJwt, | ||
User = require('mongoose').model('User'); | ||
|
||
module.exports = function (config) { | ||
var opts = { | ||
jwtFromRequest: ExtractJwt.versionOneCompatibility({ tokenQueryParameterName: 'auth_token' }), | ||
secretOrKey: config.jwt.secret | ||
// opts.issuer = "accounts.examplesoft.com", | ||
// opts.audience = "yoursite.net" | ||
}; | ||
|
||
passport.use(new JwtStrategy(opts, function (jwt_payload, done) { | ||
User.findById({ _id: jwt_payload.user }, '-salt -password', function (err, user) { | ||
if (err) { | ||
return done(err, false); | ||
} | ||
|
||
if (!user) { | ||
return done('User not found'); | ||
} | ||
|
||
return done(null, user); | ||
}); | ||
})); | ||
}; |
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
Oops, something went wrong.