Auth Helpers
Helper functions for working with authentication in Apollo.
Nuxt Apollo provides three (3) auth helpers which can be easily plugged into your authentication flow to interface with configured Apollo clients.
getToken
Retrieve the auth token token for the specified client.
This adheres to the apollo:auth
hook. and will attempt to automatically retrieve the token from the apollo:auth
hook.
const { getToken } = useApollo()const token = getToken()const otherToken = getToken('<client_name>')
getToken
Reference
client
: The Apollo client who's token should be retrieved.
onLogin
Used to apply the given auth token to the specified Apollo client. This is required if your GraphQL API expects authentication to be passed via a HTTP header.
By default, this function will reset the Apollo client cache and re-execute all queries, this behavior can be averted by passing false
as the third parameter (skipResetStore
).
const { onLogin } = useApollo()function handleLogin() { // your login flow... onLogin(token)}
onLogin
Reference
token
: The token to be applied.client
: The Apollo client to authenticate.- Default:
default
- Default:
skipResetStore
: Whether to skip resetting the Apollo client cache.- Default:
false
- Default:
onLogout
Used to remove the auth token from the specified Apollo client.
const { onLogout } = useApollo()function handleLogout() { // your logout flow... onLogout()}
onLogout
Reference
client
: The Apollo client to de-authenticate.- Default:
default
- Default:
skipResetStore
: Whether to skip resetting the Apollo client cache.- Default:
false
- Default: