Skip to content

Instantly share code, notes, and snippets.

@aldarund
Created Apr 3, 2018
Embed
What would you like to do?
import TOKEN_AUTH from '~/graphql/mutation/tokenAuth.gql'
import GET_LOGGED_USER from '~/graphql/query/getLoggedUser.gql'
export default class ApolloScheme {
constructor (auth, options) {
this.auth = auth
this.name = options._name
this.client = this.auth.ctx.app.apolloProvider.clients.defaultClient
this.options = Object.assign({}, options)
}
async login (endpoint) {
await this.logout()
const {
data: { tokenAuth: { token } }
} = await this.client.mutate({
mutation: TOKEN_AUTH,
variables: {
email: endpoint.data.email,
password: endpoint.data.password
}
})
this.auth.setToken(this.name, token)
return this.fetchUser()
}
async fetchUser (endpoint) {
if (!this.auth.getToken('apollo')) {
return
}
try {
const { data } = await this.client.query({ query: GET_LOGGED_USER })
this.auth.setUser(data)
} catch (e) {
console.log(e)
}
}
async logout (endpoint) {
return this.auth.reset()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment