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. Composables

useSanctumAuth

PreviousToken AuthenticationNextuseSanctumUser

Composable provides 2 computed properties and 3 methods:

  • user - currently authenticated user (basically the same as )

  • isAuthenticated - a boolean flag indicating whether the user is authenticated or not

  • login - method for logging in the user

  • logout - method for logging out the user

  • refreshIdentity - method for manually re-fetching current authenticated user data

To authenticate a user you should pass the credentials payload as an argument to the login method. The payload should contain all fields required by your Laravel Sanctum backend.

components/LoginForm.vue
const { login } = useSanctumAuth();

const userCredentials = {
    email: 'user@mail.com',
    password: '123123',
};

await login(userCredentials);

If the login operation was successful, the user property will be updated with the current user information returned by the Laravel API.

By default, methods will use the following Laravel endpoints:

  • /login to authenticate the user

  • /logout to log out the user

  • /api/user to get the current user information

  • /sanctum/csrf-cookie to get the CSRF token cookie

To change the default endpoints, please check the section.

useSanctumUser
Configuration