Created
July 23, 2021 10:42
-
-
Save yoovanr/cd60b33cf6f701293d88a280707eae90 to your computer and use it in GitHub Desktop.
[Vue] store/oauth.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const state = () => ({ | |
signinLoading: false, | |
logoutLoading: false, | |
}) | |
export const getters = { | |
signinLoading: (state) => state.signinLoading, | |
logoutLoading: (state) => state.logoutLoading, | |
} | |
export const mutations = { | |
'SIGNIN_REQUEST' (state) { | |
state.signinLoading = true | |
}, | |
'SIGNIN_SUCCESS' (state) { | |
state.signinLoading = false | |
}, | |
'SIGNIN_FAILURE' (state) { | |
state.signinLoading = false | |
}, | |
'LOGOUT_REQUEST' (state) { | |
state.logoutLoading = true | |
}, | |
'LOGOUT_SUCCESS' (state) { | |
state.logoutLoading = false | |
}, | |
'LOGOUT_FAILURE' (state) { | |
state.logoutLoading = false | |
} | |
} | |
export const actions = { | |
async signin ({ commit, dispatch }, data) { | |
commit('SIGNIN_REQUEST') | |
try { | |
const params = { | |
username: data.email, | |
password: data.password, | |
} | |
const loginData = await this.$axios.$post('oauth', params) | |
this.$cookies.set('access_token', loginData.access_token) | |
this.$cookies.set('refresh_token', loginData.refresh_token) | |
dispatch('session/updateCurrentAccessToken', loginData.access_token, { root: true }) | |
dispatch('session/updateCurrentRefreshToken', loginData.refresh_token, { root: true }) | |
const userData = await this.$axios.$get('/api/users/me') | |
dispatch('session/updateCurrentUser', userData, { root: true }) | |
this.$router.push('dashboard') | |
commit('SIGNIN_SUCCESS') | |
} catch (err) { | |
commit('SIGNIN_FAILURE') | |
} | |
}, | |
async logout ({ commit, dispatch }) { | |
commit('LOGOUT_REQUEST') | |
try { | |
await this.$axios.$post('api/users/logout') | |
this.$cookies.remove('access_token') | |
this.$cookies.remove('refresh_token') | |
dispatch('session/updateCurrentUser', null, { root: true }) | |
this.$router.push('sign-in') | |
commit('LOGOUT_SUCCESS') | |
} catch (err) { | |
commit('LOGOUT_FAILURE') | |
} | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment