2020-10-15 14:10:38 +02:00
|
|
|
import Vue from 'vue';
|
2020-11-20 10:32:17 +01:00
|
|
|
import Overlay from 'zero/helpers/overlay.js';
|
2020-10-15 14:10:38 +02:00
|
|
|
import { find as _find, extend as _extend } from 'underscore';
|
2020-04-15 15:13:38 +02:00
|
|
|
import Axios from 'axios';
|
2020-12-30 13:21:35 +01:00
|
|
|
//import PasswordChangeOverlay from 'zero/pages/password-change.vue';
|
2020-04-09 10:38:27 +02:00
|
|
|
|
2020-10-15 14:10:38 +02:00
|
|
|
export default new Vue({
|
2020-04-10 13:35:39 +02:00
|
|
|
|
2020-10-15 14:10:38 +02:00
|
|
|
data: () => ({
|
|
|
|
|
isAuthenticated: false,
|
|
|
|
|
rejectReason: null,
|
|
|
|
|
user: {
|
|
|
|
|
name: null,
|
|
|
|
|
email: null
|
2020-04-09 10:38:27 +02:00
|
|
|
}
|
2020-10-15 14:10:38 +02:00
|
|
|
}),
|
2020-10-14 01:55:51 +02:00
|
|
|
|
2020-10-15 14:10:38 +02:00
|
|
|
watch: {
|
|
|
|
|
isAuthenticated(value)
|
2020-10-14 01:55:51 +02:00
|
|
|
{
|
2020-10-15 14:10:38 +02:00
|
|
|
this.$emit('authenticated', value);
|
|
|
|
|
},
|
|
|
|
|
user(value)
|
|
|
|
|
{
|
|
|
|
|
this.$emit('user', value);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
|
|
|
|
|
|
// loads the current user into the cache
|
|
|
|
|
loadUser()
|
|
|
|
|
{
|
|
|
|
|
Axios.get('authentication/getUser').then(res =>
|
2020-10-14 01:55:51 +02:00
|
|
|
{
|
2020-10-15 14:10:38 +02:00
|
|
|
this.isAuthenticated = res.data.success && res.data.model;
|
|
|
|
|
|
|
|
|
|
if (res.data.success)
|
|
|
|
|
{
|
|
|
|
|
this.user = res.data.model;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// the cached user has been rejected by the server so we clear credentials here
|
|
|
|
|
rejectUser(reason)
|
|
|
|
|
{
|
|
|
|
|
this.rejectReason = reason;
|
|
|
|
|
this.isAuthenticated = false;
|
|
|
|
|
this.user = null;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// sets the current user and isAuthenticated to true
|
|
|
|
|
setUser(user)
|
|
|
|
|
{
|
|
|
|
|
if (!user)
|
2020-10-14 01:55:51 +02:00
|
|
|
{
|
2020-10-15 14:10:38 +02:00
|
|
|
this.rejectUser();
|
|
|
|
|
return;
|
2020-10-14 01:55:51 +02:00
|
|
|
}
|
2020-10-15 14:10:38 +02:00
|
|
|
this.isAuthenticated = true;
|
|
|
|
|
this.user = user;
|
|
|
|
|
},
|
2020-10-14 01:55:51 +02:00
|
|
|
|
2020-10-15 14:10:38 +02:00
|
|
|
// logs the user in with the passed credentials
|
|
|
|
|
login(model)
|
|
|
|
|
{
|
|
|
|
|
return Axios.post('authentication/loginUser', model).then(res =>
|
|
|
|
|
{
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
|
{
|
|
|
|
|
if (res.data.success)
|
|
|
|
|
{
|
|
|
|
|
this.setUser(res.data.model);
|
|
|
|
|
resolve(res.data);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
this.rejectUser();
|
|
|
|
|
reject(res.data.errors);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
},
|
2020-10-14 01:55:51 +02:00
|
|
|
|
2020-10-15 14:10:38 +02:00
|
|
|
// logs the current user out
|
|
|
|
|
logout()
|
|
|
|
|
{
|
|
|
|
|
let promise = Axios.post('authentication/logoutUser');
|
|
|
|
|
this.rejectUser("@login.rejectReasons.logout");
|
|
|
|
|
return promise;
|
|
|
|
|
},
|
2020-10-14 01:55:51 +02:00
|
|
|
|
2020-10-15 14:10:38 +02:00
|
|
|
// try to switch selected application for user
|
|
|
|
|
switchApp(appId)
|
|
|
|
|
{
|
2021-03-04 16:03:37 +01:00
|
|
|
return Axios.post('authentication/switchApp', null, { params: { appId } }).then(res =>
|
|
|
|
|
{
|
2021-03-05 15:35:55 +01:00
|
|
|
//return this.zero.reloadConfig().then(() =>
|
|
|
|
|
//{
|
|
|
|
|
this.$emit('appswitch', res.data);
|
|
|
|
|
return Promise.resolve(res.data.success);
|
|
|
|
|
//});
|
2021-03-04 16:03:37 +01:00
|
|
|
});
|
2020-10-15 14:10:38 +02:00
|
|
|
},
|
2020-10-14 01:55:51 +02:00
|
|
|
|
2020-10-15 14:10:38 +02:00
|
|
|
// open overlay to update password
|
|
|
|
|
openPasswordOverlay()
|
|
|
|
|
{
|
|
|
|
|
return Overlay.open({
|
|
|
|
|
title: '@changepasswordoverlay.title',
|
|
|
|
|
closeLabel: '@ui.close',
|
|
|
|
|
confirmLabel: '@changepasswordoverlay.confirm',
|
|
|
|
|
component: PasswordChangeOverlay
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|