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, vnApp, $translate, $state, $transitions, $window, vnToken, vnModules) { Object.assign(this, { $http, $q, vnApp, $translate, $state, $transitions, $window, vnToken, vnModules, loggedIn: false }); } initialize() { let criteria = { to: state => { const outLayout = ['login', 'recover-password', 'reset-password', 'change-password', 'validate-email']; return !outLayout.some(ol => ol == state.name); } }; this.$transitions.onStart(criteria, transition => { if (this.loggedIn) return true; let redirectToLogin = () => { return transition.router.stateService.target('login', { continue: this.$window.location.hash }); }; if (this.vnToken.token) { const loadWithRetry = () => { return this.validateToken() .then(() => true) .catch(err => { switch (err.status) { case 400: case 401: return redirectToLogin(); default: return new Promise(resolve => { setTimeout(() => { this.vnApp.showMessage(this.$translate.instant('Loading...')); resolve(loadWithRetry()); }, 2000); }); } }); }; return loadWithRetry(); } else return redirectToLogin(); }); } 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 }; const now = new Date(); return this.$http.post('VnUsers/sign-in', params).then( json => this.onLoginOk(json, now, remember)); } validateCode(user, password, code, remember) { if (!user) { let err = new UserError('Please enter your username'); err.code = 'EmptyLogin'; return this.$q.reject(err); } let params = { user: user, password: password || undefined, code: code }; const now = new Date(); return this.$http.post('VnUsers/validate-auth', params) .then(json => this.onLoginOk(json, now, remember)); } onLoginOk(json, now, remember) { return this.$http.get('VnUsers/ShareToken', { headers: {Authorization: json.data.token} }).then(({data}) => { this.vnToken.set(json.data.token, data.multimediaToken.id, now, json.data.ttl, remember); let continueHash = this.$state.params.continue; if (continueHash) this.$window.location = continueHash; else this.$state.go('home'); }).catch(() => {}); } logout() { this.$http.post('Accounts/logout', null, {headers: {'Authorization': this.vnToken.tokenMultimedia}, }).catch(() => {}); let promise = this.$http.post('VnUsers/logout', null, { headers: {Authorization: this.vnToken.token} }).catch(() => {}); this.vnToken.unset(); this.loggedIn = false; this.vnModules.reset(); this.vnModules.aclService.reset(); this.$state.go('login'); return promise; } validateToken() { return this.$http.get('VnUsers/validateToken') .then(() => { this.loggedIn = true; this.vnModules.reset(); }) .catch(err => { throw err; }); } } Auth.$inject = [ '$http', '$q', 'vnApp', '$translate', '$state', '$transitions', '$window', 'vnToken', 'vnModules']; ngModule.service('vnAuth', Auth);