Created
June 20, 2017 14:19
-
-
Save ndarilek/1e3f1c165d3b405c09061ad2dc32bc84 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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