Created
January 31, 2021 08:56
-
-
Save ennjoy/75f3cb146506392e7b37aadd2e6f831a to your computer and use it in GitHub Desktop.
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
import { apollo, gql } from '/@/apollo' | |
import router from '/@/router' | |
const state = { | |
accessToken: localStorage.getItem('access_token') || null | |
} | |
const getters = {} | |
const actions = { | |
async login ({commit}, payload) { | |
try { | |
const { data: { login } } = await apollo.mutate({ | |
query: gql` | |
mutation Login { | |
login (input: { phone: $phone, password: $password }) { | |
accessToken | |
} | |
} | |
`, | |
variables: { | |
phone: payload.phone, | |
password: payload.password, | |
} | |
}) | |
commit('setToken', login.accessToken) | |
} catch (error) { | |
throw error | |
} | |
} | |
} | |
const mutations = { | |
setToken (state, payload) { | |
localStorage.setItem('access_token', payload) | |
state.accessToken = payload | |
router.push({ name: 'Feed' }) | |
} | |
} | |
export default { | |
namespaced: true, | |
state, | |
getters, | |
actions, | |
mutations, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment