-
Change
token-auth/models/UserInterface.jstoAdapter.js -
Change
TokenAuthUsertoTokenAuthAdapter -
TokenAuthAdapter provides an instance of ApiClient instead of User. This is what gets bound to
req.userclass ApiClient { constructor() { this.user = null; this.terminal = null; } /** * @return {Array} */ roles() {} } -
I think now that we've got this better model, TokenAuthAdapter should extend Adapter from token-auth, which means that we can move
serializeUseranddeserializeUserinto Adapter
Last active
August 29, 2015 14:04
-
-
Save simonexmachina/f0b1a861403aed5d7a8c to your computer and use it in GitHub Desktop.
token-auth adapter
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
| diff --git a/modules/token-auth/models/UserInterface.js b/modules/token-auth/models/UserInterface.js | |
| index ff14924..39e88a1 100644 | |
| --- a/modules/token-auth/models/UserInterface.js | |
| +++ b/modules/token-auth/models/UserInterface.js | |
| @@ -44,3 +44,17 @@ UserInterface.findByUsername = function(username) {}; | |
| @return Promise A promise that yields an instance | |
| */ | |
| UserInterface.findById = function(id) {}; | |
| + | |
| +/** | |
| + * Called by Passport | |
| + * @param user | |
| + * @param done | |
| + */ | |
| +UserInterface.serializeUser = function (user, done); | |
| + | |
| +/** | |
| + * Called by Passport | |
| + * @param id | |
| + * @param done | |
| + */ | |
| +UserInterface.deserializeUser = function(id, done) | |
| diff --git a/modules/token-auth/token-auth.js b/modules/token-auth/token-auth.js | |
| index a2629a5..caea1e5 100644 | |
| --- a/modules/token-auth/token-auth.js | |
| +++ b/modules/token-auth/token-auth.js | |
| @@ -35,14 +35,10 @@ exports.recoverRoutes = function(app, urlPrefix) { | |
| /** | |
| @param User {UserInterface} | |
| */ | |
| -exports.initPassport = function(User) { | |
| - passport.serializeUser(function (user, done) { | |
| - done(null, {id: user.getId()}); | |
| - }); | |
| - passport.deserializeUser(function (id, done) { | |
| - User.findById(id).nodeify(done); | |
| - }); | |
| - passport.use(exports.localStrategy(User)); | |
| +exports.initPassport = function(Adapter) { | |
| + passport.serializeUser(Adapter.serializeUser); | |
| + passport.deserializeUser(Adapter.deserializeUser); | |
| + passport.use(exports.localStrategy(Adapter)); | |
| }; | |
| exports.generateToken = function(payload, expiresInMinutes) { | |
| @@ -65,10 +61,10 @@ exports.extractToken = exports.degenerateToken; | |
| /** | |
| @param User {UserInterface} | |
| */ | |
| -exports.localStrategy = function(User) { | |
| +exports.localStrategy = function(Adapter) { | |
| return new LocalStrategy(function (username, password, done) { | |
| log.debug('authenticating', username); | |
| - User.findByUsername(username) | |
| + Adapter.findByUsername(username) | |
| .then(function(user) { | |
| log.debug('findByUsername returned', user); | |
| if (!user) { | |
| diff --git a/src/models/TokenAuthUser.js b/src/models/TokenAuthUser.js | |
| index 81874c7..58060dd 100644 | |
| --- a/src/models/TokenAuthUser.js | |
| +++ b/src/models/TokenAuthUser.js | |
| @@ -25,6 +25,7 @@ class TokenAuthUser extends User { | |
| authenticates. This should include `token`, `expiresInMinutes` and `user` | |
| */ | |
| authenticatedResponse(token, tokenPayload) { | |
| + // if terminal | |
| return { | |
| token: token, | |
| expires: tokenPayload.exp, | |
| @@ -39,6 +40,7 @@ class TokenAuthUser extends User { | |
| @see UserInterface#create | |
| */ | |
| tokenPayload() { | |
| + // if terminal | |
| return { | |
| user: this.serialize(), | |
| activeOrganisation: this.activeOrganisation, | |
| @@ -66,6 +68,7 @@ class TokenAuthUser extends User { | |
| */ | |
| TokenAuthUser.create = function(req) { | |
| var tokenPayload = req.user; | |
| + // return a new ApiClient here | |
| var user = new TokenAuthUser(tokenPayload.user); | |
| user.activeOrganisation = tokenPayload.activeOrganisation; | |
| user.roles = tokenPayload.roles; | |
| @@ -145,4 +148,11 @@ TokenAuthUser.fetch = function(where) { | |
| }); | |
| }; | |
| +TokenAuthUser.serializeUser = function(user, done) { | |
| + done(null, {id: user.getId()}); | |
| +}; | |
| +TokenAuthUser.deserializeUser = function(id, done) { | |
| + User.findById(id).nodeify(done); | |
| +}; | |
| + | |
| module.exports = TokenAuthUser; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment