This commit is contained in:
parent
886c4a15b5
commit
b95ee6cbfa
|
@ -17,37 +17,6 @@ export class Layout extends Component {
|
|||
this.getAccessTokenConfig();
|
||||
}
|
||||
|
||||
getAccessTokenConfig() {
|
||||
this.$http.get('AccessTokenConfigs').then(json => {
|
||||
const firtsResult = json.data[0];
|
||||
if (!firtsResult) return;
|
||||
window.localStorage.renewPeriod = firtsResult.renewPeriod;
|
||||
window.localStorage.renewInterval = firtsResult.renewInterval;
|
||||
|
||||
const intervalMilliseconds = firtsResult.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 => {
|
||||
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() {
|
||||
this.$http.get('VnUsers/getCurrentUserData').then(json => {
|
||||
this.$.$root.user = json.data;
|
||||
|
@ -67,6 +36,38 @@ export class Layout extends Component {
|
|||
window.location.reload();
|
||||
}
|
||||
|
||||
getAccessTokenConfig() {
|
||||
this.$http.get('AccessTokenConfigs').then(json => {
|
||||
const firtsResult = json.data[0];
|
||||
if (!firtsResult) return;
|
||||
window.localStorage.renewPeriod = firtsResult.renewPeriod;
|
||||
window.localStorage.renewInterval = firtsResult.renewInterval;
|
||||
|
||||
const intervalMilliseconds = firtsResult.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);
|
||||
|
||||
console.log(differenceSeconds, window.localStorage.renewPeriod);
|
||||
if (differenceSeconds > window.localStorage.renewPeriod) {
|
||||
this.$http.post('VnUsers/renewToken')
|
||||
.then(json => {
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
$onDestroy() {
|
||||
clearInterval(this.inservalId);
|
||||
}
|
||||
|
|
|
@ -37,4 +37,51 @@ describe('Component vnLayout', () => {
|
|||
expect(url).not.toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getAccessTokenConfig()', () => {
|
||||
it(`should set the renewPeriod and renewInterval properties in localStorage`, () => {
|
||||
const response = [{
|
||||
renewPeriod: 100,
|
||||
renewInterval: 5
|
||||
}];
|
||||
|
||||
$httpBackend.expect('GET', `AccessTokenConfigs`).respond(response);
|
||||
controller.getAccessTokenConfig();
|
||||
$httpBackend.flush();
|
||||
|
||||
const renewPeriod = localStorage.getItem('renewPeriod');
|
||||
const renewInterval = localStorage.getItem('renewInterval');
|
||||
|
||||
expect(renewPeriod).toBe('100');
|
||||
expect(renewInterval).toBe('5');
|
||||
expect(controller.inservalId).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('checkTokenValidity()', () => {
|
||||
it(`should not call renewToken and not set vnToken in the controller`, () => {
|
||||
localStorage.setItem('renewPeriod', 100);
|
||||
controller.vnTokenCreated.created = new Date();
|
||||
|
||||
controller.checkTokenValidity();
|
||||
|
||||
expect(controller.vnToken.token).toBeNull();
|
||||
});
|
||||
|
||||
it(`should call renewToken and set vnToken and vnTokenCreated properties in the controller`, () => {
|
||||
const response = {
|
||||
token: 999,
|
||||
created: new Date()
|
||||
};
|
||||
localStorage.setItem('renewPeriod', 100);
|
||||
controller.vnTokenCreated.created = new Date(Date.now() - (60 * 60 * 1000));
|
||||
|
||||
$httpBackend.expect('POST', `VnUsers/renewToken`).respond(response);
|
||||
controller.checkTokenValidity();
|
||||
$httpBackend.flush();
|
||||
|
||||
expect(controller.vnToken.token).toBe(999);
|
||||
expect(controller.vnTokenCreated.created).toEqual(response.created);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue