Guest User

Untitled

a guest
Oct 22nd, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. import axios from 'axios';
  2. import router from '../router';
  3. import notificator from '../services/notificator';
  4.  
  5. const generateEmptyUser = () => ({
  6. id: '',
  7. name: '',
  8. email: '',
  9. });
  10.  
  11. export default {
  12. namespaced: true,
  13. state: {
  14. user: generateEmptyUser(),
  15. tokenRenewalIntervalHandle: null,
  16. },
  17. getters: {
  18. getUser: (state) => state.user,
  19. isAuthenticated: (state) => !!state.user.id,
  20. },
  21. mutations: {
  22. setUser: (state, userData) => state.user = userData,
  23. },
  24. actions: {
  25. updateProfile: async ({dispatch}, profileData) => {
  26. try {
  27. const {data} = await axios.put('/user/profile', profileData);
  28.  
  29. await dispatch('setToken', data.token);
  30. await dispatch('fetchUser');
  31.  
  32. router.push({name: 'home'});
  33.  
  34. notificator.success('Your profile has been updated!');
  35. } catch (e) {
  36. console.log({...e});
  37.  
  38. notificator.error('Could not update your profile.');
  39. }
  40. },
  41. getPasswordResetToken: async (ctx, email) => {
  42. try {
  43. await axios.post('/user/forgot-password', {email});
  44.  
  45. router.push({name: 'userLogin'});
  46.  
  47. notificator.success('Your password reset link has been sent to your email!');
  48. } catch (e) {
  49. console.log({...e});
  50.  
  51. notificator.error('Could not reset your password.');
  52. }
  53. },
  54. resetPassword: async (ctx, payload) => {
  55. try {
  56. await axios.put(
  57. '/user/reset-password',
  58. {newPassword: payload.newPassword},
  59. {headers: {Authorization: 'Bearer ' + payload.token}}
  60. );
  61.  
  62. router.push({name: 'userLogin'});
  63.  
  64. notificator.success('Your password has been reset successfully!');
  65. } catch (e) {
  66. console.log({...e});
  67.  
  68. notificator.error('Could not reset your password.');
  69. }
  70. },
  71. logOut: ({dispatch}) => {
  72. dispatch('unsetToken');
  73.  
  74. router.push({name: 'userLogin'});
  75. },
  76. changePassword: async ({dispatch, state}, changePasswordData) => {
  77. try {
  78. await axios.put('/user/change-password', changePasswordData);
  79.  
  80. router.push({name: 'home'});
  81.  
  82. notificator.success('Your password has been successfully changed!');
  83. } catch (e) {
  84. console.log({...e});
  85.  
  86. notificator.error('Could not change your password.');
  87. }
  88. },
  89. register: async ({dispatch, state}, registerData) => {
  90. try {
  91. const {data} = await axios.post('/user/register', registerData);
  92.  
  93. dispatch('setToken', data.token);
  94. await dispatch('fetchUser');
  95. } catch (e) {
  96. console.log({...e});
  97.  
  98. notificator.error('There was an error when registering your account.');
  99. }
  100. },
  101. logIn: async ({dispatch, state}, loginData) => {
  102. try {
  103. const {data} = await axios.post('/user/login', loginData);
  104.  
  105. dispatch('setToken', data.token);
  106. await dispatch('fetchUser');
  107. } catch (e) {
  108. console.log({...e});
  109.  
  110. notificator.error('Specified credentials are invalid.');
  111. }
  112. },
  113. tryAutoLogin: async ({dispatch, getters}) => {
  114. const token = localStorage.getItem('token');
  115.  
  116. if (token && !getters.isAuthenticated) {
  117. // token needs to be set
  118. // for renewToken to work
  119. dispatch('setToken', token);
  120.  
  121. await dispatch('renewToken');
  122. await dispatch('fetchUser');
  123. }
  124. },
  125. renewToken: async ({dispatch}) => {
  126. try {
  127. const {data} = await axios.get('/user/renew-token');
  128.  
  129. await dispatch('setToken', data.token);
  130. } catch (e) {
  131. console.log({...e});
  132.  
  133. dispatch('unsetToken');
  134.  
  135. router.push({name: 'userLogin'});
  136. }
  137. },
  138. fetchUser: async ({commit}) => {
  139. try {
  140. const {data} = await axios.get('/user/authenticate');
  141.  
  142. commit('setUser', data.user);
  143. } catch (e) {
  144. console.log({...e});
  145.  
  146. router.push({name: 'userLogin'});
  147. }
  148. },
  149. setToken: ({state, dispatch}, token) => {
  150. localStorage.setItem('token', token);
  151.  
  152. axios.defaults.headers.common['authorization'] = `Bearer ${token}`;
  153.  
  154. if (state.tokenRenewalIntervalHandle) return;
  155.  
  156. state.tokenRenewalIntervalHandle = setInterval(() => dispatch('renewToken'), 1000 * 60 * 10);
  157. },
  158. unsetToken: ({state, commit}) => {
  159. commit('setUser', generateEmptyUser());
  160.  
  161. localStorage.removeItem('token');
  162.  
  163. delete axios.defaults.headers.common['authorization'];
  164.  
  165. clearInterval(state.tokenRenewalIntervalHandle);
  166. state.tokenRenewalIntervalHandle = null;
  167. },
  168. },
  169. };
Add Comment
Please, Sign In to add comment