refs #554 feat: actualizar el token automáticamente
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
This commit is contained in:
parent
e43e0a8f9f
commit
eab329b230
|
@ -0,0 +1,48 @@
|
|||
module.exports = Self => {
|
||||
Self.remoteMethodCtx('renewToken', {
|
||||
description: 'Send email to the user',
|
||||
accepts: [],
|
||||
http: {
|
||||
path: `/renewToken`,
|
||||
verb: 'POST'
|
||||
}
|
||||
});
|
||||
|
||||
Self.renewToken = async function(ctx, options) {
|
||||
const models = Self.app.models;
|
||||
const userId = ctx.req.accessToken.userId;
|
||||
const created = ctx.req.accessToken.created;
|
||||
// const tokenId = ctx.req.accessToken.id;
|
||||
const myOptions = {};
|
||||
let tx;
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
if (!myOptions.transaction) {
|
||||
tx = await Self.beginTransaction({});
|
||||
myOptions.transaction = tx;
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
const differenceMilliseconds = now - created;
|
||||
const differenceSeconds = Math.floor(differenceMilliseconds / 1000); // Convertir la diferencia a segundos
|
||||
|
||||
const accessTokenConfig = await models.AccessTokenConfig.findOne();
|
||||
if (differenceSeconds <= accessTokenConfig.renewPeriod) {
|
||||
const response = {
|
||||
statusCode: 200,
|
||||
data: {
|
||||
message: 'Token is active',
|
||||
}
|
||||
};
|
||||
return response;
|
||||
}
|
||||
|
||||
const accessToken = await models.AccessToken.create({userId: userId}, myOptions);
|
||||
await models.AccessToken.destroyAll({userId: userId}, myOptions);
|
||||
// await models.AccessToken.destroyById(tokenId, myOptions);
|
||||
|
||||
return {token: accessToken.id, created: accessToken.created};
|
||||
};
|
||||
};
|
|
@ -63,6 +63,6 @@ module.exports = Self => {
|
|||
|
||||
let loginInfo = Object.assign({password}, userInfo);
|
||||
token = await Self.login(loginInfo, 'user');
|
||||
return {token: token.id};
|
||||
return {token: token.id, created: token.created};
|
||||
};
|
||||
};
|
||||
|
|
|
@ -2,6 +2,14 @@
|
|||
"AccountingType": {
|
||||
"dataSource": "vn"
|
||||
},
|
||||
"AccessTokenConfig": {
|
||||
"dataSource": "vn",
|
||||
"options": {
|
||||
"mysql": {
|
||||
"table": "salix.accessTokenConfig"
|
||||
}
|
||||
}
|
||||
},
|
||||
"Bank": {
|
||||
"dataSource": "vn"
|
||||
},
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
{
|
||||
"name": "AccessTokenConfig",
|
||||
"base": "VnModel",
|
||||
"options": {
|
||||
"mysql": {
|
||||
"table": "accessTokenConfig"
|
||||
}
|
||||
},
|
||||
"properties": {
|
||||
"id": {
|
||||
"type": "number",
|
||||
"id": true,
|
||||
"description": "Identifier"
|
||||
},
|
||||
"renewPeriod": {
|
||||
"type": "number",
|
||||
"required": true
|
||||
},
|
||||
"renewInterval": {
|
||||
"type": "number",
|
||||
"required": true
|
||||
}
|
||||
},
|
||||
"acls": [{
|
||||
"accessType": "READ",
|
||||
"principalType": "ROLE",
|
||||
"principalId": "$everyone",
|
||||
"permission": "ALLOW"
|
||||
}]
|
||||
}
|
|
@ -10,6 +10,7 @@ module.exports = function(Self) {
|
|||
require('../methods/vn-user/recover-password')(Self);
|
||||
require('../methods/vn-user/validate-token')(Self);
|
||||
require('../methods/vn-user/privileges')(Self);
|
||||
require('../methods/vn-user/renew-token')(Self);
|
||||
|
||||
// Validations
|
||||
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
CREATE TABLE `salix`.`accessTokenConfig` (
|
||||
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
`renewPeriod` int(10) unsigned DEFAULT NULL,
|
||||
`renewInterval` int(10) unsigned DEFAULT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci;
|
|
@ -2886,6 +2886,6 @@ INSERT INTO `vn`.`wagonTypeTray` (`id`, `typeFk`, `height`, `colorFk`)
|
|||
(2, 1, 50, 2),
|
||||
(3, 1, 0, 3);
|
||||
|
||||
|
||||
|
||||
|
||||
INSERT INTO `salix`.`accessTokenConfig` (`id`, `renewPeriod`, `renewInterval`)
|
||||
VALUES
|
||||
(0, 50, 100);
|
||||
|
|
|
@ -7,7 +7,7 @@ import UserError from 'core/lib/user-error';
|
|||
* @property {Boolean} loggedIn Whether the user is currently logged
|
||||
*/
|
||||
export default class Auth {
|
||||
constructor($http, $q, $state, $transitions, $window, vnToken, vnModules, aclService) {
|
||||
constructor($http, $q, $state, $transitions, $window, vnToken, vnTokenCreated, vnModules, aclService) {
|
||||
Object.assign(this, {
|
||||
$http,
|
||||
$q,
|
||||
|
@ -15,6 +15,7 @@ export default class Auth {
|
|||
$transitions,
|
||||
$window,
|
||||
vnToken,
|
||||
vnTokenCreated,
|
||||
vnModules,
|
||||
aclService,
|
||||
loggedIn: false
|
||||
|
@ -29,8 +30,11 @@ export default class Auth {
|
|||
}
|
||||
};
|
||||
this.$transitions.onStart(criteria, transition => {
|
||||
if (this.loggedIn)
|
||||
this.getAccessTokenConfig();
|
||||
if (this.loggedIn) {
|
||||
console.log('firstIf');
|
||||
return true;
|
||||
}
|
||||
|
||||
let redirectToLogin = () => {
|
||||
return transition.router.stateService.target('login', {
|
||||
|
@ -39,14 +43,44 @@ export default class Auth {
|
|||
};
|
||||
|
||||
if (this.vnToken.token) {
|
||||
console.log('secondIf');
|
||||
|
||||
return this.loadAcls()
|
||||
.then(() => true)
|
||||
.catch(redirectToLogin);
|
||||
} else
|
||||
} else {
|
||||
console.log('else');
|
||||
|
||||
return redirectToLogin();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
getAccessTokenConfig() {
|
||||
this.$http.get('AccessTokenConfigs/findOne').then(json => {
|
||||
window.localStorage.renewPeriod = json.data.renewPeriod;
|
||||
window.localStorage.renewInterval = json.data.renewInterval;
|
||||
|
||||
this.checkTokenValidity();
|
||||
const intervalMilliseconds = 50 * 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');
|
||||
|
@ -65,6 +99,7 @@ export default class Auth {
|
|||
|
||||
onLoginOk(json, remember) {
|
||||
this.vnToken.set(json.data.token, remember);
|
||||
this.vnTokenCreated.set(json.data.created, remember);
|
||||
|
||||
return this.loadAcls().then(() => {
|
||||
let continueHash = this.$state.params.continue;
|
||||
|
@ -101,6 +136,6 @@ export default class Auth {
|
|||
});
|
||||
}
|
||||
}
|
||||
Auth.$inject = ['$http', '$q', '$state', '$transitions', '$window', 'vnToken', 'vnModules', 'aclService'];
|
||||
Auth.$inject = ['$http', '$q', '$state', '$transitions', '$window', 'vnToken', 'vnTokenCreated', 'vnModules', 'aclService'];
|
||||
|
||||
ngModule.service('vnAuth', Auth);
|
||||
|
|
|
@ -11,3 +11,5 @@ import './report';
|
|||
import './email';
|
||||
import './file';
|
||||
import './date';
|
||||
import './token-created';
|
||||
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
import ngModule from '../module';
|
||||
|
||||
/**
|
||||
* Saves and loads the created for the current logged in user.
|
||||
*
|
||||
* @property {String} created The current login created or %null
|
||||
*/
|
||||
export default class created {
|
||||
constructor() {
|
||||
try {
|
||||
this.created = sessionStorage.getItem('vnTokenCreated');
|
||||
if (!this.created)
|
||||
this.created = localStorage.getItem('vnTokenCreated');
|
||||
} catch (e) {}
|
||||
}
|
||||
set(value, remember) {
|
||||
this.unset();
|
||||
try {
|
||||
if (remember)
|
||||
localStorage.setItem('vnTokenCreated', value);
|
||||
else
|
||||
sessionStorage.setItem('vnTokenCreated', value);
|
||||
} catch (e) {}
|
||||
|
||||
this.created = value;
|
||||
}
|
||||
unset() {
|
||||
localStorage.removeItem('vnTokenCreated');
|
||||
sessionStorage.removeItem('vnTokenCreated');
|
||||
this.created = null;
|
||||
}
|
||||
}
|
||||
|
||||
ngModule.service('vnTokenCreated', created);
|
|
@ -10,6 +10,7 @@ export class Layout extends Component {
|
|||
|
||||
$onInit() {
|
||||
this.getUserData();
|
||||
// this.getAccessTokenConfig();
|
||||
}
|
||||
|
||||
getUserData() {
|
||||
|
@ -27,6 +28,25 @@ export class Layout extends Component {
|
|||
return `/api/Images/user/160x160/${userId}/download?access_token=${token}`;
|
||||
}
|
||||
|
||||
getAccessTokenConfig() {
|
||||
this.$http.get('AccessTokenConfigs/findOne').then(json => {
|
||||
window.localStorage.renewPeriod = json.data.renewPeriod;
|
||||
window.localStorage.renewInterval = json.data.renewInterval;
|
||||
|
||||
const intervalMilliseconds = 1 * 1000;
|
||||
const intervalID = setInterval(this.checkTokenValidity, intervalMilliseconds);
|
||||
});
|
||||
}
|
||||
|
||||
checkTokenValidity() {
|
||||
console.log('checkTokenValidity');
|
||||
|
||||
// this.$http.post('VnUsers/renewToken')
|
||||
// .then(() => {
|
||||
// console.log('fin');
|
||||
// });
|
||||
}
|
||||
|
||||
refresh() {
|
||||
window.location.reload();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue