mirror of
https://git.meshkee.com/cssd/cssd-back.git
synced 2026-10-13 21:31:07 +03:30
init project
This commit is contained in:
+53
@@ -0,0 +1,53 @@
|
||||
import { UserService, AuthenticationError } from '@/services/auth/user.service'
|
||||
import router from '@/router'
|
||||
|
||||
export default {
|
||||
async login({ commit }, {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 || '/');
|
||||
|
||||
return true
|
||||
} catch (e) {
|
||||
if (e instanceof AuthenticationError) {
|
||||
commit('loginError', {errorCode: e.errorCode, errorMessage: e.message})
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
},
|
||||
|
||||
logout({ commit }) {
|
||||
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
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
export default {
|
||||
loggedIn: (state) => {
|
||||
return state.accessToken ? true : false
|
||||
},
|
||||
|
||||
authenticationErrorCode: (state) => {
|
||||
return state.authenticationErrorCode
|
||||
},
|
||||
|
||||
authenticationError: (state) => {
|
||||
return state.authenticationError
|
||||
},
|
||||
|
||||
authenticating: (state) => {
|
||||
return state.authenticating
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
export default {
|
||||
loginRequest(state) {
|
||||
state.authenticating = true;
|
||||
state.authenticationError = ''
|
||||
state.authenticationErrorCode = 0
|
||||
},
|
||||
|
||||
loginSuccess(state, accessToken) {
|
||||
state.accessToken = accessToken
|
||||
state.authenticating = false;
|
||||
},
|
||||
|
||||
loginError(state, {errorCode, errorMessage}) {
|
||||
state.authenticating = false
|
||||
state.authenticationErrorCode = errorCode
|
||||
state.authenticationError = errorMessage
|
||||
},
|
||||
|
||||
logoutSuccess(state) {
|
||||
state.accessToken = ''
|
||||
},
|
||||
|
||||
|
||||
refreshTokenPromise(state, promise) {
|
||||
state.refreshTokenPromise = promise
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { TokenService } from '@/services/auth/storage.service'
|
||||
|
||||
export default {
|
||||
authenticating: false,
|
||||
accessToken: TokenService.getToken(),
|
||||
authenticationErrorCode: 0,
|
||||
authenticationError: '',
|
||||
refreshTokenPromise: null // Holds the promise of the refresh token
|
||||
}
|
||||
Reference in New Issue
Block a user