Token Authentication

How to setup Sanctum Bearer token-based authentication for Laravel Echo.

Token-based authentication is not recommended for SPA applications. Still, it might be useful when you cannot host it on the same TLD or have a mobile or desktop application built with Nuxt (e.g. based on Capacitor).

To explicitly set this authentication mode, update echo.authentication.mode configuration property to token.

You can check the official Laravel documentation here - API Token Authentication.

How it works

You should already have an authenticated user by submitting credentials to your login endpoint, for instance, using the Nuxt Auth Sanctum module.

Ensure that you use token mode for nuxt-auth-sanctum module to save the Bearer token from the API response on authentication requests.

Custom token storage

Default token storage uses cookies to keep the Authentication token and automatically load it for Echo channel authorization requests. However, you are free to define custom storage in your app.config.ts by implementing an interface (especially, when cookies are not supported, for example - Capacitor, Ionic, LocalStorage, etc).

Check this section for more details - Token storage.

Last updated