Nuxt - Laravel Sanctum
GitHubNuxt Module
0.6
0.6
  • Getting Started
    • Introduction
    • Installation
  • Usage
    • Configuration
    • Cookie Authentication
    • Token Authentication
  • Composables
    • useSanctumAuth
    • useSanctumUser
    • useSanctumClient
    • useSanctumFetch
    • useLazySanctumFetch
    • useSanctumConfig
    • useSanctumAppConfig
  • Middleware
    • sanctum:auth
    • sanctum:guest
    • Global middleware
  • Hooks
    • sanctum:error
    • sanctum:error:request
    • sanctum:redirect
    • sanctum:init
    • sanctum:refresh
    • sanctum:login
    • sanctum:logout
  • Advanced
    • Interceptors
    • Error handling
    • Logging
    • Token storage
    • Plugin dependencies
    • Breeze Nuxt template
    • Troubleshooting
Powered by GitBook
On this page
Edit on GitHub
  1. Hooks

sanctum:error

Subscribe to hook and react to any error received from your Laravel API.

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.

plugins/sanctum-listener.ts
export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.hook('sanctum:error', (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: FetchResponse<any>) => HookResult
}
PreviousGlobal middlewareNextsanctum:error:request

Last updated 2 months ago