Add base UI for account management

This commit is contained in:
Dane Everitt 2018-06-11 22:36:43 -07:00
parent e5e66fdb58
commit 14927c3e7e
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
8 changed files with 220 additions and 14 deletions

View file

@ -13,12 +13,20 @@ export default {
* @param state
* @returns {User|null}
*/
currentUser: function (state) {
getUser: function (state) {
return state.user;
}
},
},
setters: {},
actions: {
/**
* Log a user into the Panel.
*
* @param commit
* @param {String} user
* @param {String} password
* @returns {Promise<any>}
*/
login: ({commit}, {user, password}) => {
return new Promise((resolve, reject) => {
window.axios.post(route('auth.login'), {user, password})
@ -47,6 +55,13 @@ export default {
.catch(reject);
});
},
/**
* Log a user out of the Panel.
*
* @param commit
* @returns {Promise<any>}
*/
logout: function ({commit}) {
return new Promise((resolve, reject) => {
window.axios.get(route('auth.logout'))
@ -57,8 +72,39 @@ export default {
.catch(reject);
})
},
/**
* Update a user's email address on the Panel and store the updated result in Vuex.
*
* @param commit
* @param {String} email
* @param {String} password
* @param {String} confirm
* @return {Promise<any>}
*/
updateEmail: function ({commit}, {email, password, confirm}) {
return new Promise((resolve, reject) => {
window.axios.put(route('api.client.account.update-email'), {
email, password, password_confirmation: confirm
})
.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.
if (!(response.data instanceof Object)) {
return reject(new Error('An error was encountered while processing this request.'));
}
commit('setEmail', response.data.email);
return resolve();
})
.catch(reject);
});
},
},
mutations: {
setEmail: function (state, email) {
state.user.email = email;
},
login: function (state, {jwt}) {
localStorage.setItem('token', jwt);
state.user = User.fromToken(jwt);