Skip to content

Commit

Permalink
feat(User/Auth): Use stand alone auth token
Browse files Browse the repository at this point in the history
  • Loading branch information
BerkeleyTrue authored and raisedadead committed Jan 2, 2018
1 parent 7a92222 commit 07f3042
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 5 deletions.
8 changes: 7 additions & 1 deletion common/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,12 @@ module.exports = function(User) {
}
);

User.prototype.createAuthToken = function createAuthToken({ ttl } = {}) {
return Observable.fromNodeCallback(
this.authTokens.create.bind(this.authTokens)
)({ ttl });
};

User.prototype.getEncodedEmail = function getEncodedEmail() {
if (!this.email) {
return null;
Expand All @@ -506,7 +512,7 @@ module.exports = function(User) {
}

// create a temporary access token with ttl for 15 minutes
return this.createAccessToken$({ ttl: 15 * 60 * 1000 });
return this.createAuthToken({ ttl: 15 * 60 * 1000 });
})
.flatMap(token => {
let renderAuthEmail = renderSignInEmail;
Expand Down
8 changes: 8 additions & 0 deletions common/models/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,14 @@
"type": "hasOne",
"model": "pledge",
"foreignKey": ""
},
"authTokens": {
"type": "hasMany",
"model": "AuthToken",
"foreignKey": "userId",
"options": {
"disableInclude": true
}
}
},
"acls": [
Expand Down
8 changes: 4 additions & 4 deletions server/boot/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = function enableAuthentication(app) {
const ifUserRedirect = ifUserRedirectTo();
const router = app.loopback.Router();
const api = app.loopback.Router();
const { AccessToken, User } = app.models;
const { AuthToken, User } = app.models;

router.get('/login', (req, res) => res.redirect(301, '/signin'));
router.get('/logout', (req, res) => res.redirect(301, '/signout'));
Expand Down Expand Up @@ -99,7 +99,7 @@ module.exports = function enableAuthentication(app) {
));
}
// first find
return AccessToken.findOne$({ where: { id: authTokenId } })
return AuthToken.findOne$({ where: { id: authTokenId } })
.flatMap(authToken => {
if (!authToken) {
throw wrapHandledError(
Expand Down Expand Up @@ -135,7 +135,7 @@ module.exports = function enableAuthentication(app) {
}
);
}
return authToken.validate$()
return authToken.validate()
.map(isValid => {
if (!isValid) {
throw wrapHandledError(
Expand All @@ -150,7 +150,7 @@ module.exports = function enableAuthentication(app) {
}
);
}
return authToken.destroy$();
return authToken.destroy();
})
.map(() => user);
});
Expand Down
4 changes: 4 additions & 0 deletions server/model-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,9 @@
"about": {
"dataSource": "db",
"public": true
},
"AuthToken": {
"dataSource": "db",
"public": false
}
}
15 changes: 15 additions & 0 deletions server/models/auth-token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Observable } from 'rx';

export default function(AuthToken) {
AuthToken.on('dataSourceAttached', () => {
AuthToken.findOne$ = Observable.fromNodeCallback(
AuthToken.findOne.bind(AuthToken)
);
AuthToken.prototype.validate = Observable.fromNodeCallback(
AuthToken.prototype.validate
);
AuthToken.prototype.destroy = Observable.fromNodeCallback(
AuthToken.prototype.destroy
);
});
}
13 changes: 13 additions & 0 deletions server/models/auth-token.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "AuthToken",
"base": "AccessToken",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}

0 comments on commit 07f3042

Please sign in to comment.