Files
mixtape/zero.Web.UI/App/services/auth.js
T

111 lines
2.4 KiB
JavaScript
Raw Normal View History

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