Skip to content
Migrating from NextAuth.js v4? Read our migration guide.
Getting Started
Installation

Installing Auth.js

Start by installing the appropriate package for your framework.

npm install @auth/sveltekit

Installing @auth/core is not necessary, as a user you should never have to interact with @auth/core.

Setup Environment

The only environment variable that is mandatory is the AUTH_SECRET. This is a random value used by the library to encrypt tokens and email verification hashes. (See Deployment to learn more). You can generate one via the official Auth.js CLI running:

npx auth secret

This will also add it to your .env file, respecting the framework conventions (eg.: Next.js’ .env.local).

Configure

Next, create the Auth.js config file and object. This is where you can control the behaviour of the library and specify custom authentication logic, adapters, etc. We recommend all frameworks to create an auth.ts file in the project. In this file we’ll pass in all the options to the framework specific initalization function and then export the route handler(s), signin and signout methods, and more.

You can name this file whatever you want and place it wherever you like, these are just conventions we’ve come up with.

  1. Start by creating a new auth.ts file in your src/ directory with the following content.
./src/auth.ts
import { SvelteKitAuth } from "@auth/sveltekit"
 
export const { handle } = SvelteKitAuth({
  providers: [],
})
  1. Re-export the handle method in SvelteKit’s src/hooks.server.ts file.
./src/hooks.server.ts
export { handle } from "./auth"
  1. That handle function adds an auth() method onto our event.locals, which is available in any +layout.server.ts or +page.server.ts file. Therefore, we can access the session in our load function like this.
./src/routes/+layout.server.ts
import type { LayoutServerLoad } from "./$types"
 
export const load: LayoutServerLoad = async (event) => {
  const session = await event.locals.auth()
 
  return {
    session,
  }
}

Setup Authentication Methods

With that, the basic setup is complete! Next we’ll setup the first authentication methods and fill out that providers array.

Last updated on
Auth.js © Balázs Orbán and Team - 2025