Guides

Authentication

How the demo session works and how to replace it with your own login.

stores/auth.ts

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.tsHolds the session, the user, login(), logout() and the demo role.
Auth middlewaremiddleware/auth.global.tsSends guests to /login?redirect=… and signed-in users away from the auth pages.
Demo usermocks/user.mock.tsBuilds the profile from the email.
Auth pagespages/login.vue, register.vue, forgot-password.vueThe forms, using the auth layout.
Settingsconfig/auth.config.tsSocial providers, demo credentials, legal links, mail apps and the resend countdown.
ts
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 auth or landing-page layout, 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:

pages/login.vue
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:

pages/login.vue
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.

Keep the token out of JavaScript
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_CREDENTIALS in config/auth.config.ts.
  • The social buttons you do not support: edit AUTH_PROVIDERS, or remove AppAuthSocial from the pages.
  • The demo role switcher: auth.setDemoRole(), used by View as this role in Team and roles, and the auth-demo-role cookie that user reads in stores/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.

© 2026 melsami UI.
Developer documentation for melsami UI.