2019-01-23 12:11:44 +00:00
|
|
|
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, $state, $transitions, $window, vnToken, vnModules, aclService) {
|
|
|
|
Object.assign(this, {
|
|
|
|
$http,
|
|
|
|
$state,
|
|
|
|
$transitions,
|
|
|
|
$window,
|
|
|
|
vnToken,
|
|
|
|
vnModules,
|
|
|
|
aclService,
|
2019-01-25 11:04:35 +00:00
|
|
|
loggedIn: false
|
2019-01-23 12:11:44 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
initialize() {
|
|
|
|
let criteria = {
|
|
|
|
to: state => state.name != 'login'
|
|
|
|
};
|
|
|
|
this.$transitions.onStart(criteria, transition => {
|
2019-01-25 11:04:35 +00:00
|
|
|
if (this.loggedIn)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
let redirectToLogin = () => {
|
|
|
|
return transition.router.stateService.target('login', {
|
|
|
|
continue: this.$window.location.hash
|
|
|
|
});
|
|
|
|
};
|
2019-01-23 12:11:44 +00:00
|
|
|
|
2019-01-25 11:04:35 +00:00
|
|
|
if (this.vnToken.token) {
|
|
|
|
return this.loadAcls()
|
|
|
|
.then(() => true)
|
|
|
|
.catch(redirectToLogin);
|
|
|
|
} else
|
|
|
|
return redirectToLogin();
|
2019-01-23 12:11:44 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
login(user, password, remember) {
|
|
|
|
if (!user)
|
|
|
|
throw new UserError('Please insert your user and password');
|
|
|
|
|
|
|
|
let params = {
|
|
|
|
user,
|
|
|
|
password: password || undefined
|
|
|
|
};
|
2019-01-25 11:04:35 +00:00
|
|
|
|
2019-01-23 12:11:44 +00:00
|
|
|
return this.$http.post('/api/Accounts/login', params).then(
|
|
|
|
json => this.onLoginOk(json, remember),
|
2019-01-25 11:04:35 +00:00
|
|
|
json => this.onLoginErr(json));
|
2019-01-23 12:11:44 +00:00
|
|
|
}
|
|
|
|
onLoginOk(json, remember) {
|
|
|
|
this.vnToken.set(json.data.token, remember);
|
|
|
|
|
2019-01-25 11:04:35 +00:00
|
|
|
return this.loadAcls().then(() => {
|
|
|
|
let continueHash = this.$state.params.continue;
|
|
|
|
if (continueHash)
|
|
|
|
this.$window.location = continueHash;
|
|
|
|
else
|
|
|
|
this.$state.go('home');
|
|
|
|
});
|
2019-01-23 12:11:44 +00:00
|
|
|
}
|
|
|
|
onLoginErr(json) {
|
|
|
|
let message;
|
|
|
|
|
|
|
|
switch (json.status) {
|
|
|
|
case 401:
|
|
|
|
message = 'Invalid credentials';
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
message = 'Can\'t contact with server';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
message = 'Something went wrong';
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new UserError(message);
|
|
|
|
}
|
|
|
|
logout() {
|
|
|
|
let promise = this.$http.post('/api/Accounts/logout', null, {
|
|
|
|
headers: {Authorization: this.vnToken.token}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.vnToken.unset();
|
|
|
|
this.loggedIn = false;
|
2019-01-25 11:04:35 +00:00
|
|
|
this.vnModules.reset();
|
|
|
|
this.aclService.reset();
|
2019-01-23 12:11:44 +00:00
|
|
|
this.$state.go('login');
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
}
|
2019-01-25 11:04:35 +00:00
|
|
|
loadAcls() {
|
|
|
|
return this.aclService.load()
|
|
|
|
.then(() => {
|
|
|
|
this.loggedIn = true;
|
|
|
|
this.vnModules.reset();
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
this.vnToken.unset();
|
|
|
|
throw err;
|
|
|
|
});
|
2019-01-23 12:11:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Auth.$inject = ['$http', '$state', '$transitions', '$window', 'vnToken', 'vnModules', 'aclService'];
|
|
|
|
|
|
|
|
ngModule.service('vnAuth', Auth);
|