Skip to content

Instantly share code, notes, and snippets.

@kriansa
Created April 29, 2023 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kriansa/3771e8303a2600885bd1feaab2ce2a98 to your computer and use it in GitHub Desktop.
Save kriansa/3771e8303a2600885bd1feaab2ce2a98 to your computer and use it in GitHub Desktop.
Authenticate with ID Token on Nuxt.js
// Here after the $auth is properly initialized, we add the idToken as part of the Authorization
// header sent to our API, as it is authenticatable by using the IDToken (JWT), not the AuthToken.
$axios.interceptors.request.use((config) => {
config.headers['Authorization'] = `Bearer ${$auth.strategy.idToken.get()}`
return config
})
// By default, axios request interceptors are executed in the order they are added in the
// interceptors stack. At this point on runtime, Nuxt Auth has already injected an interceptor
// that adds an Authorization token to the requests, so we are unable to override it unless we
// move our interceptor declared above in the front of $auth's one.
// Here's a quick way of simply taking that value we just added and moving it to the array start.
const moveFromLastToFirst = (a) => a.splice(0, 0, a.at(-1)) && a.pop()
moveFromLastToFirst($axios.interceptors.request.handlers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment