142 lines
4.1 KiB
JavaScript
142 lines
4.1 KiB
JavaScript
import ngModule from '../module';
|
|
import UserError from 'core/lib/user-error';
|
|
|
|
/**
|
|
* Authentication service.
|
|
*
|
|
* @property {Boolean} loggedIn Whether the user is currently logged
|
|
*/
|
|
export default class Auth {
|
|
constructor($http, $q, $state, $transitions, $window, vnToken, vnTokenCreated, vnModules, aclService) {
|
|
Object.assign(this, {
|
|
$http,
|
|
$q,
|
|
$state,
|
|
$transitions,
|
|
$window,
|
|
vnToken,
|
|
vnTokenCreated,
|
|
vnModules,
|
|
aclService,
|
|
loggedIn: false
|
|
});
|
|
}
|
|
|
|
initialize() {
|
|
let criteria = {
|
|
to: state => {
|
|
const outLayout = ['login', 'recover-password', 'reset-password'];
|
|
return !outLayout.some(ol => ol == state.name);
|
|
}
|
|
};
|
|
this.$transitions.onStart(criteria, transition => {
|
|
this.getAccessTokenConfig();
|
|
if (this.loggedIn) {
|
|
console.log('firstIf');
|
|
return true;
|
|
}
|
|
|
|
let redirectToLogin = () => {
|
|
return transition.router.stateService.target('login', {
|
|
continue: this.$window.location.hash
|
|
});
|
|
};
|
|
|
|
if (this.vnToken.token) {
|
|
console.log('secondIf');
|
|
|
|
return this.loadAcls()
|
|
.then(() => true)
|
|
.catch(redirectToLogin);
|
|
} 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');
|
|
err.code = 'EmptyLogin';
|
|
return this.$q.reject(err);
|
|
}
|
|
|
|
let params = {
|
|
user,
|
|
password: password || undefined
|
|
};
|
|
|
|
return this.$http.post('VnUsers/signIn', params).then(
|
|
json => this.onLoginOk(json, remember));
|
|
}
|
|
|
|
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;
|
|
if (continueHash)
|
|
this.$window.location = continueHash;
|
|
else
|
|
this.$state.go('home');
|
|
});
|
|
}
|
|
|
|
logout() {
|
|
let promise = this.$http.post('VnUsers/logout', null, {
|
|
headers: {Authorization: this.vnToken.token}
|
|
}).catch(() => {});
|
|
|
|
this.vnToken.unset();
|
|
this.loggedIn = false;
|
|
this.vnModules.reset();
|
|
this.aclService.reset();
|
|
this.$state.go('login');
|
|
|
|
return promise;
|
|
}
|
|
|
|
loadAcls() {
|
|
return this.aclService.load()
|
|
.then(() => {
|
|
this.loggedIn = true;
|
|
this.vnModules.reset();
|
|
})
|
|
.catch(err => {
|
|
this.vnToken.unset();
|
|
throw err;
|
|
});
|
|
}
|
|
}
|
|
Auth.$inject = ['$http', '$q', '$state', '$transitions', '$window', 'vnToken', 'vnTokenCreated', 'vnModules', 'aclService'];
|
|
|
|
ngModule.service('vnAuth', Auth);
|