Guides
Authentication
How the demo session works and how to replace it with your own login.
How the demo works
The demo session is the user's email saved in the auth-session cookie. Because it is a cookie, the server knows who is signed in and renders every page with the user already in place.
Piece | File | Role |
|---|---|---|
useAuthStore() | stores/auth.ts | Holds the session, the user, login(), logout() and the demo role. |
| Auth middleware | middleware/auth.global.ts | Sends guests to /login?redirect=… and signed-in users away from the auth pages. |
| Demo user | mocks/user.mock.ts | Builds the profile from the email. |
| Auth pages | pages/login.vue, register.vue, forgot-password.vue | The forms, using the auth layout. |
| Settings | config/auth.config.ts | Social providers, demo credentials, legal links, mail apps and the resend countdown. |
const auth = useAuthStore()
auth.isAuthenticated
auth.user
auth.login('[email protected]', true)
await auth.logout()
login(email, remember) keeps the session for 30 days when remember is true and for the browser session otherwise. After signing in, the user goes to the redirect query or to AUTH_HOME_ROUTE (/dashboard/analytics).
Which pages need a session
The middleware lets a page through without a session when it:
- uses the
authorlanding-pagelayout, or - sets
definePageMeta({ public: true }).
Every other page sends guests to the login page and brings them back after signing in.
Replace the demo with your API
1. Call your endpoint on login
The login page passes mockLogin from mocks/forms.mock.ts to useFormSubmit() and, on success, saves the session and follows the redirect:
const { formRef, isSubmitting, formError, fieldErrors, submit } = useFormSubmit(values, mockLogin, {
onSuccess: async () => {
auth.login(values.email, values.remember)
await router.push(redirectTo.value)
},
})
Replace mockLogin with a function that calls your endpoint and stores the token:
const api = useApi()
const token = useCookie<string | null>(AUTH_TOKEN_COOKIE_KEY)
async function login(payload: LoginPayload) {
try {
const session = await api<{ token: string }>('/auth/login', { method: 'POST', body: payload })
token.value = session.token
} catch (error) {
throw toFormSubmitError(error)
}
}
useApi() sends Authorization: Bearer <token> on every request while the auth-token cookie exists, and signs the user out when the API answers 401. Do the same in register.vue and forgot-password.vue with mockRegister and mockRequestPasswordReset.
auth-token and auth-session are set from the browser, so any script on the page can read them; outside development they are marked Secure and only travel over HTTPS. In production, let your API set the session in an HttpOnly; Secure; SameSite=Lax cookie and send requests with credentials: 'include': the client never sees the token and an injected script cannot steal it.2. Load the real user
user in stores/auth.ts is built from the email by mockSessionUser(). Replace it with your profile request, for example with useAsyncData keyed on the session, and keep the same UserModel shape so the user menu, the profile and the permissions keep working.
3. Remove the demo shortcuts
- The Try the demo button and its hint on the login page.
AUTH_DEMO_CREDENTIALSinconfig/auth.config.ts.- The social buttons you do not support: edit
AUTH_PROVIDERS, or removeAppAuthSocialfrom the pages. - The demo role switcher:
auth.setDemoRole(), used by View as this role in Team and roles, and theauth-demo-rolecookie thatuserreads instores/auth.ts. While that cookie is read, anyone can give themselves any role by editing it.
Social sign-in
AppAuthSocial renders the providers in AUTH_PROVIDERS with their brand icons. compact shows them as icon buttons and passkey adds a passkey option. In the demo, useSocialSignIn() signs in the demo user; replace it with your OAuth redirect.
Preview the auth pages while signed in
Signed-in users are sent away from the auth pages. Add ?preview to the URL (/login?preview) to see them anyway; the Pages section of the menu uses it.