mirror of
https://git.meshkee.com/cssd/cssd-back.git
synced 2026-10-12 17:51:06 +03:30
64 lines
2.1 KiB
JavaScript
Vendored
64 lines
2.1 KiB
JavaScript
Vendored
import { UserService, AuthenticationError } from '@/services/auth/user.service'
|
|
import router from '@/router'
|
|
|
|
export default {
|
|
async login({ commit, dispatch }, {cell_number, password}) {
|
|
commit('loginRequest');
|
|
try {
|
|
const token = await UserService.login(cell_number, password);
|
|
commit('loginSuccess', token);
|
|
// Redirect the user to the page he first tried to visit or to the home view
|
|
router.push(router.history.current.query.redirect || '/');
|
|
|
|
dispatch('info', {}, {root : true});
|
|
|
|
return true
|
|
} catch (e) {
|
|
if (e instanceof AuthenticationError) {
|
|
commit('loginError', {errorCode: e.errorCode, errorMessage: e.message})
|
|
}
|
|
|
|
return false
|
|
}
|
|
},
|
|
|
|
async forgotPassword({ commit }, {cell_number}) {
|
|
const result = await UserService.forgotPassword(cell_number);
|
|
// TODO: Open Modal For Get Section
|
|
},
|
|
|
|
async verifyCodeSMS({ commit }, {verifyCode}) {
|
|
const result = await UserService.verifyCode(verifyCode);
|
|
// TODO: Open Modal For Get Section
|
|
},
|
|
|
|
async logout({ commit }) {
|
|
const result = await UserService.logout();
|
|
commit('logoutSuccess')
|
|
router.push('/login')
|
|
},
|
|
|
|
refreshToken({ commit, state }) {
|
|
// If this is the first time the refreshToken has been called, make a request
|
|
// otherwise return the same promise to the caller
|
|
if(!state.refreshTokenPromise) {
|
|
const p = UserService.refreshToken()
|
|
commit('refreshTokenPromise', p)
|
|
|
|
// Wait for the UserService.refreshToken() to resolve. On success set the token and clear promise
|
|
// Clear the promise on error as well.
|
|
p.then(
|
|
response => {
|
|
commit('refreshTokenPromise', null)
|
|
commit('loginSuccess', response)
|
|
},
|
|
error => {
|
|
commit('refreshTokenPromise', null)
|
|
}
|
|
)
|
|
}
|
|
|
|
return state.refreshTokenPromise
|
|
}
|
|
}
|