Skip to content

Authentication

giggity-hub edited this page Aug 3, 2023 · 1 revision

Lucia

Lucia needs three tables to work. One for user data, one for passwords and one for sessions. You can add additional fields to the tables as long as you update the types inside src/app.d.ts. For example below you see how the additional name and row fields of the user Table are added.

declare global {
	namespace Lucia {
		type DatabaseUserAttributes = {
			// All additional Attributes have to be specified here
			name: String,
			role?: 'user' | 'admin',
		};
	}
}

The Configuration for Lucia is in lib/server/lucia.ts

Sessions

  • The session id is stored in the auth_session cookie
  • The lucia auth object is added to event.locals inside the hooks.server.ts
  • Inside the +layout.server.ts the session object is retreived from the session id in the cookie and returned in the response
    • If the cookie is valid it will be refreshed
    • If the cookie is invalid or not present the auth field of the response will be null
  • Now the session object is available in the whole application through the $page store
import {page} from '$app/stores'
let userObject = $page.data.session.user

Authentication Actions

Clone this wiki locally