Advertisement
Guest User

Untitled

a guest
Jun 20th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. // Resolver for login mutation
  2. async logIn(root, {emailOrUsername, password}) {
  3. const user = await User.authenticate(emailOrUsername, password)
  4. if(user)
  5. return {
  6. token: buildMacaroon(user).serialize(),
  7. user
  8. }
  9. else if(user == false)
  10. throw new Error("Incorrect password")
  11. else
  12. throw new Error("User not found")
  13. }
  14.  
  15. // Mutation call using vue-apollo
  16. async submit(e) {
  17. const success = await this.$validator.validateAll()
  18. if(!success)
  19. return
  20. try {
  21. const {data} = await this.$apollo.mutate({
  22. mutation: gql`mutation($emailOrUsername: String!, $password: String!) {
  23. logIn(
  24. emailOrUsername: $emailOrUsername,
  25. password: $password
  26. ) {
  27. token
  28. user {
  29. username
  30. }
  31. }
  32. }`,
  33. variables: {
  34. emailOrUsername: this.emailOrUsername,
  35. password: this.password
  36. }
  37. })
  38. if(data.logIn) {
  39. this.setToken(data.logIn.token)
  40. this.setUsername(data.logIn.user.username)
  41. this.$apollo.provider.defaultClient.resetStore()
  42. this.$router.push({name: "index"})
  43. }
  44. } catch(e) {
  45. console.error("got an error", e)
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement