Merge pull request '#7190 - RenewTokenMultimedia' (!2335) from 7190_renewTokenMultimedia into dev
gitea/salix/pipeline/head This commit looks good Details

Reviewed-on: #2335
Reviewed-by: Alex Moreno <alexm@verdnatura.es>
This commit is contained in:
Javier Segarra 2024-04-23 08:28:08 +00:00
commit 27354e452f
2 changed files with 24 additions and 13 deletions

View File

@ -12,8 +12,8 @@ module.exports = Self => {
http: {
path: `/renewToken`,
verb: 'POST'
}
});
},
accessScopes: ['DEFAULT', 'read:multimedia']});
Self.renewToken = async function(ctx) {
const {accessToken: token} = ctx.req;

View File

@ -1,5 +1,6 @@
import ngModule from '../module';
const TOKEN_MULTIMEDIA = 'vnTokenMultimedia';
const TOKEN = 'vnToken';
/**
* Saves and loads the token for the current logged in user.
*
@ -58,8 +59,8 @@ export default class Token {
}
getStorage(storage) {
this.token = storage.getItem('vnToken');
this.tokenMultimedia = storage.getItem('vnTokenMultimedia');
this.token = storage.getItem(TOKEN);
this.tokenMultimedia = storage.getItem(TOKEN_MULTIMEDIA);
if (!this.token) return;
const created = storage.getItem('vnTokenCreated');
this.created = created && new Date(created);
@ -67,15 +68,15 @@ export default class Token {
}
setStorage(storage, token, tokenMultimedia, created, ttl) {
storage.setItem('vnTokenMultimedia', tokenMultimedia);
storage.setItem('vnToken', token);
storage.setItem(TOKEN_MULTIMEDIA, tokenMultimedia);
storage.setItem(TOKEN, token);
storage.setItem('vnTokenCreated', created.toJSON());
storage.setItem('vnTokenTtl', ttl);
}
removeStorage(storage) {
storage.removeItem('vnToken');
storage.removeItem('vnTokenMultimedia');
storage.removeItem(TOKEN);
storage.removeItem(TOKEN_MULTIMEDIA);
storage.removeItem('vnTokenCreated');
storage.removeItem('vnTokenTtl');
}
@ -96,9 +97,9 @@ export default class Token {
this.checking = true;
const renewPeriod = Math.min(this.ttl, this.renewPeriod) * 1000;
const maxDate = this.created.getTime() + renewPeriod;
const now = new Date();
const now = new Date().getTime();
if (now.getTime() <= maxDate) {
if (now <= maxDate) {
this.checking = false;
return;
}
@ -106,7 +107,17 @@ export default class Token {
this.$http.post('VnUsers/renewToken')
.then(res => {
const token = res.data;
this.set(token.id, now, token.ttl, this.remember);
const tokenMultimedia =
localStorage.getItem(TOKEN_MULTIMEDIA)
?? sessionStorage.getItem(TOKEN_MULTIMEDIA);
return this.$http.post('VnUsers/renewToken', null, {
headers: {Authorization: tokenMultimedia}
})
.then(({data}) => {
const tokenMultimedia = data;
this.set(token.id, tokenMultimedia.id, new Date(), token.ttl, this.remember);
});
})
.finally(() => {
this.checking = false;
@ -119,4 +130,4 @@ export default class Token {
}
Token.$inject = ['vnInterceptor', '$http', '$rootScope'];
ngModule.service('vnToken', Token);
ngModule.service(TOKEN, Token);