Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@aldarund
Created April 3, 2018 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aldarund/e6f030287057dc056732b1875eb11297 to your computer and use it in GitHub Desktop.
Save aldarund/e6f030287057dc056732b1875eb11297 to your computer and use it in GitHub Desktop.
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