mirror of
https://github.com/SrIzan10/next-auth.git
synced 2026-05-01 10:55:20 +00:00
Bumps [next-auth](https://github.com/nextauthjs/next-auth) from 4.5.0 to 4.9.0. - [Release notes](https://github.com/nextauthjs/next-auth/releases) - [Changelog](https://github.com/nextauthjs/next-auth/blob/main/CHANGELOG.md) - [Commits](https://github.com/nextauthjs/next-auth/compare/next-auth@v4.5.0...next-auth@v4.9.0) --- updated-dependencies: - dependency-name: next-auth dependency-type: direct:production ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
SvelteKit + NextAuth.js Playground
NextAuth.js is committed to bringing easy authentication to other frameworks. https://github.com/nextauthjs/next-auth/issues/2294
SvelteKit support with NextAuth.js is currently experimental. This directory contains a minimal, proof-of-concept application. Parts of this is expected to be abstracted away into a package like @next-auth/sveltekit
Existing Project
Add API route
To add NextAuth.js to a project create a file called [...nextauth].js in routes/api/auth. This contains the dynamic route handler for NextAuth.js which will also contain all of your global NextAuth.js configurations.
import NextAuth from "$lib"
import GithubProvider from "next-auth/providers/github"
const nextAuthOptions = {
// Configure one or more authentication providers
providers: [
GithubProvider({
clientId: import.meta.env.VITE_GITHUB_CLIENT_ID,
clientSecret: import.meta.env.VITE_GITHUB_CLIENT_SECRET,
}),
// ...add more providers here
],
}
export const { get, post } = NextAuth(nextAuthOptions)
Add hook
import { getServerSession } from "$lib"
import GithubProvider from "next-auth/providers/github"
const nextAuthOptions = {
providers: [
GithubProvider({
clientId: import.meta.env.VITE_GITHUB_CLIENT_ID,
clientSecret: import.meta.env.VITE_GITHUB_CLIENT_SECRET,
}),
],
}
export async function handle({ event, resolve }) {
const session = await getServerSession(event.request, nextAuthOptions)
event.locals.session = session
return resolve(event)
}
export function getSession(event) {
return event.locals.session || {}
}
Protecting a route
<script context="module">
export async function load({ session }) {
const { user } = session
if (!user) {
return {
status: 302,
redirect: "/",
}
}
return {
props: {
session,
},
}
}
</script>
<script>
export let session
</script>
<p>Session expiry: {session.expires}</p>