5554-renovar_token #1566

Merged
vicent merged 24 commits from 5554-renovar_token into dev 2023-06-20 12:38:47 +00:00
3 changed files with 49 additions and 40 deletions
Showing only changes of commit 6010bc1253 - Show all commits

View File

@ -2,6 +2,10 @@ module.exports = Self => {
Self.remoteMethodCtx('renewToken', { Self.remoteMethodCtx('renewToken', {
description: 'Send email to the user', description: 'Send email to the user',
accepts: [], accepts: [],
returns: {
type: 'Object',
root: true
},
http: { http: {
path: `/renewToken`, path: `/renewToken`,
verb: 'POST' verb: 'POST'
@ -12,7 +16,7 @@ module.exports = Self => {
const models = Self.app.models; const models = Self.app.models;
const userId = ctx.req.accessToken.userId; const userId = ctx.req.accessToken.userId;
const created = ctx.req.accessToken.created; const created = ctx.req.accessToken.created;
// const tokenId = ctx.req.accessToken.id; const tokenId = ctx.req.accessToken.id;
const now = new Date(); const now = new Date();
const differenceMilliseconds = now - created; const differenceMilliseconds = now - created;
@ -29,10 +33,8 @@ module.exports = Self => {
return response; return response;
vicent marked this conversation as resolved Outdated
Outdated
Review

Devolver un 'UserError' si todavía no se ha pasado el tiempo de renovación.

Devolver un 'UserError' si todavía no se ha pasado el tiempo de renovación.
} }
await models.AccessToken.destroyAll({userId: userId});
// await models.AccessToken.destroyById(tokenId);
const accessToken = await models.AccessToken.create({userId: userId}); const accessToken = await models.AccessToken.create({userId: userId});
await models.AccessToken.destroyById(tokenId);
return {token: accessToken.id, created: accessToken.created}; return {token: accessToken.id, created: accessToken.created};
}; };

View File

@ -30,11 +30,8 @@ export default class Auth {
} }
}; };
this.$transitions.onStart(criteria, transition => { this.$transitions.onStart(criteria, transition => {
this.getAccessTokenConfig(); if (this.loggedIn)
if (this.loggedIn) {
console.log('firstIf');
return true; return true;
}
let redirectToLogin = () => { let redirectToLogin = () => {
return transition.router.stateService.target('login', { return transition.router.stateService.target('login', {
@ -43,43 +40,14 @@ export default class Auth {
}; };
if (this.vnToken.token) { if (this.vnToken.token) {
console.log('secondIf');
return this.loadAcls() return this.loadAcls()
.then(() => true) .then(() => true)
.catch(redirectToLogin); .catch(redirectToLogin);
} else { } else
console.log('else');
return redirectToLogin(); return redirectToLogin();
}
}); });
} }
getAccessTokenConfig() {
this.$http.get('AccessTokenConfigs/findOne').then(json => {
window.localStorage.renewPeriod = json.data.renewPeriod;
window.localStorage.renewInterval = json.data.renewInterval;
const intervalMilliseconds = json.data.renewInterval * 1000;
setInterval(this.checkTokenValidity.bind(this), intervalMilliseconds);
});
}
checkTokenValidity() {
const now = new Date();
const differenceMilliseconds = now - new Date(this.vnTokenCreated.created);
const differenceSeconds = Math.floor(differenceMilliseconds / 1000);
console.log(differenceSeconds, window.localStorage.renewPeriod);
if (differenceSeconds > window.localStorage.renewPeriod) {
this.$http.post('VnUsers/renewToken')
.then(() => {
console.log('fin');
});
}
}
login(user, password, remember) { login(user, password, remember) {
if (!user) { if (!user) {
let err = new UserError('Please enter your username'); let err = new UserError('Please enter your username');

View File

@ -3,13 +3,48 @@ import Component from 'core/lib/component';
import './style.scss'; import './style.scss';
export class Layout extends Component { export class Layout extends Component {
constructor($element, $, vnModules) { constructor($element, $, vnModules, vnToken, vnTokenCreated) {
super($element, $); super($element, $);
this.modules = vnModules.get(); this.modules = vnModules.get();
Object.assign(this, {
vnToken,
vnTokenCreated
});
} }
$onInit() { $onInit() {
this.getUserData(); this.getUserData();
this.getAccessTokenConfig();
}
getAccessTokenConfig() {
this.$http.get('AccessTokenConfigs/findOne').then(json => {
window.localStorage.renewPeriod = json.data.renewPeriod;
window.localStorage.renewInterval = json.data.renewInterval;
const intervalMilliseconds = json.data.renewInterval * 1000;
this.inservalId = setInterval(this.checkTokenValidity.bind(this), intervalMilliseconds);
});
}
checkTokenValidity() {
const now = new Date();
const differenceMilliseconds = now - new Date(this.vnTokenCreated.created);
const differenceSeconds = Math.floor(differenceMilliseconds / 1000);
if (differenceSeconds > window.localStorage.renewPeriod) {
this.$http.post('VnUsers/renewToken')
.then(json => {
console.log('fin renewToken');
if (json.data.token) {
let remember = true;
vicent marked this conversation as resolved Outdated
Outdated
Review

No guardar renewPeriod y renewInterval en localStorage, guardarlos en this

No guardar `renewPeriod` y `renewInterval` en `localStorage`, guardarlos en `this`
if (window.sessionStorage.vnToken) remember = false;
this.vnToken.set(json.data.token, remember);
this.vnTokenCreated.set(json.data.created, remember);
}
});
}
} }
getUserData() { getUserData() {
@ -30,8 +65,12 @@ export class Layout extends Component {
refresh() { refresh() {
window.location.reload(); window.location.reload();
} }
$onDestroy() {
clearInterval(this.inservalId);
}
} }
Layout.$inject = ['$element', '$scope', 'vnModules']; Layout.$inject = ['$element', '$scope', 'vnModules', 'vnToken', 'vnTokenCreated'];
ngModule.vnComponent('vnLayout', { ngModule.vnComponent('vnLayout', {
template: require('./index.html'), template: require('./index.html'),