@@ -13,12 +13,20 @@ export default {
1313 * @param state
1414 * @returns {User|null }
1515 */
16- currentUser : function ( state ) {
16+ getUser : function ( state ) {
1717 return state . user ;
18- }
18+ } ,
1919 } ,
2020 setters : { } ,
2121 actions : {
22+ /**
23+ * Log a user into the Panel.
24+ *
25+ * @param commit
26+ * @param {String } user
27+ * @param {String } password
28+ * @returns {Promise<any> }
29+ */
2230 login : ( { commit} , { user, password} ) => {
2331 return new Promise ( ( resolve , reject ) => {
2432 window . axios . post ( route ( 'auth.login' ) , { user, password} )
@@ -47,6 +55,13 @@ export default {
4755 . catch ( reject ) ;
4856 } ) ;
4957 } ,
58+
59+ /**
60+ * Log a user out of the Panel.
61+ *
62+ * @param commit
63+ * @returns {Promise<any> }
64+ */
5065 logout : function ( { commit} ) {
5166 return new Promise ( ( resolve , reject ) => {
5267 window . axios . get ( route ( 'auth.logout' ) )
@@ -57,8 +72,39 @@ export default {
5772 . catch ( reject ) ;
5873 } )
5974 } ,
75+
76+ /**
77+ * Update a user's email address on the Panel and store the updated result in Vuex.
78+ *
79+ * @param commit
80+ * @param {String } email
81+ * @param {String } password
82+ * @param {String } confirm
83+ * @return {Promise<any> }
84+ */
85+ updateEmail : function ( { commit} , { email, password, confirm} ) {
86+ return new Promise ( ( resolve , reject ) => {
87+ window . axios . put ( route ( 'api.client.account.update-email' ) , {
88+ email, password, password_confirmation : confirm
89+ } )
90+ . then ( response => {
91+ // If there is a 302 redirect or some other odd behavior (basically, response that isnt
92+ // in JSON format) throw an error and don't try to continue with the login.
93+ if ( ! ( response . data instanceof Object ) ) {
94+ return reject ( new Error ( 'An error was encountered while processing this request.' ) ) ;
95+ }
96+
97+ commit ( 'setEmail' , response . data . email ) ;
98+ return resolve ( ) ;
99+ } )
100+ . catch ( reject ) ;
101+ } ) ;
102+ } ,
60103 } ,
61104 mutations : {
105+ setEmail : function ( state , email ) {
106+ state . user . email = email ;
107+ } ,
62108 login : function ( state , { jwt} ) {
63109 localStorage . setItem ( 'token' , jwt ) ;
64110 state . user = User . fromToken ( jwt ) ;
0 commit comments