I am trying to create a login functionality using nuxtjs with the nuxtjs apollo-module and nodejs in the backend using apollo-server. I would like to pass the token from the frontend (nuxtjs/apollo-client) to the backend (nodejs/apollo-server).
Signin Function
async signin () {
const email = this.email
const password = this.password
try {
const res = await this.$apollo.mutate({
mutation: signIn,
variables: {
email,
password
}
}).then(({ data }) => data && data.signIn)
const token = res.token
await this.$apolloHelpers.onLogin(token)
this.$router.push('/')
} catch (err) {
// Error message
}
}
nuxtjs.config
import Cookies from 'js-cookie'
const token = Cookies.get('apollo-token')
apollo: {
clientConfigs: {
httpEndpoint: 'http://localhost:8000/graphql',
httpLinkOptions: {
credentials: 'include',
headers: {
'authorization': token ? token : '' // passing the token on this line fails it returns ''
}
}
}
},
I am unable to pass the token to the authorization header. I also tried to do it using localStorage (`'authorization': (process.client) ? localStorage.getItem('token'`) : ' '`) but this does not seem to be possible because of the server side rendering.
The token is saved in a cookie called 'apollo-token'. However the Authoriation header in the format 'Bearer token' is not set. According to the apollo-client documentation this should be set automatically (https://github.com/nuxt-community/apollo-module#authenticationtype-string-optional-default-bearer).
I would be very very thankful for any kind of help!