Hooks
Nuxt hooks to interact with nuxt-oidc-auth
Hooks
The following hooks are available to extend the default behavior of the OIDC module:
fetch
(Called when a user session is fetched)clear
(Called before a user session is cleared)refresh
(Called before a user session is refreshed)
Remember to also update the refresh hook if you modify the session, as claims and other fields would otherwise be wiped.
Example
export default defineNitroPlugin(() => {
sessionHooks.hook('fetch', async (session) => {
// Extend User Session
// Or throw createError({ ... }) if session is invalid
// session.extended = {
// fromHooks: true
// }
console.log('Injecting "status" claim as test')
if (!(Object.keys(session).length === 0)) {
const claimToAdd = { status: 'Fetch' }
session.claims = { ...session.claims, ...claimToAdd }
}
})
sessionHooks.hook('refresh', async (session) => {
console.log('Injecting "status" claim as test on refresh')
if (!(Object.keys(session).length === 0)) {
const claimToAdd = { status: 'Refresh' }
session.claims = { ...session.claims, ...claimToAdd }
}
})
sessionHooks.hook('clear', async (session) => {
// Log that user logged out
console.log('User logged out')
})
})
You can theoretically register a hook that overwrites internal session fields like loggedInAt
, but this is not recommended as it has an impact on the loggedIn
state of your session. It will not impact the server side refresh and expiration logic, but will be overwritten with each refresh.