Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2021
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # ~/plugins/apollo-schema.js
  2.  
  3. import { login as loginMutation, me as meMutation } from '~/apollo/auth/login'
  4.  
  5. export default class ApolloScheme {
  6.   constructor(auth, options) {
  7.     this.$auth = auth
  8.     this.$apollo = auth.ctx.app.apolloProvider.defaultClient
  9.     this.$apolloHelpers = auth.ctx.app.$apolloHelpers
  10.     this.options = options
  11.   }
  12.  
  13.   async login({ credentials }) {
  14.     try {
  15.       const user = await this.loginUser(credentials)
  16.  
  17.       if (user) {
  18.         this.setUser(user)
  19.       }
  20.     } catch (error) {}
  21.   }
  22.  
  23.   async loginUser(credentials) {
  24.     try {
  25.       const { data } = await this.$apollo.mutate({
  26.         mutation: loginMutation,
  27.         variables: credentials,
  28.       })
  29.  
  30.       return Promise.resolve(data.login)
  31.     } catch (error) {
  32.       this.logout()
  33.       return Promise.reject(error)
  34.     }
  35.   }
  36.  
  37.   setUser(user) {
  38.     this.$auth.setUser(user)
  39.   }
  40.  
  41.   async fetchUser() {
  42.     try {
  43.       const { data } = await this.$apollo.mutate({
  44.         mutation: meMutation,
  45.       })
  46.  
  47.       this.setUser(data.me)
  48.     } catch (error) {
  49.       this.logout()
  50.       return Promise.reject(error)
  51.     }
  52.   }
  53.  
  54.   logout() {
  55.     this.$auth.logout()
  56.   }
  57. }
  58.  
  59. # nuxt.config.js
  60.  
  61. auth: {
  62.     redirect: {
  63.       logout: '/auth/login',
  64.       login: '/auth/login',
  65.       callback: '/auth/login',
  66.       home: '/',
  67.     },
  68.  
  69.     strategies: {
  70.       apollo: {
  71.         scheme: '~/plugins/apollo-schema.js',
  72.         name: 'apollo',
  73.         provider: 'apollo',
  74.         default: true,
  75.       },
  76.     },
  77.   },
  78.  
  79. # ~/plugins/apollo.js
  80.  
  81. import { HttpLink } from 'apollo-link-http'
  82. import { setContext } from 'apollo-link-context'
  83. import { from } from 'apollo-link'
  84. import { InMemoryCache } from 'apollo-cache-inmemory'
  85.  
  86. export default ({ app, store }) => {
  87.   const { baseGQL } = app.context.env
  88.  
  89.   const headersConfig = setContext(() => ({
  90.     headers: {
  91.       'x-xsrf-token': app.$cookies.get('XSRF-TOKEN'),
  92.     },
  93.     credentials: 'include',
  94.   }))
  95.  
  96.   const httpLink = new HttpLink({
  97.     uri: baseGQL,
  98.   })
  99.  
  100.   const link = from([headersConfig, httpLink])
  101.   const cache = new InMemoryCache()
  102.  
  103.   return {
  104.     link,
  105.     cache,
  106.     defaultHttpLink: false,
  107.   }
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement