Hooks

sanctum:error:response

Subscribe to response error hook to inject custom event handling

Usage

When you send a request to Laravel API using any available module's composable, it may return an error, such as 401, 419, 403, 404, etc.

By default, nuxt-auth-sanctum will try to redirect a user if 401 is returned. Unless you disable this by setting sanctum.redirectIfUnauthenticated to false in your nuxt.config.ts file.

However, if you need more granular control over API errors, you can subscribe to the sanctum:error hook and process the HTTP response according to your requirements.

app/plugins/sanctum-listener.ts
export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.hook('sanctum:error:response', (response) => {
    console.log('Sanctum error hook triggered', response)
  })
})

Here is what the hook looks like

interface RuntimeNuxtHooks {
  /**
   * Triggers when receiving an error response.
   */
  'sanctum:error:response': (response: FetchResponse<any>) => HookResult
}