feat(User/Auth): Use stand alone auth token
This commit is contained in:
committed by
mrugesh mohapatra
parent
7a922229f4
commit
07f30427cb
@ -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() {
|
User.prototype.getEncodedEmail = function getEncodedEmail() {
|
||||||
if (!this.email) {
|
if (!this.email) {
|
||||||
return null;
|
return null;
|
||||||
@ -506,7 +512,7 @@ module.exports = function(User) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create a temporary access token with ttl for 15 minutes
|
// 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 => {
|
.flatMap(token => {
|
||||||
let renderAuthEmail = renderSignInEmail;
|
let renderAuthEmail = renderSignInEmail;
|
||||||
|
@ -288,6 +288,14 @@
|
|||||||
"type": "hasOne",
|
"type": "hasOne",
|
||||||
"model": "pledge",
|
"model": "pledge",
|
||||||
"foreignKey": ""
|
"foreignKey": ""
|
||||||
|
},
|
||||||
|
"authTokens": {
|
||||||
|
"type": "hasMany",
|
||||||
|
"model": "AuthToken",
|
||||||
|
"foreignKey": "userId",
|
||||||
|
"options": {
|
||||||
|
"disableInclude": true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"acls": [
|
"acls": [
|
||||||
|
@ -21,7 +21,7 @@ module.exports = function enableAuthentication(app) {
|
|||||||
const ifUserRedirect = ifUserRedirectTo();
|
const ifUserRedirect = ifUserRedirectTo();
|
||||||
const router = app.loopback.Router();
|
const router = app.loopback.Router();
|
||||||
const api = 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('/login', (req, res) => res.redirect(301, '/signin'));
|
||||||
router.get('/logout', (req, res) => res.redirect(301, '/signout'));
|
router.get('/logout', (req, res) => res.redirect(301, '/signout'));
|
||||||
@ -99,7 +99,7 @@ module.exports = function enableAuthentication(app) {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
// first find
|
// first find
|
||||||
return AccessToken.findOne$({ where: { id: authTokenId } })
|
return AuthToken.findOne$({ where: { id: authTokenId } })
|
||||||
.flatMap(authToken => {
|
.flatMap(authToken => {
|
||||||
if (!authToken) {
|
if (!authToken) {
|
||||||
throw wrapHandledError(
|
throw wrapHandledError(
|
||||||
@ -135,7 +135,7 @@ module.exports = function enableAuthentication(app) {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return authToken.validate$()
|
return authToken.validate()
|
||||||
.map(isValid => {
|
.map(isValid => {
|
||||||
if (!isValid) {
|
if (!isValid) {
|
||||||
throw wrapHandledError(
|
throw wrapHandledError(
|
||||||
@ -150,7 +150,7 @@ module.exports = function enableAuthentication(app) {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return authToken.destroy$();
|
return authToken.destroy();
|
||||||
})
|
})
|
||||||
.map(() => user);
|
.map(() => user);
|
||||||
});
|
});
|
||||||
|
@ -78,5 +78,9 @@
|
|||||||
"about": {
|
"about": {
|
||||||
"dataSource": "db",
|
"dataSource": "db",
|
||||||
"public": true
|
"public": true
|
||||||
|
},
|
||||||
|
"AuthToken": {
|
||||||
|
"dataSource": "db",
|
||||||
|
"public": false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
15
server/models/auth-token.js
Normal file
15
server/models/auth-token.js
Normal file
@ -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
server/models/auth-token.json
Normal file
13
server/models/auth-token.json
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"name": "AuthToken",
|
||||||
|
"base": "AccessToken",
|
||||||
|
"idInjection": true,
|
||||||
|
"options": {
|
||||||
|
"validateUpsert": true
|
||||||
|
},
|
||||||
|
"properties": {},
|
||||||
|
"validations": [],
|
||||||
|
"relations": {},
|
||||||
|
"acls": [],
|
||||||
|
"methods": {}
|
||||||
|
}
|
Reference in New Issue
Block a user