Composables
useSanctumAuth
How to use useSanctumAuth composable to work with state
Usage
Composable provides 2 computed properties and 4 methods:
user- currently authenticated user (basically the same asuseSanctumUser)isAuthenticated- a boolean flag indicating whether the user is authenticated or notlogin- method for logging in the userlogout- method for logging out the userrefreshIdentity- method for manually re-fetching current authenticated user datacheckSession- method to validate existence of the session (cookie in the browser or token in the storage)
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.
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:
/loginto authenticate the user/logoutto log out the user/api/userto get the current user information/sanctum/csrf-cookieto get theCSRFtoken cookie
To change the default endpoints, please check the Configuration section.