2018-12-16 23:29:44 +00:00
|
|
|
import User, {UserData} from '../../models/user';
|
|
|
|
import {ActionContext} from "vuex";
|
2018-12-17 02:57:34 +00:00
|
|
|
import {AuthenticationState} from "../types";
|
2018-06-16 21:30:20 +00:00
|
|
|
|
2018-06-06 06:00:01 +00:00
|
|
|
const route = require('./../../../../../vendor/tightenco/ziggy/src/js/route').default;
|
|
|
|
|
2018-12-16 23:29:44 +00:00
|
|
|
type LoginAction = {
|
|
|
|
user: string,
|
|
|
|
password: string,
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateEmailAction = {
|
|
|
|
email: string,
|
|
|
|
password: string,
|
|
|
|
}
|
|
|
|
|
2018-07-15 23:57:00 +00:00
|
|
|
export default {
|
2018-06-06 06:00:01 +00:00
|
|
|
namespaced: true,
|
|
|
|
state: {
|
2018-12-16 23:29:44 +00:00
|
|
|
// @ts-ignore
|
2018-07-15 05:42:58 +00:00
|
|
|
user: typeof window.PterodactylUser === 'object' ? new User(window.PterodactylUser) : null,
|
2018-06-06 06:00:01 +00:00
|
|
|
},
|
|
|
|
getters: {
|
|
|
|
/**
|
|
|
|
* Return the currently authenticated user.
|
|
|
|
*/
|
2018-12-16 23:29:44 +00:00
|
|
|
getUser: function (state: AuthenticationState): null | User {
|
2018-06-06 06:00:01 +00:00
|
|
|
return state.user;
|
2018-06-12 05:36:43 +00:00
|
|
|
},
|
2018-06-06 06:00:01 +00:00
|
|
|
},
|
|
|
|
setters: {},
|
|
|
|
actions: {
|
2018-06-12 05:36:43 +00:00
|
|
|
/**
|
|
|
|
* Log a user into the Panel.
|
|
|
|
*/
|
2018-12-16 23:29:44 +00:00
|
|
|
login: ({commit}: ActionContext<AuthenticationState, any>, {user, password}: LoginAction): Promise<{
|
|
|
|
complete: boolean,
|
|
|
|
intended?: string,
|
|
|
|
token?: string,
|
|
|
|
}> => {
|
2018-06-06 06:00:01 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-12-16 23:29:44 +00:00
|
|
|
// @ts-ignore
|
2018-06-06 06:00:01 +00:00
|
|
|
window.axios.post(route('auth.login'), {user, password})
|
2018-12-16 23:29:44 +00:00
|
|
|
// @ts-ignore
|
2018-06-06 06:00:01 +00:00
|
|
|
.then(response => {
|
|
|
|
commit('logout');
|
|
|
|
|
|
|
|
// If there is a 302 redirect or some other odd behavior (basically, response that isnt
|
|
|
|
// in JSON format) throw an error and don't try to continue with the login.
|
|
|
|
if (!(response.data instanceof Object)) {
|
|
|
|
return reject(new Error('An error was encountered while processing this request.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.data.complete) {
|
2018-07-15 05:42:58 +00:00
|
|
|
commit('login', response.data.user);
|
2018-06-06 06:00:01 +00:00
|
|
|
return resolve({
|
|
|
|
complete: true,
|
|
|
|
intended: response.data.intended,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolve({
|
|
|
|
complete: false,
|
|
|
|
token: response.data.login_token,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
},
|
2018-06-12 05:36:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a user's email address on the Panel and store the updated result in Vuex.
|
|
|
|
*/
|
2018-12-16 23:29:44 +00:00
|
|
|
updateEmail: function ({commit}: ActionContext<AuthenticationState, any>, {email, password}: UpdateEmailAction): Promise<void> {
|
2018-06-06 06:00:01 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-12-16 23:29:44 +00:00
|
|
|
// @ts-ignore
|
2018-06-16 21:30:20 +00:00
|
|
|
window.axios.put(route('api.client.account.update-email'), {email, password})
|
2018-12-16 23:29:44 +00:00
|
|
|
// @ts-ignore
|
2018-06-12 05:36:43 +00:00
|
|
|
.then(response => {
|
|
|
|
// If there is a 302 redirect or some other odd behavior (basically, response that isnt
|
|
|
|
// in JSON format) throw an error and don't try to continue with the login.
|
2018-07-15 05:03:19 +00:00
|
|
|
if (!(response.data instanceof Object) && response.status !== 201) {
|
2018-06-12 05:36:43 +00:00
|
|
|
return reject(new Error('An error was encountered while processing this request.'));
|
|
|
|
}
|
|
|
|
|
2018-07-15 05:03:19 +00:00
|
|
|
commit('setEmail', email);
|
2018-06-06 06:00:01 +00:00
|
|
|
return resolve();
|
|
|
|
})
|
|
|
|
.catch(reject);
|
2018-06-12 05:36:43 +00:00
|
|
|
});
|
2018-06-06 06:00:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
mutations: {
|
2018-12-16 23:29:44 +00:00
|
|
|
setEmail: function (state: AuthenticationState, email: string) {
|
|
|
|
if (state.user) {
|
|
|
|
state.user.email = email;
|
|
|
|
}
|
2018-06-12 05:36:43 +00:00
|
|
|
},
|
2018-12-16 23:29:44 +00:00
|
|
|
login: function (state: AuthenticationState, data: UserData) {
|
2018-07-15 05:42:58 +00:00
|
|
|
state.user = new User(data);
|
2018-06-06 06:00:01 +00:00
|
|
|
},
|
2018-12-16 23:29:44 +00:00
|
|
|
logout: function (state: AuthenticationState) {
|
2018-06-06 06:00:01 +00:00
|
|
|
state.user = null;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|