mirror of
				https://github.com/crater-invoice/crater.git
				synced 2025-10-31 05:31:10 -04:00 
			
		
		
		
	init crater
This commit is contained in:
		
							
								
								
									
										77
									
								
								resources/assets/js/store/modules/auth/actions.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								resources/assets/js/store/modules/auth/actions.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,77 @@ | ||||
| import Ls from '@/services/ls' | ||||
| import * as types from './mutation-types' | ||||
| import * as userTypes from '../user/mutation-types' | ||||
| import * as rootTypes from '../../mutation-types' | ||||
| import router from '@/router.js' | ||||
|  | ||||
| export const login = ({ commit, dispatch, state }, data) => { | ||||
|   let loginData = { | ||||
|     username: data.email, | ||||
|     password: data.password | ||||
|   } | ||||
|   return new Promise((resolve, reject) => { | ||||
|     axios.post('/api/auth/login', loginData).then((response) => { | ||||
|       let token = response.data.access_token | ||||
|       Ls.set('auth.token', token) | ||||
|  | ||||
|       commit('user/' + userTypes.RESET_CURRENT_USER, null, { root: true }) | ||||
|       commit(rootTypes.UPDATE_APP_LOADING_STATUS, false, { root: true }) | ||||
|  | ||||
|       commit(types.AUTH_SUCCESS, token) | ||||
|       window.toastr['success']('Login Successful') | ||||
|       resolve(response) | ||||
|     }).catch(err => { | ||||
|       if (err.response.data.error === 'invalid_credentials') { | ||||
|         window.toastr['error']('Invalid Credentials') | ||||
|       } else { | ||||
|         // Something happened in setting up the request that triggered an Error | ||||
|         console.log('Error', err.message) | ||||
|       } | ||||
|  | ||||
|       commit(types.AUTH_ERROR, err.response) | ||||
|       Ls.remove('auth.token') | ||||
|       reject(err) | ||||
|     }) | ||||
|   }) | ||||
| } | ||||
|  | ||||
| export const refreshToken = ({ commit, dispatch, state }) => { | ||||
|   return new Promise((resolve, reject) => { | ||||
|     let data = { | ||||
|       token: Ls.get('auth.token') | ||||
|     } | ||||
|     console.log('REFRESH ACTION') | ||||
|     axios.post('/api/auth/refresh_token', data).then((response) => { | ||||
|       let token = response.data.data.token | ||||
|       Ls.set('auth.token', token) | ||||
|       commit(types.REFRESH_SUCCESS, token) | ||||
|       resolve(response) | ||||
|     }).catch(err => { | ||||
|       reject(err) | ||||
|     }) | ||||
|   }) | ||||
| } | ||||
|  | ||||
| export const logout = ({ commit, dispatch, state }, noRequest = false) => { | ||||
|   if (noRequest) { | ||||
|     commit(types.AUTH_LOGOUT) | ||||
|     Ls.remove('auth.token') | ||||
|     router.push('/login') | ||||
|  | ||||
|     return true | ||||
|   } | ||||
|  | ||||
|   return new Promise((resolve, reject) => { | ||||
|     axios.get('/api/auth/logout').then((response) => { | ||||
|       commit(types.AUTH_LOGOUT) | ||||
|       Ls.remove('auth.token') | ||||
|       router.push('/login') | ||||
|       window.toastr['success']('Logged out!', 'Success') | ||||
|     }).catch(err => { | ||||
|       reject(err) | ||||
|       commit(types.AUTH_LOGOUT) | ||||
|       Ls.remove('auth.token') | ||||
|       router.push('/login') | ||||
|     }) | ||||
|   }) | ||||
| } | ||||
							
								
								
									
										2
									
								
								resources/assets/js/store/modules/auth/getters.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								resources/assets/js/store/modules/auth/getters.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,2 @@ | ||||
| export const isAuthenticated = (state) => !!state.token | ||||
| export const authStatus = (state) => state.status | ||||
							
								
								
									
										23
									
								
								resources/assets/js/store/modules/auth/index.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								resources/assets/js/store/modules/auth/index.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,23 @@ | ||||
| import mutations from './mutations' | ||||
| import * as actions from './actions' | ||||
| import * as getters from './getters' | ||||
| import Ls from '@/services/ls' | ||||
|  | ||||
| const initialState = { | ||||
|   token: Ls.get('auth.token'), | ||||
|   status: '', | ||||
|   validateTokenError: '', | ||||
|   validateTokenSuccess: '' | ||||
| } | ||||
|  | ||||
| export default { | ||||
|   namespaced: true, | ||||
|  | ||||
|   state: initialState, | ||||
|  | ||||
|   getters: getters, | ||||
|  | ||||
|   actions: actions, | ||||
|  | ||||
|   mutations: mutations | ||||
| } | ||||
							
								
								
									
										4
									
								
								resources/assets/js/store/modules/auth/mutation-types.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								resources/assets/js/store/modules/auth/mutation-types.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,4 @@ | ||||
| export const AUTH_SUCCESS = 'AUTH_SUCCESS' | ||||
| export const AUTH_LOGOUT = 'AUTH_LOGOUT' | ||||
| export const AUTH_ERROR = 'AUTH_ERROR' | ||||
| export const REFRESH_SUCCESS = 'REFRESH_SUCCESS' | ||||
							
								
								
									
										22
									
								
								resources/assets/js/store/modules/auth/mutations.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								resources/assets/js/store/modules/auth/mutations.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,22 @@ | ||||
| import * as types from './mutation-types' | ||||
|  | ||||
| export default { | ||||
|   [types.AUTH_SUCCESS] (state, token) { | ||||
|     state.token = token | ||||
|     state.status = 'success' | ||||
|   }, | ||||
|  | ||||
|   [types.AUTH_LOGOUT] (state) { | ||||
|     state.token = null | ||||
|   }, | ||||
|  | ||||
|   [types.AUTH_ERROR] (state, errorResponse) { | ||||
|     state.token = null | ||||
|     state.status = 'error' | ||||
|   }, | ||||
|  | ||||
|   [types.REFRESH_SUCCESS] (state, token) { | ||||
|     state.token = token | ||||
|     state.status = 'success' | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user