Client - handle error on api calls
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
import { ActionContext, ActionTree } from 'vuex'
|
||||
import { USER_STORE } from '@/store/constants'
|
||||
import { IUserActions } from '@/store/modules/user/interfaces'
|
||||
import { IUserState } from '@/store/modules/user/interfaces'
|
||||
import { ROOT_STORE, USER_STORE } from '@/store/constants'
|
||||
import {
|
||||
ILoginOrRegisterData,
|
||||
IUserActions,
|
||||
IUserState,
|
||||
} from '@/store/modules/user/interfaces'
|
||||
import { IRootState } from '@/store/modules/root/interfaces'
|
||||
import { ILoginOrRegisterData } from '@/store/modules/user/interfaces'
|
||||
import { handleError } from '@/utils'
|
||||
import api from '@/api/defaultApi'
|
||||
import authApi from '@/api/authApi'
|
||||
import router from '@/router'
|
||||
@@ -12,6 +15,7 @@ export const actions: ActionTree<IUserState, IRootState> & IUserActions = {
|
||||
[USER_STORE.ACTIONS.GET_USER_PROFILE](
|
||||
context: ActionContext<IUserState, IRootState>
|
||||
): void {
|
||||
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGE)
|
||||
authApi
|
||||
.get('auth/profile')
|
||||
.then((res) => {
|
||||
@@ -20,26 +24,31 @@ export const actions: ActionTree<IUserState, IRootState> & IUserActions = {
|
||||
USER_STORE.MUTATIONS.UPDATE_AUTH_USER_PROFILE,
|
||||
res.data.data
|
||||
)
|
||||
} else {
|
||||
handleError(context, null)
|
||||
}
|
||||
})
|
||||
.catch((err) => console.log(err))
|
||||
.catch((error) => handleError(context, error))
|
||||
},
|
||||
[USER_STORE.ACTIONS.LOGIN_OR_REGISTER](
|
||||
context: ActionContext<IUserState, IRootState>,
|
||||
data: ILoginOrRegisterData
|
||||
): void {
|
||||
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGE)
|
||||
api
|
||||
.post(`/auth/${data.actionType}`, data.formData)
|
||||
.then((res) => {
|
||||
if (res.status == 200 && res.data.status === 'success') {
|
||||
if (res.data.status === 'success') {
|
||||
const token = res.data.auth_token
|
||||
window.localStorage.setItem('authToken', token)
|
||||
context.commit(USER_STORE.MUTATIONS.UPDATE_AUTH_TOKEN, token)
|
||||
context
|
||||
.dispatch(USER_STORE.ACTIONS.GET_USER_PROFILE)
|
||||
.then(() => router.push('/'))
|
||||
} else {
|
||||
handleError(context, null)
|
||||
}
|
||||
})
|
||||
.catch((err) => console.log(err))
|
||||
.catch((error) => handleError(context, error))
|
||||
},
|
||||
}
|
||||
|
@@ -37,7 +37,7 @@ export interface IUserState {
|
||||
}
|
||||
|
||||
export interface IUserGetters {
|
||||
[USER_STORE.GETTERS.AUTH_TOKEN](state: IUserState): string
|
||||
[USER_STORE.GETTERS.AUTH_TOKEN](state: IUserState): string | null
|
||||
[USER_STORE.GETTERS.IS_AUTHENTICATED](state: IUserState): boolean
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user