refs #5554 movido codigo al vnLayout y parar el evento periódico
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Vicent Llopis 2023-05-25 09:51:56 +02:00
parent fdd22ca2d8
commit 6010bc1253
3 changed files with 49 additions and 40 deletions

View File

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

View File

@ -30,11 +30,8 @@ export default class Auth {
}
};
this.$transitions.onStart(criteria, transition => {
this.getAccessTokenConfig();
if (this.loggedIn) {
console.log('firstIf');
if (this.loggedIn)
return true;
}
let redirectToLogin = () => {
return transition.router.stateService.target('login', {
@ -43,43 +40,14 @@ export default class Auth {
};
if (this.vnToken.token) {
console.log('secondIf');
return this.loadAcls()
.then(() => true)
.catch(redirectToLogin);
} else {
console.log('else');
} else
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) {
if (!user) {
let err = new UserError('Please enter your username');

View File

@ -3,13 +3,48 @@ import Component from 'core/lib/component';
import './style.scss';
export class Layout extends Component {
constructor($element, $, vnModules) {
constructor($element, $, vnModules, vnToken, vnTokenCreated) {
super($element, $);
this.modules = vnModules.get();
Object.assign(this, {
vnToken,
vnTokenCreated
});
}
$onInit() {
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;
if (window.sessionStorage.vnToken) remember = false;
this.vnToken.set(json.data.token, remember);
this.vnTokenCreated.set(json.data.created, remember);
}
});
}
}
getUserData() {
@ -30,8 +65,12 @@ export class Layout extends Component {
refresh() {
window.location.reload();
}
$onDestroy() {
clearInterval(this.inservalId);
}
Layout.$inject = ['$element', '$scope', 'vnModules'];
}
Layout.$inject = ['$element', '$scope', 'vnModules', 'vnToken', 'vnTokenCreated'];
ngModule.vnComponent('vnLayout', {
template: require('./index.html'),