Skip to content

Instantly share code, notes, and snippets.

@ndarilek
Created June 20, 2017 14:19
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 ndarilek/1e3f1c165d3b405c09061ad2dc32bc84 to your computer and use it in GitHub Desktop.
Save ndarilek/1e3f1c165d3b405c09061ad2dc32bc84 to your computer and use it in GitHub Desktop.
// Resolver for login mutation
async logIn(root, {emailOrUsername, password}) {
const user = await User.authenticate(emailOrUsername, password)
if(user)
return {
token: buildMacaroon(user).serialize(),
user
}
else if(user == false)
throw new Error("Incorrect password")
else
throw new Error("User not found")
}
// Mutation call using vue-apollo
async submit(e) {
const success = await this.$validator.validateAll()
if(!success)
return
try {
const {data} = await this.$apollo.mutate({
mutation: gql`mutation($emailOrUsername: String!, $password: String!) {
logIn(
emailOrUsername: $emailOrUsername,
password: $password
) {
token
user {
username
}
}
}`,
variables: {
emailOrUsername: this.emailOrUsername,
password: this.password
}
})
if(data.logIn) {
this.setToken(data.logIn.token)
this.setUsername(data.logIn.user.username)
this.$apollo.provider.defaultClient.resetStore()
this.$router.push({name: "index"})
}
} catch(e) {
console.error("got an error", e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment