0Pricing
Sveltejs Academy · Lesson

OAuth with SvelteKit and Lucia Auth

Add social login with OAuth providers using the Lucia auth library.

OAuth with SvelteKit and Lucia Auth is a free Sveltejs Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Sveltejs Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

OAuth Flow

OAuth lets users log in with Google, GitHub, etc. without sharing passwords with your app.

Lucia Auth

Lucia is a popular auth library for SvelteKit, simplifying sessions, OAuth, and CSRF.

Install Lucia

Install and choose an adapter for your database.

npm install lucia @lucia-auth/adapter-prisma

Initialize Lucia

Configure Lucia in $lib/server/auth.js with your adapter and env vars.

OAuth Setup

Add provider configuration with client ID and secret.

import { GitHub } from "arctic";
export const github = new GitHub(CLIENT_ID, CLIENT_SECRET);

Login Route

Create /login/github that redirects users to GitHub.

Callback Route

Handle the callback URL to exchange the code for tokens and create a session.

Session Cookies

Lucia sets a session cookie and provides helpers to read/clear it.

User Data

After successful OAuth, fetch user info and persist or link to an existing account.

CSRF in OAuth

Use the state parameter and PKCE to prevent CSRF in the OAuth flow.

Multi-Provider

Add Google, Apple, Discord, etc. with similar patterns from the Arctic library.

Quick Check

What is Lucia?

Recap

Lucia simplifies OAuth and sessions in SvelteKit. Configure providers (GitHub, Google) and handle callbacks to create sessions.

Frequently asked questions

Is the “OAuth with SvelteKit and Lucia Auth” lesson free?

Yes — the full text of “OAuth with SvelteKit and Lucia Auth” is free to read here on the web, and the Sveltejs Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Sveltejs Academy course, upgrade to CoddyKit PRO.

What will I learn in “OAuth with SvelteKit and Lucia Auth”?

Add social login with OAuth providers using the Lucia auth library. You practise Sveltejs Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Sveltejs Academy?

No prior experience is required. Sveltejs Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “OAuth with SvelteKit and Lucia Auth” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Sveltejs Academy lesson?

Yes. Every Sveltejs Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Cookie-Based Sessions
  2. JWT in HTTP-Only Cookies
  3. Protected Routes in handle() Hook
  4. OAuth with SvelteKit and Lucia Auth
← Back to Sveltejs Academy