Compare commits

..

3 Commits

Author SHA1 Message Date
Balázs Orbán
c9a47a5138 add warning if CSRF endpoint used when skipped 2023-01-12 15:43:50 +01:00
Balázs Orbán
da81e98084 fix logic 2023-01-12 15:24:27 +01:00
Balázs Orbán
56f414f8d6 feat(core): add way to opt-out of CSRF checks 2023-01-12 15:14:17 +01:00
70 changed files with 1440 additions and 2252 deletions

View File

@@ -23,8 +23,8 @@ pnpm-lock.yaml
.docusaurus .docusaurus
build build
docs/docs/reference/core docs/docs/reference/03-core
docs/docs/reference/sveltekit docs/docs/reference/04-sveltekit
static static
# --------------- Packages --------------- # --------------- Packages ---------------

View File

@@ -30,7 +30,7 @@ body:
Run this command in your project's root folder and paste the result: Run this command in your project's root folder and paste the result:
```sh ```sh
npx envinfo --system --binaries --browsers --npmPackages "next,react,next-auth,@auth/*" npx envinfo --system --binaries --browsers --npmPackages "next,react,next-auth"
``` ```
Alternatively, you can manually gather the version information from your package.json for these packages: "next", "react" and "next-auth". Please also mention your OS and Node.js version, as well as the browser you are using. Alternatively, you can manually gather the version information from your package.json for these packages: "next", "react" and "next-auth". Please also mention your OS and Node.js version, as well as the browser you are using.
validations: validations:

View File

@@ -25,7 +25,6 @@ body:
- "Custom provider" - "Custom provider"
- "42 School" - "42 School"
- "Apple" - "Apple"
- "Asgardeo"
- "Atlassian" - "Atlassian"
- "Auth0" - "Auth0"
- "Authentik" - "Authentik"
@@ -58,7 +57,6 @@ body:
- "Medium" - "Medium"
- "Naver" - "Naver"
- "Netlify" - "Netlify"
- "Notion"
- "Okta" - "Okta"
- "OneLogin" - "OneLogin"
- "Osso" - "Osso"
@@ -89,7 +87,7 @@ body:
Run this command in your project's root folder and paste the result: Run this command in your project's root folder and paste the result:
```sh ```sh
npx envinfo --system --binaries --browsers --npmPackages "next,react,next-auth,@auth/*" npx envinfo --system --binaries --browsers --npmPackages "next,react,next-auth"
``` ```
Alternatively, you can manually gather the version information from your package.json for these packages: "next", "react" and "next-auth". Please also mention your OS and Node.js version, as well as the browser you are using. Alternatively, you can manually gather the version information from your package.json for these packages: "next", "react" and "next-auth". Please also mention your OS and Node.js version, as well as the browser you are using.
validations: validations:

View File

@@ -44,7 +44,7 @@ body:
Run this command in your project's root folder and paste the result: Run this command in your project's root folder and paste the result:
```sh ```sh
npx envinfo --system --binaries --browsers --npmPackages "next,react,next-auth,@auth/*" && npx envinfo --npmPackages "@next-auth/*" npx envinfo --system --binaries --browsers --npmPackages "next,react,next-auth" && npx envinfo --npmPackages "@next-auth/*"
``` ```
Alternatively, if the above command did not work, we need the version of the following packages from your package.json: "next", "react", "next-auth" and your adapter. Please also mention your OS and Node.js version, as well as the browser you are using. Alternatively, if the above command did not work, we need the version of the following packages from your package.json: "next", "react", "next-auth" and your adapter. Please also mention your OS and Node.js version, as well as the browser you are using.
validations: validations:

View File

@@ -35,22 +35,6 @@ jobs:
UPSTASH_REDIS_KEY: ${{ secrets.UPSTASH_REDIS_KEY }} UPSTASH_REDIS_KEY: ${{ secrets.UPSTASH_REDIS_KEY }}
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }} TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
TURBO_TEAM: ${{ secrets.TURBO_TEAM }} TURBO_TEAM: ${{ secrets.TURBO_TEAM }}
# - name: Run E2E tests
# if: github.repository == 'nextauthjs/next-auth'
# run: pnpm e2e
# timeout-minutes: 15
# env:
# AUTH0_USERNAME: ${{ secrets.AUTH0_USERNAME }}
# AUTH0_PASSWORD: ${{ secrets.AUTH0_PASSWORD }}
# TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
# TURBO_TEAM: ${{ secrets.TURBO_TEAM }}
# - name: Upload E2E artifacts
# if: github.repository == 'nextauthjs/next-auth'
# uses: actions/upload-artifact@v3
# with:
# name: playwright-report
# path: apps/dev/nextjs/playwright-report/
# retention-days: 30
# - name: Coverage # - name: Coverage
# uses: codecov/codecov-action@v1 # uses: codecov/codecov-action@v1
# with: # with:

14
.gitignore vendored
View File

@@ -34,10 +34,13 @@ packages/next-auth/utils
packages/next-auth/core packages/next-auth/core
packages/next-auth/jwt packages/next-auth/jwt
packages/next-auth/react packages/next-auth/react
packages/next-auth/adapters.d.ts
packages/next-auth/adapters.js
packages/next-auth/index.d.ts
packages/next-auth/index.js
packages/next-auth/next packages/next-auth/next
packages/*/*.js packages/next-auth/middleware.d.ts
packages/*/*.d.ts packages/next-auth/middleware.js
packages/*/*.d.ts.map
# Development app # Development app
apps/dev/src/css apps/dev/src/css
@@ -81,12 +84,11 @@ docs/providers.json
packages/core/*.js packages/core/*.js
packages/core/*.d.ts packages/core/*.d.ts
packages/core/*.d.ts.map packages/core/*.d.ts.map
packages/core/src/providers/oauth-types.ts
packages/core/lib packages/core/lib
packages/core/providers packages/core/providers
packages/core/src/lib/pages/styles.ts packages/core/src/lib/pages/styles.ts
docs/docs/reference/core docs/docs/reference/03-core
docs/docs/reference/sveltekit docs/docs/reference/04-sveltekit
# SvelteKit # SvelteKit

View File

@@ -20,8 +20,8 @@ pnpm-lock.yaml
.docusaurus .docusaurus
build build
docs/docs/reference/core docs/docs/reference/03-core
docs/docs/reference/sveltekit docs/docs/reference/04-sveltekit
static static
docs/providers.json docs/providers.json

View File

@@ -9,10 +9,6 @@ NEXTAUTH_URL=http://localhost:3000
# and/or verification tokens. # and/or verification tokens.
NEXTAUTH_SECRET=secret NEXTAUTH_SECRET=secret
ASGARDEO_CLIENT_ID=
ASGARDEO_CLIENT_SECRET=
ASGARDEO_ISSUER=
AUTH0_ID= AUTH0_ID=
AUTH0_SECRET= AUTH0_SECRET=
AUTH0_ISSUER= AUTH0_ISSUER=
@@ -21,10 +17,6 @@ KEYCLOAK_ID=
KEYCLOAK_SECRET= KEYCLOAK_SECRET=
KEYCLOAK_ISSUER= KEYCLOAK_ISSUER=
NOTION_ID=
NOTION_SECRET=
NOTION_REDIRECT_URI=
IDS4_ID= IDS4_ID=
IDS4_SECRET= IDS4_SECRET=
IDS4_ISSUER= IDS4_ISSUER=

View File

@@ -1,4 +0,0 @@
node_modules/
/test-results/
/playwright-report/
/playwright/.cache/

View File

@@ -9,12 +9,10 @@
"build": "next build", "build": "next build",
"start": "next start", "start": "next start",
"email": "fake-smtp-server", "email": "fake-smtp-server",
"start:email": "pnpm email", "start:email": "pnpm email"
"e2e": "pnpm dlx playwright test"
}, },
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@auth/core": "workspace:*",
"@next-auth/fauna-adapter": "workspace:*", "@next-auth/fauna-adapter": "workspace:*",
"@next-auth/prisma-adapter": "workspace:*", "@next-auth/prisma-adapter": "workspace:*",
"@next-auth/supabase-adapter": "workspace:*", "@next-auth/supabase-adapter": "workspace:*",
@@ -24,16 +22,15 @@
"faunadb": "^4", "faunadb": "^4",
"next": "13.1.1", "next": "13.1.1",
"next-auth": "workspace:*", "next-auth": "workspace:*",
"@auth/core": "workspace:*",
"nodemailer": "^6", "nodemailer": "^6",
"react": "^18", "react": "^18",
"react-dom": "^18" "react-dom": "^18"
}, },
"devDependencies": { "devDependencies": {
"@playwright/test": "1.29.2",
"@types/jsonwebtoken": "^8.5.5", "@types/jsonwebtoken": "^8.5.5",
"@types/react": "^18.0.15", "@types/react": "^18.0.15",
"@types/react-dom": "^18.0.6", "@types/react-dom": "^18.0.6",
"dotenv": "^16.0.3",
"fake-smtp-server": "^0.8.0", "fake-smtp-server": "^0.8.0",
"pg": "^8.7.3", "pg": "^8.7.3",
"prisma": "^3", "prisma": "^3",

View File

@@ -2,7 +2,6 @@ import { Auth, type AuthConfig } from "@auth/core"
// Providers // Providers
import Apple from "@auth/core/providers/apple" import Apple from "@auth/core/providers/apple"
import Asgardeo from "@auth/core/providers/asgardeo"
import Auth0 from "@auth/core/providers/auth0" import Auth0 from "@auth/core/providers/auth0"
import AzureAD from "@auth/core/providers/azure-ad" import AzureAD from "@auth/core/providers/azure-ad"
import AzureB2C from "@auth/core/providers/azure-ad-b2c" import AzureB2C from "@auth/core/providers/azure-ad-b2c"
@@ -24,7 +23,6 @@ import Instagram from "@auth/core/providers/instagram"
import Line from "@auth/core/providers/line" import Line from "@auth/core/providers/line"
import LinkedIn from "@auth/core/providers/linkedin" import LinkedIn from "@auth/core/providers/linkedin"
import Mailchimp from "@auth/core/providers/mailchimp" import Mailchimp from "@auth/core/providers/mailchimp"
import Notion from "@auth/core/providers/notion"
// import Okta from "@auth/core/providers/okta" // import Okta from "@auth/core/providers/okta"
import Osu from "@auth/core/providers/osu" import Osu from "@auth/core/providers/osu"
import Patreon from "@auth/core/providers/patreon" import Patreon from "@auth/core/providers/patreon"
@@ -70,7 +68,7 @@ import WorkOS from "@auth/core/providers/workos"
export const authConfig: AuthConfig = { export const authConfig: AuthConfig = {
// adapter, // adapter,
debug: process.env.NODE_ENV !== "production", // debug: process.env.NODE_ENV !== "production",
theme: { theme: {
logo: "https://next-auth.js.org/img/logo/logo-sm.png", logo: "https://next-auth.js.org/img/logo/logo-sm.png",
brandColor: "#1786fb", brandColor: "#1786fb",
@@ -84,7 +82,6 @@ export const authConfig: AuthConfig = {
}, },
}), }),
Apple({ clientId: process.env.APPLE_ID, clientSecret: process.env.APPLE_SECRET }), Apple({ clientId: process.env.APPLE_ID, clientSecret: process.env.APPLE_SECRET }),
Asgardeo({ clientId: process.env.ASGARDEO_CLIENT_ID, clientSecret: process.env.ASGARDEO_CLIENT_SECRET, issuer: process.env.ASGARDEO_ISSUER }),
Auth0({ clientId: process.env.AUTH0_ID, clientSecret: process.env.AUTH0_SECRET, issuer: process.env.AUTH0_ISSUER }), Auth0({ clientId: process.env.AUTH0_ID, clientSecret: process.env.AUTH0_SECRET, issuer: process.env.AUTH0_ISSUER }),
AzureAD({ AzureAD({
clientId: process.env.AZURE_AD_CLIENT_ID, clientId: process.env.AZURE_AD_CLIENT_ID,
@@ -108,7 +105,6 @@ export const authConfig: AuthConfig = {
Line({ clientId: process.env.LINE_ID, clientSecret: process.env.LINE_SECRET }), Line({ clientId: process.env.LINE_ID, clientSecret: process.env.LINE_SECRET }),
LinkedIn({ clientId: process.env.LINKEDIN_ID, clientSecret: process.env.LINKEDIN_SECRET }), LinkedIn({ clientId: process.env.LINKEDIN_ID, clientSecret: process.env.LINKEDIN_SECRET }),
Mailchimp({ clientId: process.env.MAILCHIMP_ID, clientSecret: process.env.MAILCHIMP_SECRET }), Mailchimp({ clientId: process.env.MAILCHIMP_ID, clientSecret: process.env.MAILCHIMP_SECRET }),
Notion({ clientId: process.env.NOTION_ID, clientSecret: process.env.NOTION_SECRET, redirectUri: process.env.NOTION_REDIRECT_URI }),
// Okta({ clientId: process.env.OKTA_ID, clientSecret: process.env.OKTA_SECRET, issuer: process.env.OKTA_ISSUER }), // Okta({ clientId: process.env.OKTA_ID, clientSecret: process.env.OKTA_SECRET, issuer: process.env.OKTA_ISSUER }),
Osu({ clientId: process.env.OSU_CLIENT_ID, clientSecret: process.env.OSU_CLIENT_SECRET }), Osu({ clientId: process.env.OSU_CLIENT_ID, clientSecret: process.env.OSU_CLIENT_SECRET }),
Patreon({ clientId: process.env.PATREON_ID, clientSecret: process.env.PATREON_SECRET }), Patreon({ clientId: process.env.PATREON_ID, clientSecret: process.env.PATREON_SECRET }),

View File

@@ -1,107 +0,0 @@
import type { PlaywrightTestConfig } from '@playwright/test';
import { devices } from '@playwright/test';
/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
require('dotenv').config();
/**
* See https://playwright.dev/docs/test-configuration.
*/
const config: PlaywrightTestConfig = {
testDir: './tests',
/* Maximum time one test can run for. */
timeout: 30 * 1000,
expect: {
/**
* Maximum time expect() should wait for the condition to be met.
* For example in `await expect(locator).toHaveText();`
*/
timeout: 5000
},
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: 'html',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
actionTimeout: 0,
/* Base URL to use in actions like `await page.goto('/')`. */
// baseURL: 'http://localhost:3000',
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},
/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: {
...devices['Desktop Chrome'],
},
},
{
name: 'firefox',
use: {
...devices['Desktop Firefox'],
},
},
{
name: 'webkit',
use: {
...devices['Desktop Safari'],
},
},
/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: {
// ...devices['Pixel 5'],
// },
// },
// {
// name: 'Mobile Safari',
// use: {
// ...devices['iPhone 12'],
// },
// },
/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: {
// channel: 'msedge',
// },
// },
// {
// name: 'Google Chrome',
// use: {
// channel: 'chrome',
// },
// },
],
/* Folder for test artifacts such as screenshots, videos, traces, etc. */
// outputDir: 'test-results/',
/* Run your local dev server before starting the tests */
// webServer: {
// command: 'npm run start',
// port: 3000,
// },
};
export default config;

View File

@@ -1,39 +0,0 @@
import { test, expect } from "@playwright/test"
test("Sign in with Auth0", async ({ page }) => {
// Go to NextAuth example app
await page.goto("https://next-auth-example.vercel.app")
// Click 'Sign In'
await page.click("#__next > header > div > p > a")
// Auth0 Login Provider
await page.click('body > div > div form[action*="auth0"] > button')
// Enter Credentials (Username/Password Login) on Auth0 Widget
await page.type("#username", process.env.AUTH0_USERNAME!)
await page.type("#password", process.env.AUTH0_PASSWORD!)
// Snap a screenshot
// await page.screenshot({ path: "1-auth0-login.png", fullPage: true })
// Press submit on Auth0 form
await page.click('body > div > main > section > div button[type="submit"]')
// Wait for next-auth example page login status header to appear
await page.waitForTimeout(2000)
// Snap a screenshot
// await page.screenshot({
// path: "2-next-auth-redirect-result.png",
// fullPage: false,
// })
// Check session object after successful login
const response = await page.goto(
"https://next-auth-example.vercel.app/api/auth/session"
)
const session = await response?.json()
expect(session?.user?.email).toBe(process.env.AUTH0_USERNAME)
// TODO: Check whole object with .toEqual()
})

View File

@@ -19,8 +19,8 @@
"vite": "4.0.1" "vite": "4.0.1"
}, },
"dependencies": { "dependencies": {
"@auth/core": "workspace:*", "@auth/core": "0.2.5",
"@auth/sveltekit": "workspace:*" "@auth/sveltekit": "0.1.12"
}, },
"type": "module" "type": "module"
} }

View File

@@ -2,10 +2,11 @@
"name": "playground-nuxt", "name": "playground-nuxt",
"private": true, "private": true,
"scripts": { "scripts": {
"build": "nuxt prepare && nuxt build", "build": "nuxt build",
"dev": "nuxt prepare && export NODE_OPTIONS='--no-experimental-fetch' && nuxt dev", "dev": "export NODE_OPTIONS='--no-experimental-fetch' && nuxt dev",
"generate": "nuxt generate", "generate": "nuxt generate",
"preview": "nuxt preview" "preview": "nuxt preview",
"postinstall": "nuxt prepare"
}, },
"devDependencies": { "devDependencies": {
"@nuxt/eslint-config": "^0.1.1", "@nuxt/eslint-config": "^0.1.1",

View File

@@ -70,7 +70,7 @@ Auth.js is extremely customizable, [our guides section](/guides/overview) will t
To be able to use `useSession` first you'll need to expose the session context, [`<SessionProvider />`](/reference/react/#sessionprovider), at the top level of your application: To be able to use `useSession` first you'll need to expose the session context, [`<SessionProvider />`](/reference/react/#sessionprovider), at the top level of your application:
```ts title="pages/_app.tsx" ```ts title="pages/_app.ts"
import { SessionProvider } from "next-auth/react" import { SessionProvider } from "next-auth/react"
export default function App({ export default function App({
@@ -186,14 +186,14 @@ http://localhost:3000/api/auth/callback/github
Auth.js will already magically create this API endpoint for you when we start the application later. Note that because we're using Next.js, locally it starts our server on the port `3000`, hence the origin is `http://localhost:3000`. Auth.js will already magically create this API endpoint for you when we start the application later. Note that because we're using Next.js, locally it starts our server on the port `3000`, hence the origin is `http://localhost:3000`.
::: :::
Next you'll be presented with the following screen which presents all the configuration for your new OAuth app. For now, we need two things from it: the **Client ID** and **Client Secret** for our new OAuth app: Next you'll be presented with the following screen which presents all the configuration for your new OAuth app. For now, let's we need two things from it: the **Client ID** and **Client Secret** for our new OAuth app:
<img src={gettingClientIdSecretImg} /> <img src={gettingClientIdSecretImg} />
The Client ID is always there, a public identifier of your OAuth application within Github. Click on the **Generate a new client Secret** button and should be presented with a new string (which is just a randomized string). The Client ID is always there, a public identifier of your OAuth application within Github. Click on the **Generate a new client Secret** button and should be presented with a new string (which is just a randomized string).
:::warning :::warning
🔥 Keep both your Client ID and Client Secret secure and never expose them to the public or shared with people outside your organization. With them, a malicious actor could hijack your application and cause you and your user serious problems! 🔥 Keep both your Client ID and Client Secret secure and never expose them to the public or shared with people outside your organization. With tem a malicious actor could hijack your application and cause you and your user serious problems!
::: :::
Now let's copy both the Client ID and Client Secret and paste them in an environment file in the root of your project like so: Now let's copy both the Client ID and Client Secret and paste them in an environment file in the root of your project like so:

View File

@@ -5,7 +5,7 @@ title: TypeScript
Auth.js has its own type definitions to use in your TypeScript projects safely. Even if you don't use TypeScript, IDEs like VSCode will pick this up to provide you with a better developer experience. While you are typing, you will get suggestions about what certain objects/functions look like, and sometimes links to documentation, examples, and other valuable resources. Auth.js has its own type definitions to use in your TypeScript projects safely. Even if you don't use TypeScript, IDEs like VSCode will pick this up to provide you with a better developer experience. While you are typing, you will get suggestions about what certain objects/functions look like, and sometimes links to documentation, examples, and other valuable resources.
Check out the example repository showcasing how to use `next-auth` on a Next.js application with TypeScript: Check out the example repository showcasing how to use `next-auth` on a Next.js application with TypeScript:
https://github.com/nextauthjs/next-auth-example https://github.com/nextauthjs/next-auth-typescript-example
--- ---

View File

@@ -22,7 +22,7 @@ Using a JWT to store the `refresh_token` is less secure than saving it in a data
#### JWT strategy #### JWT strategy
Using the [jwt](../../reference/core/interfaces/types.CallbacksOptions.md#jwt) and [session](../../reference/core/interfaces/types.CallbacksOptions.md#session) callbacks, we can persist OAuth tokens and refresh them when they expire. Using the [jwt](../../reference/03-core/interfaces/types.CallbacksOptions.md#jwt) and [session](../../reference/03-core/interfaces/types.CallbacksOptions.md#session) callbacks, we can persist OAuth tokens and refresh them when they expire.
Below is a sample implementation using Google's Identity Provider. Please note that the OAuth 2.0 request in the `refreshAccessToken()` function will vary between different providers, but the core logic should remain similar. Below is a sample implementation using Google's Identity Provider. Please note that the OAuth 2.0 request in the `refreshAccessToken()` function will vary between different providers, but the core logic should remain similar.
@@ -136,7 +136,7 @@ export default Auth(new Request("https://example.com"), {
const [google] = await prisma.account.findMany({ const [google] = await prisma.account.findMany({
where: { userId: user.id, provider: "google" }, where: { userId: user.id, provider: "google" },
}) })
if (google.expires_at < Date.now()) { if (google.expires_at >= Date.now()) {
// If the access token has expired, try to refresh it // If the access token has expired, try to refresh it
try { try {
// https://accounts.google.com/.well-known/openid-configuration // https://accounts.google.com/.well-known/openid-configuration

View File

@@ -1,153 +0,0 @@
---
title: Role-based authentication
---
There are two ways to add role-based authentication (RBAC) to your application, based on the [session strategy](/concepts/session-strategies) you choose. Let's see an example for each of these.
## Getting the role
We are going to start by adding a `profile()` callback to the providers' config to determine the user role:
```ts title="/pages/api/auth/[...nextauth].ts"
import NextAuth from "next-auth"
import Google from "next-auth/providers/google"
export default NextAuth({
providers: [
Google({
profile(profile) {
return { role: profile.role ?? "user", ... }
},
...
})
],
})
```
:::tip
To determine the user's role, you can either add your logic or if your provider assigns roles already, use that instead.
:::
## Persisting the role
### With JWT
When you don't have a database configured, the role will be persisted in a cookie, by using the `jwt()` callback. On sign-in, the `role` property is exposed from the `profile` callback on the `user` object. Persist the `user.role` value by assigning it to `token.role`. That's it!
If you also want to use the role on the client, you can expose it via the `session` callback.
```ts title="/pages/api/auth/[...nextauth].ts"
import NextAuth from "next-auth"
import Google from "next-auth/providers/google"
export default NextAuth({
providers: [
Google({
profile(profile) {
return { role: profile.role ?? "user", ... }
},
...
})
],
// highlight-start
callbacks: {
jwt({ token, user }) {
if(user) token.role = user.role
return token
},
session({ session, token }) {
session.user.role = token.role
return session
}
}
// highlight-end
})
```
:::info
With this strategy, if you want to update the role, the user needs to be forced to sign in again.
:::
### With Database
When you have a database, you can save the user role on the [User model](/reference/adapters/models#user). The below example is showing you how to do this with Prisma, but the idea is the same for all adapters.
First, add a `role` column to the User model.
```ts title="/prisma/schema.prisma"
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
role String? // New column
accounts Account[]
sessions Session[]
}
```
The `profile()` callback's return value is used to create users in the database. That's it! Your newly created users will now have an assigned role.
If you also want to use the role on the client, you can expose it via the `session` callback.
```ts title="/pages/api/auth/[...nextauth].ts"
import NextAuth from "next-auth"
import Google from "next-auth/providers/google"
// highlight-next-line
import prisma from "lib/prisma"
export default NextAuth({
// highlight-next-line
adapter: PrismaAdapter(prisma),
providers: [
Google({
profile(profile) {
return { role: profile.role ?? "user", ... }
}
...
})
],
// highlight-start
callbacks: {
session({ session, user }) {
session.user.role = user.role
return session
}
}
// highlight-end
})
```
:::info
It is up to you how you want to manage to update the roles, either through direct database access or building your role update API.
:::
## Using the role
If you want to use the role in the client, for both cases above, when using the `useSession` hook, `session.user.role` will have the required role if you exposed it via the `session` callback. You can use this to render a different UI for different users.
```ts title="/pages/admin.tsx"
import { useSession } from "next-auth/react"
export default function Page() {
const session = await useSession()
if (session?.user.role === "admin") {
return <p>You are an admin, welcome!</p>
}
return <p>You are not authorized to view this page!</p>
}
```
:::tip
When using Next.js and JWT, you can alternatively also use [Middleware](https://next-auth.js.org/configuration/nextjs#wrap-middleware) to redirect the user based on their role, even before rendering the page.
:::
## Resources
- [Concepts: Session strategies](/concepts/session-strategies)
- [Next.js: Middleware](https://next-auth.js.org/configuration/nextjs#wrap-middleware)
- [Adapters: User model](/reference/adapters/models#user)
- [Adapters: Prisma adapter](/reference/adapters/prisma)
- [TypeScript](/getting-started/typescript)

View File

@@ -0,0 +1,64 @@
---
title: Role based logins
---
To add role based authentication to your application, you must do three things.
1. Update your database schema
2. Add the `role` to the session object
3. Check for `role` in your pages/components
First modify the `user` table and add a `role` column with the type of `String?`.
Below is an example Prisma schema file.
```javascript title="/prisma/schema.prisma"
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
role String? // New Column
accounts Account[]
sessions Session[]
}
```
Next, implement a custom session callback in the `[...nextauth].js` file, as shown below.
```javascript title="/pages/api/auth/[...nextauth].js"
callbacks: {
async session({ session, token, user }) {
session.user.role = user.role; // Add role value to user object so it is passed along with session
return session;
},
```
Going forward, when using the `getSession` hook, check that `session.user.role` matches the required role. The example below assumes the role `'admin'` is required.
```javascript title="/pages/admin.js"
import { getSession } from "next-auth/react"
export default function Page() {
const session = await getSession({ req })
if (session && session.user.role === "admin") {
return (
<div>
<h1>Admin</h1>
<p>Welcome to the Admin Portal!</p>
</div>
)
} else {
return (
<div>
<h1>You are not authorized to view this page!</h1>
</div>
)
}
}
```
Then it is up to you how you manage your roles, either through direct database access or building your own role update API.

View File

@@ -16,4 +16,4 @@ sidebar_label: Email options
See our guides on magic links authentication for further tips on how to customize this provider: See our guides on magic links authentication for further tips on how to customize this provider:
- [Tutorial](/getting-started/email-tutorial) - [Tutorial](/getting-started/email-tutorial)
- [Guide deep-dive](/guides/providers/email) - [Guide deep-dive](guides/providers/email)

View File

@@ -139,10 +139,9 @@ Prisma supports MongoDB, and so does Auth.js. Following the instructions of the
id String @id @default(auto()) @map("_id") @db.ObjectId id String @id @default(auto()) @map("_id") @db.ObjectId
``` ```
2. The Native database type attribute to `@db.String` from `@db.Text` and userId to `@db.ObjectId`. 2. The Native database type attribute to `@db.String` from `@db.Text`.
```prisma ```prisma
user_id String @db.ObjectId
refresh_token String? @db.String refresh_token String? @db.String
access_token String? @db.String access_token String? @db.String
id_token String? @db.String id_token String? @db.String

View File

@@ -32,6 +32,7 @@ Now that we're ready, let's create a new Xata project using our next-auth schema
```json title="schema.json" ```json title="schema.json"
{ {
"formatVersion": "",
"tables": [ "tables": [
{ {
"name": "nextauth_users", "name": "nextauth_users",

View File

@@ -0,0 +1,25 @@
---
title: Overview
sidebar_label: Overview
sidebar_position: 0
---
## Core
## Providers
- OAuth/OIDC
- Email/Passwordless
- Credentials
## Database Adapters
## Frameworks
- Next.js
- SvelteKit
- SolidStart
- Remix
- Nuxt
- Gatsby
- etc.

View File

@@ -62,7 +62,7 @@ const docusaurusConfig = {
position: "left", position: "left",
}, },
{ {
to: "/reference/core", to: "/reference/core/modules/main",
// TODO: change to this when the overview page looks better. // TODO: change to this when the overview page looks better.
// to: "/reference", // to: "/reference",
activeBasePath: "/reference", activeBasePath: "/reference",
@@ -101,7 +101,7 @@ const docusaurusConfig = {
announcementBar: { announcementBar: {
id: "new-major-announcement", id: "new-major-announcement",
content: content:
"<a target='_blank' rel='noopener noreferrer' href='https://next-auth.js.org'>NextAuth.js</a> is becoming Auth.js! 🎉 We're creating Authentication for the Web. Everyone included. Starting with SvelteKit, check out <a href='/reference/sveltekit'>the docs</a>. Note, this site is under active development.", "<a target='_blank' rel='noopener noreferrer' href='https://next-auth.js.org'>NextAuth.js</a> is becoming Auth.js! 🎉 We're creating Authentication for the Web. Everyone included. Starting with SvelteKit, check out <a href='/reference/sveltekit'>the docs</a>.",
backgroundColor: "#000", backgroundColor: "#000",
textColor: "#fff", textColor: "#fff",
}, },
@@ -182,7 +182,10 @@ const docusaurusConfig = {
lastVersion: "current", lastVersion: "current",
showLastUpdateAuthor: true, showLastUpdateAuthor: true,
showLastUpdateTime: true, showLastUpdateTime: true,
remarkPlugins: [require("@sapphire/docusaurus-plugin-npm2yarn2pnpm").npm2yarn2pnpm], remarkPlugins: [
require("@sapphire/docusaurus-plugin-npm2yarn2pnpm").npm2yarn2pnpm,
require("remark-github"),
],
versions: { versions: {
current: { current: {
label: "experimental", label: "experimental",
@@ -201,14 +204,20 @@ const docusaurusConfig = {
{ {
...typedocConfig, ...typedocConfig,
id: "core", id: "core",
plugin: [require.resolve("./typedoc-mdn-links")], plugin: ["./tyepdoc"],
watch: process.env.TYPEDOC_WATCH, entryPoints: [
entryPoints: ["index.ts", "adapters.ts", "errors.ts", "jwt.ts", "types.ts"].map((e) => `${coreSrc}/${e}`).concat(providers), "index.ts",
"adapters.ts",
"errors.ts",
"jwt.ts",
"types.ts",
]
.map((e) => `${coreSrc}/${e}`)
.concat(providers),
tsconfig: "../packages/core/tsconfig.json", tsconfig: "../packages/core/tsconfig.json",
out: "reference/core", out: "reference/03-core",
sidebar: { watch: process.env.TYPEDOC_WATCH,
indexLabel: "index", includeExtension: false,
},
}, },
], ],
[ [
@@ -216,14 +225,14 @@ const docusaurusConfig = {
{ {
...typedocConfig, ...typedocConfig,
id: "sveltekit", id: "sveltekit",
plugin: [require.resolve("./typedoc-mdn-links")], plugin: ["./tyepdoc"],
watch: process.env.TYPEDOC_WATCH, entryPoints: ["index.ts", "client.ts"].map(
entryPoints: ["index.ts", "client.ts"].map((e) => `../packages/frameworks-sveltekit/src/lib/${e}`), (e) => `../packages/frameworks-sveltekit/src/lib/${e}`
),
tsconfig: "../packages/frameworks-sveltekit/tsconfig.json", tsconfig: "../packages/frameworks-sveltekit/tsconfig.json",
out: "reference/sveltekit", out: "reference/04-sveltekit",
sidebar: { watch: process.env.TYPEDOC_WATCH,
indexLabel: "index", includeExtension: false,
},
}, },
], ],
], ],

View File

@@ -3,7 +3,7 @@
"repository": "https://github.com/nextauthjs/next-auth", "repository": "https://github.com/nextauthjs/next-auth",
"name": "docs", "name": "docs",
"scripts": { "scripts": {
"start": "TYPEDOC_WATCH=true docusaurus start --no-open", "start": "TYPEDOC_WATCH=true docusaurus start --no-open --port 8000",
"dev": "pnpm providers && pnpm snippets && pnpm start", "dev": "pnpm providers && pnpm snippets && pnpm start",
"build": "pnpm providers && docusaurus build", "build": "pnpm providers && docusaurus build",
"docusaurus": "docusaurus", "docusaurus": "docusaurus",
@@ -27,6 +27,7 @@
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0", "react-dom": "^18.2.0",
"react-marquee-slider": "^1.1.5", "react-marquee-slider": "^1.1.5",
"remark-github": "10.1.0",
"styled-components": "5.3.6" "styled-components": "5.3.6"
}, },
"devDependencies": { "devDependencies": {
@@ -36,9 +37,7 @@
"@docusaurus/preset-classic": "2.2.0", "@docusaurus/preset-classic": "2.2.0",
"@docusaurus/theme-common": "2.2.0", "@docusaurus/theme-common": "2.2.0",
"@docusaurus/types": "2.2.0", "@docusaurus/types": "2.2.0",
"docusaurus-plugin-typedoc": "1.0.0-next.2", "docusaurus-plugin-typedoc": "^0.18.0"
"typedoc": "^0.23.24",
"typedoc-plugin-markdown": "4.0.0-next.2"
}, },
"browserslist": { "browserslist": {
"production": [ "production": [

View File

@@ -14,28 +14,61 @@ module.exports = {
}, },
], ],
referenceSidebar: [ referenceSidebar: [
"reference/index",
{ {
type: "category", type: "category",
label: "@auth/core", label: "@auth/core",
link: { type: "doc", id: "reference/core/index" }, link: {
items: [{ type: "autogenerated", dirName: "reference/core" }], type: "doc",
id: "reference/core/modules/main",
},
items: [
{
type: "autogenerated",
dirName: "reference/03-core/modules",
// See: https://github.com/facebook/docusaurus/issues/5689
// exclude: ["index"],
},
{
type: "category",
label: "Reflections",
collapsed: true,
className: "reflection-category", // See src/index.css
items: [{ type: "autogenerated", dirName: "reference/03-core" }],
},
],
}, },
{ {
type: "category", type: "category",
label: "@auth/sveltekit", label: "@auth/sveltekit",
link: { type: "doc", id: "reference/sveltekit/index" }, link: { type: "doc", id: "reference/sveltekit/modules/main" },
items: [{ type: "autogenerated", dirName: "reference/sveltekit" }], items: [
{ type: "autogenerated", dirName: "reference/04-sveltekit/modules" },
{
type: "category",
label: "Reflections",
collapsed: true,
className: "reflection-category", // See src/index.css
items: [{ type: "autogenerated", dirName: "reference/04-sveltekit" }],
},
],
}, },
{ {
type: "category", type: "category",
label: "@auth/solid-start", label: "@auth/solid-start",
link: { type: "doc", id: "reference/solidstart/index" }, link: {
items: [{ type: "autogenerated", dirName: "reference/04-solidstart" }], type: "doc",
id: "reference/solidstart/index",
},
items: ["reference/solidstart/client", "reference/solidstart/protected"],
}, },
{ {
type: "category", type: "category",
label: "@auth/nextjs", label: "@auth/nextjs",
link: { type: "doc", id: "reference/nextjs/index" }, link: {
type: "doc",
id: "reference/nextjs/index",
},
items: [ items: [
"reference/nextjs/client", "reference/nextjs/client",
{ {

View File

@@ -272,4 +272,27 @@ html[data-theme="dark"] #carbonads > span {
html[data-theme="dark"] #carbonads .carbon-poweredby { html[data-theme="dark"] #carbonads .carbon-poweredby {
color: #aaa; color: #aaa;
background: #1e2021; background: #1e2021;
} }
/*
This is a hack to hide the "Reflection" category and "main" module from the sidebar.
This is because:
1. opening any page under the "Reflection" category would hide the entire sidebar.
2. the "main" module would show up twice.
See sidebars.js
*/
.reflection-category,
.theme-doc-sidebar-item-link-level-2 [href="/reference/core/modules/main"],
.theme-doc-sidebar-item-link-level-2
[href="/reference/sveltekit/modules/main"] {
display: none;
}
/*
HACK: to hide the "Classes" header and duplicate items together with the "typedoc-plugin-markdown" patch.
See: https://github.com/TypeStrong/typedoc/issues/2006
*/
/* h3.anchor + p:has(code, strong), */ /** hack did not work as it hides property types elsewhere */
#classes {
display: none;
}

View File

@@ -1,7 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 99.881 86.449">
<g id="asgardeo-trifactor-logo-dark-16x40" transform="translate(-553.024 -388.98)">
<path id="Path_264" data-name="Path 264" d="M743.533,388.98l9.161,15.892-10.153,17.6h20.306l9.209,15.892H714.97Z" transform="translate(-119.151 0)" fill="#ff7300"/>
<path id="Path_265" data-name="Path 265" d="M705.95,438.364l9.209-15.892h20.306l-10.153-17.6,9.162-15.892,28.6,49.393Z" transform="translate(-152.926 0.009)" fill="#ff7300"/>
<path id="Path_266" data-name="Path 266" d="M749.175,446.183l-10.153-17.6-10.2,17.6H710.46l28.6-49.393,28.515,49.393Z" transform="translate(-136.043 29.246)"/>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 711 B

View File

@@ -1,7 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 99.881 86.449">
<g id="asgardeo-trifactor-logo-light-16x40" transform="translate(-553.024 -388.98)">
<path id="Path_264" data-name="Path 264" d="M743.533,388.98l9.161,15.892-10.153,17.6h20.306l9.209,15.892H714.97Z" transform="translate(-119.151)" fill="#ff7300"/>
<path id="Path_265" data-name="Path 265" d="M705.95,438.364l9.209-15.892h20.306l-10.153-17.6,9.162-15.892,28.6,49.393Z" transform="translate(-152.926 0.009)" fill="#ff7300"/>
<path id="Path_266" data-name="Path 266" d="M749.175,446.183l-10.153-17.6-10.2,17.6H710.46l28.6-49.393,28.515,49.393Z" transform="translate(-136.043 29.246)" fill="#fff"/>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 722 B

View File

@@ -1,5 +0,0 @@
<svg width="32" height="32" viewBox="0 0 100 100" fill="none" xmlns="http://www.w3.org/2000/svg">
<title>Notion icon</title>
<path d="M6.017 4.313l55.333 -4.087c6.797 -0.583 8.543 -0.19 12.817 2.917l17.663 12.443c2.913 2.14 3.883 2.723 3.883 5.053v68.243c0 4.277 -1.553 6.807 -6.99 7.193L24.467 99.967c-4.08 0.193 -6.023 -0.39 -8.16 -3.113L3.3 79.94c-2.333 -3.113 -3.3 -5.443 -3.3 -8.167V11.113c0 -3.497 1.553 -6.413 6.017 -6.8z" fill="#fff"/>
<path fill-rule="evenodd" clip-rule="evenodd" d="M61.35 0.227l-55.333 4.087C1.553 4.7 0 7.617 0 11.113v60.66c0 2.723 0.967 5.053 3.3 8.167l13.007 16.913c2.137 2.723 4.08 3.307 8.16 3.113l64.257 -3.89c5.433 -0.387 6.99 -2.917 6.99 -7.193V20.64c0 -2.21 -0.873 -2.847 -3.443 -4.733L74.167 3.143c-4.273 -3.107 -6.02 -3.5 -12.817 -2.917zM25.92 19.523c-5.247 0.353 -6.437 0.433 -9.417 -1.99L8.927 11.507c-0.77 -0.78 -0.383 -1.753 1.557 -1.947l53.193 -3.887c4.467 -0.39 6.793 1.167 8.54 2.527l9.123 6.61c0.39 0.197 1.36 1.36 0.193 1.36l-54.933 3.307 -0.68 0.047zM19.803 88.3V30.367c0 -2.53 0.777 -3.697 3.103 -3.893L86 22.78c2.14 -0.193 3.107 1.167 3.107 3.693v57.547c0 2.53 -0.39 4.67 -3.883 4.863l-60.377 3.5c-3.493 0.193 -5.043 -0.97 -5.043 -4.083zm59.6 -54.827c0.387 1.75 0 3.5 -1.75 3.7l-2.91 0.577v42.773c-2.527 1.36 -4.853 2.137 -6.797 2.137 -3.107 0 -3.883 -0.973 -6.21 -3.887l-19.03 -29.94v28.967l6.02 1.363s0 3.5 -4.857 3.5l-13.39 0.777c-0.39 -0.78 0 -2.723 1.357 -3.11l3.497 -0.97v-38.3L30.48 40.667c-0.39 -1.75 0.58 -4.277 3.3 -4.473l14.367 -0.967 19.8 30.327v-26.83l-5.047 -0.58c-0.39 -2.143 1.163 -3.7 3.103 -3.89l13.4 -0.78z" fill="#000"/>
</svg>

Before

Width:  |  Height:  |  Size: 1.6 KiB

View File

@@ -1,16 +1,23 @@
{ {
"excludeNotDocumented": true,
"$schema": "https://typedoc.org/schema.json", "$schema": "https://typedoc.org/schema.json",
"cleanOutputDir": true, "allReflectionsHaveOwnDocument": true,
"disableSources": true, "disableSources": true,
"hideBreadcrumbs": true,
"excludeExternals": true, "excludeExternals": true,
"excludeInternal": true, "excludeInternal": true,
"excludeNotDocumented": true,
"excludePrivate": true, "excludePrivate": true,
"cleanOutputDir": true,
"excludeProtected": true, "excludeProtected": true,
"hideHierarchy": true,
"gitRevision": "main", "gitRevision": "main",
"hideBreadcrumbs": true,
"hideGenerator": true, "hideGenerator": true,
"intentionallyNotExported": [
"ReturnTypes",
"CallbackParameters",
"JsonValue"
],
"readme": "none",
"sort": ["kind", "static-first", "required-first", "alphabetical"],
"kindSortOrder": [ "kindSortOrder": [
"Function", "Function",
"TypeAlias", "TypeAlias",
@@ -34,13 +41,5 @@
"IndexSignature", "IndexSignature",
"GetSignature", "GetSignature",
"SetSignature" "SetSignature"
], ]
"readme": "none", }
"sort": [
"kind",
"static-first",
"required-first",
"alphabetical"
],
"symbolsWithOwnFile": "none"
}

View File

@@ -18,8 +18,7 @@
"lint": "prettier --check .", "lint": "prettier --check .",
"format": "prettier --write .", "format": "prettier --write .",
"release": "release", "release": "release",
"version:pr": "node ./config/version-pr", "version:pr": "node ./config/version-pr"
"e2e": "turbo run e2e --filter=next-auth-app"
}, },
"devDependencies": { "devDependencies": {
"@actions/core": "^1.10.0", "@actions/core": "^1.10.0",
@@ -41,6 +40,8 @@
"prettier": "2.8.1", "prettier": "2.8.1",
"prettier-plugin-svelte": "^2.8.1", "prettier-plugin-svelte": "^2.8.1",
"turbo": "1.6.3", "turbo": "1.6.3",
"typedoc": "^0.23.22",
"typedoc-plugin-markdown": "^3.14.0",
"typescript": "4.9.4" "typescript": "4.9.4"
}, },
"engines": { "engines": {
@@ -62,6 +63,7 @@
"undici": "5.11.0" "undici": "5.11.0"
}, },
"patchedDependencies": { "patchedDependencies": {
"typedoc-plugin-markdown@3.14.0": "patches/typedoc-plugin-markdown@3.14.0.patch",
"@balazsorban/monorepo-release@0.1.8": "patches/@balazsorban__monorepo-release@0.1.8.patch" "@balazsorban/monorepo-release@0.1.8": "patches/@balazsorban__monorepo-release@0.1.8.patch"
} }
} }

View File

@@ -1,7 +1,7 @@
{ {
"name": "@next-auth/dynamodb-adapter", "name": "@next-auth/dynamodb-adapter",
"repository": "https://github.com/nextauthjs/next-auth", "repository": "https://github.com/nextauthjs/next-auth",
"version": "1.2.0", "version": "1.0.6",
"description": "AWS DynamoDB adapter for next-auth.", "description": "AWS DynamoDB adapter for next-auth.",
"keywords": [ "keywords": [
"next-auth", "next-auth",

View File

@@ -1,11 +0,0 @@
import { initializeApp, getApps, FirebaseOptions } from "firebase/app"
export default function getFirebase(firebaseOptions: FirebaseOptions) {
const apps = getApps()
const app = apps.find((app) => app.name === firebaseOptions.projectId)
if (app) {
return app
} else {
return initializeApp(firebaseOptions)
}
}

View File

@@ -25,7 +25,6 @@ import type {
} from "next-auth/adapters" } from "next-auth/adapters"
import { getConverter } from "./converter" import { getConverter } from "./converter"
import getFirebase from "./getFirebase"
export type IndexableObject = Record<string, unknown> export type IndexableObject = Record<string, unknown>
@@ -40,7 +39,7 @@ export function FirestoreAdapter({
emulator, emulator,
...firebaseOptions ...firebaseOptions
}: FirebaseOptions & FirestoreAdapterOptions): Adapter { }: FirebaseOptions & FirestoreAdapterOptions): Adapter {
const firebaseApp = getFirebase(firebaseOptions) const firebaseApp = initializeApp(firebaseOptions)
const db = getFirestore(firebaseApp) const db = getFirestore(firebaseApp)
if (emulator) { if (emulator) {

View File

@@ -1,6 +1,6 @@
{ {
"name": "@next-auth/xata-adapter", "name": "@next-auth/xata-adapter",
"version": "0.2.2", "version": "0.2.0",
"description": "Xata adapter for next-auth.", "description": "Xata adapter for next-auth.",
"homepage": "https://authjs.dev", "homepage": "https://authjs.dev",
"repository": "https://github.com/nextauthjs/next-auth", "repository": "https://github.com/nextauthjs/next-auth",
@@ -43,4 +43,4 @@
"jest": { "jest": {
"preset": "@next-auth/adapter-test/jest" "preset": "@next-auth/adapter-test/jest"
} }
} }

View File

@@ -1,6 +1,6 @@
{ {
"name": "@auth/core", "name": "@auth/core",
"version": "0.3.0", "version": "0.2.5",
"description": "Authentication for the Web.", "description": "Authentication for the Web.",
"keywords": [ "keywords": [
"authentication", "authentication",
@@ -69,7 +69,7 @@
"preact-render-to-string": "5.2.3" "preact-render-to-string": "5.2.3"
}, },
"peerDependencies": { "peerDependencies": {
"nodemailer": "^6.8.0" "nodemailer": "6.8.0"
}, },
"peerDependenciesMeta": { "peerDependenciesMeta": {
"nodemailer": { "nodemailer": {
@@ -77,11 +77,10 @@
} }
}, },
"scripts": { "scripts": {
"build": "pnpm css && pnpm providers && tsc", "build": "pnpm css && tsc",
"clean": "rm -rf *.js *.d.ts* lib providers", "clean": "rm -rf *.js *.d.ts* lib providers",
"css": "node scripts/generate-css", "css": "node scripts/generate-css",
"dev": "pnpm css && pnpm providers && tsc -w", "dev": "pnpm css && tsc -w"
"providers": "node scripts/generate-providers"
}, },
"devDependencies": { "devDependencies": {
"@next-auth/tsconfig": "workspace:*", "@next-auth/tsconfig": "workspace:*",

View File

@@ -1,18 +0,0 @@
import { join } from "path"
import { readdirSync, writeFileSync } from "fs"
const providersPath = join(process.cwd(), "src/providers")
const files = readdirSync(providersPath, "utf8")
const providers = files.map((file) => {
const strippedProviderName = file.substring(0, file.indexOf("."))
return `"${strippedProviderName}"`
}).filter((provider) => provider !== '"oauth-types"' && provider !== '"index"')
const result = `
// THIS FILE IS AUTOGENERATED. DO NOT EDIT.
export type OAuthProviderType =
| ${providers.join("\n | ")}`
writeFileSync(join(providersPath, "oauth-types.ts"), result)

View File

@@ -27,14 +27,14 @@
* ## Resources * ## Resources
* *
* - [Getting started](https://authjs.dev/getting-started/introduction) * - [Getting started](https://authjs.dev/getting-started/introduction)
* - [Most common use case guides](https://authjs.dev/guides) * - [Most common use case guides](https://authjs.dev/guides/overview)
* *
* @module index * @module main
*/ */
import { assertConfig } from "./lib/assert.js" import { assertConfig } from "./lib/assert.js"
import { ErrorPageLoop } from "./errors.js" import { ErrorPageLoop } from "./errors.js"
import { AuthInternal, skipCSRFCheck } from "./lib/index.js" import { AuthInternal } from "./lib/index.js"
import renderPage from "./lib/pages/index.js" import renderPage from "./lib/pages/index.js"
import { logger, setLogger, type LoggerInstance } from "./lib/utils/logger.js" import { logger, setLogger, type LoggerInstance } from "./lib/utils/logger.js"
import { toInternalRequest, toResponse } from "./lib/web.js" import { toInternalRequest, toResponse } from "./lib/web.js"
@@ -51,8 +51,6 @@ import type {
import type { Provider } from "./providers/index.js" import type { Provider } from "./providers/index.js"
import { JWTOptions } from "./jwt.js" import { JWTOptions } from "./jwt.js"
export { skipCSRFCheck }
/** /**
* Core functionality provided by Auth.js. * Core functionality provided by Auth.js.
* *
@@ -300,3 +298,14 @@ export interface AuthConfig {
trustHost?: boolean trustHost?: boolean
skipCSRFCheck?: typeof skipCSRFCheck skipCSRFCheck?: typeof skipCSRFCheck
} }
/**
* :::danger
* This option is inteded for framework authors.
* :::
*
* Auth.js comes with built-in {@link https://authjs.dev/concepts/security#csrf CSRF} protection, but
* if you are implementing a framework that is already protected against CSRF attacks, you can skip this check by
* passing this value to {@link AuthConfig.skipCSRFCheck}.
*/
export const skipCSRFCheck = Symbol("skip-csrf-check")

View File

@@ -1,4 +1,5 @@
import { UnknownAction } from "../errors.js" import { UnknownAction } from "../errors.js"
import { skipCSRFCheck } from "../index.js"
import { SessionStore } from "./cookie.js" import { SessionStore } from "./cookie.js"
import { init } from "./init.js" import { init } from "./init.js"
import renderPage from "./pages/index.js" import renderPage from "./pages/index.js"
@@ -71,9 +72,9 @@ export async function AuthInternal<
if (pages.signIn) { if (pages.signIn) {
let signinUrl = `${pages.signIn}${ let signinUrl = `${pages.signIn}${
pages.signIn.includes("?") ? "&" : "?" pages.signIn.includes("?") ? "&" : "?"
}${new URLSearchParams({ callbackUrl: options.callbackUrl })}` }callbackUrl=${encodeURIComponent(options.callbackUrl)}`
if (error) if (error)
signinUrl = `${signinUrl}&${new URLSearchParams({ error })}` signinUrl = `${signinUrl}&error=${encodeURIComponent(error)}`
return { redirect: signinUrl, cookies } return { redirect: signinUrl, cookies }
} }
@@ -185,14 +186,3 @@ export async function AuthInternal<
} }
throw new UnknownAction(`Cannot handle action: ${action}`) throw new UnknownAction(`Cannot handle action: ${action}`)
} }
/**
* :::danger
* This option is intended for framework authors.
* :::
*
* Auth.js comes with built-in {@link https://authjs.dev/concepts/security#csrf CSRF} protection, but
* if you are implementing a framework that is already protected against CSRF attacks, you can skip this check by
* passing this value to {@link AuthConfig.skipCSRFCheck}.
*/
export const skipCSRFCheck = Symbol("skip-csrf-check")

View File

@@ -1,7 +1,7 @@
import * as checks from "./checks.js"
import * as o from "oauth4webapi" import * as o from "oauth4webapi"
import type { import type {
CookiesOptions,
InternalOptions, InternalOptions,
RequestInternal, RequestInternal,
ResponseInternal, ResponseInternal,
@@ -58,10 +58,10 @@ export async function getAuthorizationUrl(
const cookies: Cookie[] = [] const cookies: Cookie[] = []
const state = await checks.state.create(options) if (provider.checks?.includes("state")) {
if (state) { const { value, raw } = await createState(options)
authParams.set("state", state.value) authParams.set("state", raw)
cookies.push(state.cookie) cookies.push(value)
} }
if (provider.checks?.includes("pkce")) { if (provider.checks?.includes("pkce")) {
@@ -70,17 +70,17 @@ export async function getAuthorizationUrl(
// a random `nonce` must be used for CSRF protection. // a random `nonce` must be used for CSRF protection.
provider.checks = ["nonce"] provider.checks = ["nonce"]
} else { } else {
const { value, cookie } = await checks.pkce.create(options) const { code_challenge, pkce } = await createPKCE(options)
authParams.set("code_challenge", value) authParams.set("code_challenge", code_challenge)
authParams.set("code_challenge_method", "S256") authParams.set("code_challenge_method", "S256")
cookies.push(cookie) cookies.push(pkce)
} }
} }
const nonce = await checks.nonce.create(options) if (provider.checks?.includes("nonce")) {
if (nonce) { const nonce = await createNonce(options)
authParams.set("nonce", nonce.value) authParams.set("nonce", nonce.value)
cookies.push(nonce.cookie) cookies.push(nonce)
} }
// TODO: This does not work in normalizeOAuth because authorization endpoint can come from discovery // TODO: This does not work in normalizeOAuth because authorization endpoint can come from discovery
@@ -90,5 +90,54 @@ export async function getAuthorizationUrl(
} }
logger.debug("authorization url is ready", { url, cookies, provider }) logger.debug("authorization url is ready", { url, cookies, provider })
return { redirect: url.toString(), cookies } return { redirect: url, cookies }
}
/** Returns a signed cookie. */
export async function signCookie(
type: keyof CookiesOptions,
value: string,
maxAge: number,
options: InternalOptions<"oauth">
): Promise<Cookie> {
const { cookies, jwt, logger } = options
logger.debug(`CREATE_${type.toUpperCase()}`, { value, maxAge })
const expires = new Date()
expires.setTime(expires.getTime() + maxAge * 1000)
return {
name: cookies[type].name,
value: await jwt.encode({ ...jwt, maxAge, token: { value } }),
options: { ...cookies[type].options, expires },
}
}
const STATE_MAX_AGE = 60 * 15 // 15 minutes in seconds
async function createState(options: InternalOptions<"oauth">) {
const raw = o.generateRandomState()
const maxAge = STATE_MAX_AGE
const value = await signCookie("state", raw, maxAge, options)
return { value, raw }
}
const PKCE_MAX_AGE = 60 * 15 // 15 minutes in seconds
async function createPKCE(options: InternalOptions<"oauth">) {
const code_verifier = o.generateRandomCodeVerifier()
const code_challenge = await o.calculatePKCECodeChallenge(code_verifier)
const maxAge = PKCE_MAX_AGE
const pkce = await signCookie(
"pkceCodeVerifier",
code_verifier,
maxAge,
options
)
return { code_challenge, pkce }
}
const NONCE_MAX_AGE = 60 * 15 // 15 minutes in seconds
async function createNonce(options: InternalOptions<"oauth">) {
const raw = o.generateRandomNonce()
const maxAge = NONCE_MAX_AGE
return await signCookie("nonce", raw, maxAge, options)
} }

View File

@@ -1,6 +1,8 @@
import * as checks from "./checks.js"
import * as o from "oauth4webapi" import * as o from "oauth4webapi"
import { OAuthCallbackError, OAuthProfileParseError } from "../../errors.js" import { OAuthCallbackError, OAuthProfileParseError } from "../../errors.js"
import { useNonce } from "./nonce-handler.js"
import { usePKCECodeVerifier } from "./pkce-handler.js"
import { useState } from "./state-handler.js"
import type { import type {
InternalOptions, InternalOptions,
@@ -71,7 +73,7 @@ export async function handleOAuth(
const resCookies: Cookie[] = [] const resCookies: Cookie[] = []
const state = await checks.state.use(cookies, resCookies, options) const state = await useState(cookies, resCookies, options)
const parameters = o.validateAuthResponse( const parameters = o.validateAuthResponse(
as, as,
@@ -89,7 +91,7 @@ export async function handleOAuth(
throw new OAuthCallbackError(parameters.error) throw new OAuthCallbackError(parameters.error)
} }
const codeVerifier = await checks.pkce.use( const codeVerifier = await usePKCECodeVerifier(
cookies?.[options.cookies.pkceCodeVerifier.name], cookies?.[options.cookies.pkceCodeVerifier.name],
options options
) )
@@ -97,15 +99,12 @@ export async function handleOAuth(
if (codeVerifier) resCookies.push(codeVerifier.cookie) if (codeVerifier) resCookies.push(codeVerifier.cookie)
// TODO: // TODO:
const nonce = await checks.nonce.use( const nonce = await useNonce(cookies?.[options.cookies.nonce.name], options)
cookies?.[options.cookies.nonce.name],
options
)
if (nonce && provider.type === "oidc") { if (nonce && provider.type === "oidc") {
resCookies.push(nonce.cookie) resCookies.push(nonce.cookie)
} }
let codeGrantResponse = await o.authorizationCodeGrantRequest( const codeGrantResponse = await o.authorizationCodeGrantRequest(
as, as,
client, client,
parameters, parameters,
@@ -113,12 +112,6 @@ export async function handleOAuth(
codeVerifier?.codeVerifier ?? "auth" // TODO: review fallback code verifier codeVerifier?.codeVerifier ?? "auth" // TODO: review fallback code verifier
) )
if (provider.token?.conform) {
codeGrantResponse =
(await provider.token.conform(codeGrantResponse.clone())) ??
codeGrantResponse
}
let challenges: o.WWWAuthenticateChallenge[] | undefined let challenges: o.WWWAuthenticateChallenge[] | undefined
if ((challenges = o.parseWwwAuthenticateChallenges(codeGrantResponse))) { if ((challenges = o.parseWwwAuthenticateChallenges(codeGrantResponse))) {
for (const challenge of challenges) { for (const challenge of challenges) {
@@ -203,7 +196,7 @@ async function getProfile(
// If we didn't get a response either there was a problem with the provider // If we didn't get a response either there was a problem with the provider
// response *or* the user cancelled the action with the provider. // response *or* the user cancelled the action with the provider.
// //
// Unfortunately, we can't tell which - at least not in a way that works for // Unfortuately, we can't tell which - at least not in a way that works for
// all providers, so we return an empty object; the user should then be // all providers, so we return an empty object; the user should then be
// redirected back to the sign up page. We log the error to help developers // redirected back to the sign up page. We log the error to help developers
// who might be trying to debug this when configuring a new provider. // who might be trying to debug this when configuring a new provider.

View File

@@ -1,155 +0,0 @@
import * as o from "oauth4webapi"
import * as jwt from "../../jwt.js"
import type {
InternalOptions,
RequestInternal,
CookiesOptions,
} from "../../types.js"
import type { Cookie } from "../cookie.js"
import { InvalidState } from "../../errors.js"
/** Returns a signed cookie. */
export async function signCookie(
type: keyof CookiesOptions,
value: string,
maxAge: number,
options: InternalOptions<"oauth">
): Promise<Cookie> {
const { cookies, logger } = options
logger.debug(`CREATE_${type.toUpperCase()}`, { value, maxAge })
const expires = new Date()
expires.setTime(expires.getTime() + maxAge * 1000)
return {
name: cookies[type].name,
value: await jwt.encode({ ...options.jwt, maxAge, token: { value } }),
options: { ...cookies[type].options, expires },
}
}
const PKCE_MAX_AGE = 60 * 15 // 15 minutes in seconds
export const pkce = {
async create(options: InternalOptions<"oauth">) {
const code_verifier = o.generateRandomCodeVerifier()
const value = await o.calculatePKCECodeChallenge(code_verifier)
const maxAge = PKCE_MAX_AGE
const cookie = await signCookie(
"pkceCodeVerifier",
code_verifier,
maxAge,
options
)
return { cookie, value }
},
/**
* Returns code_verifier if provider uses PKCE,
* and clears the container cookie afterwards.
*/
async use(
codeVerifier: string | undefined,
options: InternalOptions<"oauth">
): Promise<{ codeVerifier: string; cookie: Cookie } | undefined> {
const { cookies, provider } = options
if (!provider?.checks?.includes("pkce") || !codeVerifier) {
return
}
const pkce = (await jwt.decode({
...options.jwt,
token: codeVerifier,
})) as any
return {
codeVerifier: pkce?.value ?? undefined,
cookie: {
name: cookies.pkceCodeVerifier.name,
value: "",
options: { ...cookies.pkceCodeVerifier.options, maxAge: 0 },
},
}
},
}
const STATE_MAX_AGE = 60 * 15 // 15 minutes in seconds
export const state = {
async create(options: InternalOptions<"oauth">) {
if (!options.provider.checks.includes("state")) return
// TODO: support customizing the state
const value = o.generateRandomState()
const maxAge = STATE_MAX_AGE
const cookie = await signCookie("state", value, maxAge, options)
return { cookie, value }
},
/**
* Returns state from the saved cookie
* if the provider supports states,
* and clears the container cookie afterwards.
*/
async use(
cookies: RequestInternal["cookies"],
resCookies: Cookie[],
options: InternalOptions<"oauth">
): Promise<string | undefined> {
const { provider, jwt } = options
if (!provider.checks.includes("state")) return
const state = cookies?.[options.cookies.state.name]
if (!state) throw new InvalidState("State was missing from the cookies.")
// IDEA: Let the user do something with the returned state
const value = (await jwt.decode({ ...options.jwt, token: state })) as any
if (!value?.value) throw new InvalidState("Could not parse state cookie.")
// Clear the state cookie after use
resCookies.push({
name: options.cookies.state.name,
value: "",
options: { ...options.cookies.state.options, maxAge: 0 },
})
return value.value
},
}
const NONCE_MAX_AGE = 60 * 15 // 15 minutes in seconds
export const nonce = {
async create(options: InternalOptions<"oauth">) {
if (!options.provider.checks.includes("nonce")) return
const value = o.generateRandomNonce()
const maxAge = NONCE_MAX_AGE
const cookie = await signCookie("nonce", value, maxAge, options)
return { cookie, value }
},
/**
* Returns nonce from if the provider supports nonce,
* and clears the container cookie afterwards.
*/
async use(
nonce: string | undefined,
options: InternalOptions<"oauth">
): Promise<{ value: string; cookie: Cookie } | undefined> {
const { cookies, provider } = options
if (!provider?.checks?.includes("nonce") || !nonce) {
return
}
const value = (await jwt.decode({ ...options.jwt, token: nonce })) as any
return {
value: value?.value ?? undefined,
cookie: {
name: cookies.nonce.name,
value: "",
options: { ...cookies.nonce.options, maxAge: 0 },
},
}
},
}

View File

@@ -0,0 +1,77 @@
import * as o from "oauth4webapi"
import * as jwt from "../../jwt.js"
import type { InternalOptions } from "../../types.js"
import type { Cookie } from "../cookie.js"
const NONCE_MAX_AGE = 60 * 15 // 15 minutes in seconds
/**
* Returns nonce if the provider supports it
* and saves it in a cookie
*/
export async function createNonce(options: InternalOptions<"oauth">): Promise<
| undefined
| {
value: string
cookie: Cookie
}
> {
const { cookies, logger, provider } = options
if (!provider.checks?.includes("nonce")) {
// Provider does not support nonce, return nothing.
return
}
const nonce = o.generateRandomNonce()
const expires = new Date()
expires.setTime(expires.getTime() + NONCE_MAX_AGE * 1000)
// Encrypt nonce and save it to an encrypted cookie
const encryptedNonce = await jwt.encode({
...options.jwt,
maxAge: NONCE_MAX_AGE,
token: { nonce },
})
logger.debug("CREATE_ENCRYPTED_NONCE", {
nonce,
maxAge: NONCE_MAX_AGE,
})
return {
cookie: {
name: cookies.nonce.name,
value: encryptedNonce,
options: { ...cookies.nonce.options, expires },
},
value: nonce,
}
}
/**
* Returns nonce from if the provider supports nonce,
* and clears the container cookie afterwards.
*/
export async function useNonce(
nonce: string | undefined,
options: InternalOptions<"oauth">
): Promise<{ value: string; cookie: Cookie } | undefined> {
const { cookies, provider } = options
if (!provider?.checks?.includes("nonce") || !nonce) {
return
}
const value = (await jwt.decode({ ...options.jwt, token: nonce })) as any
return {
value: value?.value ?? undefined,
cookie: {
name: cookies.nonce.name,
value: "",
options: { ...cookies.nonce.options, maxAge: 0 },
},
}
}

View File

@@ -0,0 +1,87 @@
import * as o from "oauth4webapi"
import * as jwt from "../../jwt.js"
import type { InternalOptions } from "../../types.js"
import type { Cookie } from "../cookie.js"
const PKCE_CODE_CHALLENGE_METHOD = "S256"
const PKCE_MAX_AGE = 60 * 15 // 15 minutes in seconds
/**
* Returns `code_challenge` and `code_challenge_method`
* and saves them in a cookie.
*/
export async function createPKCE(options: InternalOptions<"oauth">): Promise<
| undefined
| {
code_challenge: string
code_challenge_method: "S256"
cookie: Cookie
}
> {
const { cookies, logger, provider } = options
if (!provider.checks?.includes("pkce")) {
// Provider does not support PKCE, return nothing.
return
}
const code_verifier = o.generateRandomCodeVerifier()
const code_challenge = await o.calculatePKCECodeChallenge(code_verifier)
const maxAge = cookies.pkceCodeVerifier.options.maxAge ?? PKCE_MAX_AGE
const expires = new Date()
expires.setTime(expires.getTime() + maxAge * 1000)
// Encrypt code_verifier and save it to an encrypted cookie
const encryptedCodeVerifier = await jwt.encode({
...options.jwt,
maxAge,
token: { code_verifier },
})
logger.debug("CREATE_PKCE_CHALLENGE_VERIFIER", {
code_challenge,
code_challenge_method: PKCE_CODE_CHALLENGE_METHOD,
code_verifier,
maxAge,
})
return {
code_challenge,
code_challenge_method: PKCE_CODE_CHALLENGE_METHOD,
cookie: {
name: cookies.pkceCodeVerifier.name,
value: encryptedCodeVerifier,
options: { ...cookies.pkceCodeVerifier.options, expires },
},
}
}
/**
* Returns code_verifier if provider uses PKCE,
* and clears the container cookie afterwards.
*/
export async function usePKCECodeVerifier(
codeVerifier: string | undefined,
options: InternalOptions<"oauth">
): Promise<{ codeVerifier: string; cookie: Cookie } | undefined> {
const { cookies, provider } = options
if (!provider?.checks?.includes("pkce") || !codeVerifier) {
return
}
const pkce = (await jwt.decode({
...options.jwt,
token: codeVerifier,
})) as any
return {
codeVerifier: pkce?.value ?? undefined,
cookie: {
name: cookies.pkceCodeVerifier.name,
value: "",
options: { ...cookies.pkceCodeVerifier.options, maxAge: 0 },
},
}
}

View File

@@ -0,0 +1,72 @@
import * as o from "oauth4webapi"
import type { InternalOptions, RequestInternal } from "../../types.js"
import type { Cookie } from "../cookie.js"
import { InvalidState } from "../../errors.js"
const STATE_MAX_AGE = 60 * 15 // 15 minutes in seconds
/** Returns state if the provider supports it */
export async function createState(
options: InternalOptions<"oauth">
): Promise<{ cookie: Cookie; value: string } | undefined> {
const { logger, provider, jwt, cookies } = options
if (!provider.checks?.includes("state")) {
// Provider does not support state, return nothing
return
}
const state = o.generateRandomState()
const maxAge = cookies.state.options.maxAge ?? STATE_MAX_AGE
const encodedState = await jwt.encode({
...jwt,
maxAge,
token: { state },
})
logger.debug("CREATE_STATE", { state, maxAge })
const expires = new Date()
expires.setTime(expires.getTime() + maxAge * 1000)
return {
value: state,
cookie: {
name: cookies.state.name,
value: encodedState,
options: { ...cookies.state.options, expires },
},
}
}
/**
* Returns state from the saved cookie
* if the provider supports states,
* and clears the container cookie afterwards.
*/
export async function useState(
cookies: RequestInternal["cookies"],
resCookies: Cookie[],
options: InternalOptions<"oauth">
): Promise<string | undefined> {
const { provider, jwt } = options
if (!provider.checks.includes("state")) return
const state = cookies?.[options.cookies.state.name]
if (!state) throw new InvalidState("State was missing from the cookies.")
// IDEA: Let the user do something with the returned state
const value = (await jwt.decode({ ...options.jwt, token: state })) as any
if (!value?.value) throw new InvalidState("Could not parse state cookie.")
// Clear the state cookie after use
resCookies.push({
name: options.cookies.state.name,
value: "",
options: { ...options.cookies.state.options, maxAge: 0 },
})
return value.value
}

View File

@@ -96,9 +96,6 @@ function normalizeEndpoint(
// NOTE: This need to be checked when constructing the URL // NOTE: This need to be checked when constructing the URL
// for the authorization, token and userinfo endpoints. // for the authorization, token and userinfo endpoints.
const url = new URL(e?.url ?? "https://authjs.dev") const url = new URL(e?.url ?? "https://authjs.dev")
for (const k in e?.params) { for (const k in e?.params) url.searchParams.set(k, e?.params[k])
if (e?.params && k === "claims") e.params[k] = JSON.stringify(e.params[k]) return { url, request: e?.request }
url.searchParams.set(k, e?.params[k])
}
return { url, request: e?.request, conform: e?.conform }
} }

View File

@@ -53,7 +53,7 @@ export async function callback(params: {
cookies.push(...authorizationResult.cookies) cookies.push(...authorizationResult.cookies)
} }
logger.debug("authorization result", authorizationResult) logger.debug("authroization result", authorizationResult)
const { profile, account, OAuthProfile } = authorizationResult const { profile, account, OAuthProfile } = authorizationResult
@@ -149,7 +149,7 @@ export async function callback(params: {
return { return {
redirect: `${pages.newUser}${ redirect: `${pages.newUser}${
pages.newUser.includes("?") ? "&" : "?" pages.newUser.includes("?") ? "&" : "?"
}${new URLSearchParams({ callbackUrl })}`, }callbackUrl=${encodeURIComponent(callbackUrl)}`,
cookies, cookies,
} }
} }
@@ -256,7 +256,7 @@ export async function callback(params: {
return { return {
redirect: `${pages.newUser}${ redirect: `${pages.newUser}${
pages.newUser.includes("?") ? "&" : "?" pages.newUser.includes("?") ? "&" : "?"
}${new URLSearchParams({ callbackUrl })}`, }callbackUrl=${encodeURIComponent(callbackUrl)}`,
cookies, cookies,
} }
} }
@@ -350,6 +350,6 @@ export async function callback(params: {
logger.error(error) logger.error(error)
url.searchParams.set("error", CallbackRouteError.name) url.searchParams.set("error", CallbackRouteError.name)
url.pathname += "/error" url.pathname += "/error"
return { redirect: url.toString(), cookies } return { redirect: url, cookies }
} }
} }

View File

@@ -7,20 +7,19 @@ export async function handleAuthorized(
params: any, params: any,
{ url, logger, callbacks: { signIn } }: InternalOptions { url, logger, callbacks: { signIn } }: InternalOptions
) { ) {
url.pathname += "/error"
try { try {
const authorized = await signIn(params) const authorized = await signIn(params)
if (!authorized) { if (!authorized) {
url.pathname += "/error"
logger.debug("User not authorized", params) logger.debug("User not authorized", params)
url.searchParams.set("error", "AccessDenied") url.searchParams.set("error", "AccessDenied")
return { status: 403 as const, redirect: url.toString() } return { status: 403 as const, redirect: url }
} }
} catch (e) { } catch (e) {
url.pathname += "/error"
const error = new AuthorizedCallbackError(e as Error) const error = new AuthorizedCallbackError(e as Error)
logger.error(error) logger.error(error)
url.searchParams.set("error", "Configuration") url.searchParams.set("error", "Configuration")
return { status: 500 as const, redirect: url.toString() } return { status: 500 as const, redirect: url }
} }
} }

View File

@@ -54,7 +54,7 @@ export async function signin(
logger.error(error) logger.error(error)
url.searchParams.set("error", error.name) url.searchParams.set("error", error.name)
url.pathname += "/error" url.pathname += "/error"
return { redirect: url.toString() } return { redirect: url }
} }
} }

View File

@@ -8,7 +8,7 @@ import type { SessionStore } from "../cookie.js"
* If the session strategy is database, * If the session strategy is database,
* The session is also deleted from the database. * The session is also deleted from the database.
* In any case, the session cookie is cleared and * In any case, the session cookie is cleared and
* {@link EventCallbacks.signOut} is emitted. * an `events.signOut` is emitted.
*/ */
export async function signout( export async function signout(
sessionStore: SessionStore, sessionStore: SessionStore,

View File

@@ -76,22 +76,27 @@ export function toResponse(res: ResponseInternal): Response {
res.cookies?.forEach((cookie) => { res.cookies?.forEach((cookie) => {
const { name, value, options } = cookie const { name, value, options } = cookie
const cookieHeader = serialize(name, value, options) const cookieHeader = serialize(name, value, options)
if (headers.has("Set-Cookie")) headers.append("Set-Cookie", cookieHeader) if (headers.has("Set-Cookie")) {
else headers.set("Set-Cookie", cookieHeader) headers.append("Set-Cookie", cookieHeader)
} else {
headers.set("Set-Cookie", cookieHeader)
}
// headers.set("Set-Cookie", cookieHeader) // TODO: Remove. Seems to be a bug with Headers in the runtime // headers.set("Set-Cookie", cookieHeader) // TODO: Remove. Seems to be a bug with Headers in the runtime
}) })
let body = res.body const body =
headers.get("content-type") === "application/json"
? JSON.stringify(res.body)
: res.body
if (headers.get("content-type") === "application/json") const response = new Response(body, {
body = JSON.stringify(res.body) headers,
else if (headers.get("content-type") === "application/x-www-form-urlencoded") status: res.redirect ? 302 : res.status ?? 200,
body = new URLSearchParams(res.body).toString() })
const status = res.redirect ? 302 : res.status ?? 200 if (res.redirect) {
const response = new Response(body, { headers, status }) response.headers.set("Location", res.redirect.toString())
}
if (res.redirect) response.headers.set("Location", res.redirect)
return response return response
} }

View File

@@ -1,112 +0,0 @@
/**
* <div style={{backgroundColor: "#24292f", display: "flex", justifyContent: "space-between", color: "#fff", padding: 16}}>
* <span>Built-in <b>Asgardeo</b> integration.</span>
* <a href="https://wso2.com/asgardeo/">
* <img style={{display: "block"}} src="https://authjs.dev/img/providers/asgardeo-dark.svg" height="48" width="48"/>
* </a>
* </div>
*
* ---
* @module providers/asgardeo
*/
import type { OIDCConfig, OIDCUserConfig } from "./index.js"
export interface AsgardeoProfile {
sub: string
given_name: string
email: string
picture: string
}
/**
* Add Asgardeo login to your page.
* ## Documentation
*
* https://wso2.com/asgardeo/docs/guides/authentication
*
*
* ## Instructions
*
* - Log into https://console.asgardeo.io.
* - Next, go to "Application" tab (More info: https://wso2.com/asgardeo/docs/guides/applications/register-oidc-web-app/).
* - Register standard based - Open id connect, application.
* - Add callback URL: http://localhost:3000/api/auth/callback/asgardeo and https://your-domain.com/api/auth/callback/asgardeo
* - After registering the application, go to protocol tab.
* - Check `code` grant type.
* - Add Authorized redirect URLs & Allowed origins fields.
* - Make Email, First Name, Photo URL user attributes mandatory from the console.
*
* Create a `.env` file in the project root add the following entries:
*
* These values can be collected from the application created.
*
* ```
* ASGARDEO_CLIENT_ID=<Copy client ID from protocol tab here>
* ASGARDEO_CLIENT_SECRET=<Copy client from protocol tab here>
* ASGARDEO_ISSUER=<Copy the issuer url from the info tab here>
* ```
*
* In `pages/api/auth/[...nextauth].js` find or add the `Asgardeo` entries:
*
* ```js
* import Asgardeo from "next-auth/providers/asgardeo";
* ...
* providers: [
* Asgardeo({
* clientId: process.env.ASGARDEO_CLIENT_ID,
* clientSecret: process.env.ASGARDEO_CLIENT_SECRET,
* issuer: process.env.ASGARDEO_ISSUER
* }),
* ],
*
* ...
* ```
*
* ## Resources
*
* @see [Asgardeo - Authentication Guide](https://wso2.com/asgardeo/docs/guides/authentication)
* @see [Learn more about OAuth](https://authjs.dev/concepts/oauth)
* @see [Source code](https://github.com/nextauthjs/next-auth/blob/main/packages/core/src/providers/asgardeo.ts)
*
* ## Notes
*
* By default, Auth.js assumes that the Asgardeo provider is
* based on the [OAuth 2](https://www.rfc-editor.org/rfc/rfc6749.html) specification.
*
* :::tip
*
* The Asgardeo provider comes with a [default configuration](https://github.com/nextauthjs/next-auth/blob/main/packages/core/src/providers/asgardeo.ts).
* To override the defaults for your use case, check out [customizing a built-in OAuth provider](https://authjs.dev/guides/providers/custom-provider#override-default-options).
*
* :::
*
* :::info **Disclaimer**
*
* If you think you found a bug in the default configuration, you can [open an issue](https://authjs.dev/new/provider-issue).
*
* Auth.js strictly adheres to the specification and it cannot take responsibility for any deviation from
* the spec by the provider. You can open an issue, but if the problem is non-compliance with the spec,
* we might not pursue a resolution. You can ask for more help in [Discussions](https://authjs.dev/new/github-discussions).
*
* :::
*/
export default function Asgardeo(
config: OIDCUserConfig<AsgardeoProfile>
): OIDCConfig<AsgardeoProfile> {
return {
id: "asgardeo",
name: "Asgardeo",
type: "oidc",
wellKnown: `${config?.issuer}/oauth2/token/.well-known/openid-configuration`,
style: {
logo: "/asgardeo.svg",
logoDark: "/asgardeo-dark.svg",
bg: "#fff",
text: "#000",
bgDark: "#000",
textDark: "#fff",
},
options: config,
}
}

View File

@@ -34,21 +34,22 @@ export default function AzureAD<P extends AzureADProfile>(
) )
// Confirm that profile photo was returned // Confirm that profile photo was returned
let image if (response.ok) {
// TODO: Do this without Buffer const pictureBuffer = await response.arrayBuffer()
if (response.ok && typeof Buffer !== "undefined") { const pictureBase64 = Buffer.from(pictureBuffer).toString("base64")
try { return {
const pictureBuffer = await response.arrayBuffer() id: profile.sub,
const pictureBase64 = Buffer.from(pictureBuffer).toString("base64") name: profile.name,
image = `data:image/jpeg;base64, ${pictureBase64}` email: profile.email,
} catch {} image: `data:image/jpeg;base64, ${pictureBase64}`,
} }
} else {
return { return {
id: profile.sub, id: profile.sub,
name: profile.name, name: profile.name,
email: profile.email, email: profile.email,
image: image ?? null, image: null,
}
} }
}, },
style: { style: {

View File

@@ -49,6 +49,10 @@ export interface CredentialsConfig<
export type CredentialsProviderType = "Credentials" export type CredentialsProviderType = "Credentials"
export type CredentialsConfigInternal<
C extends Record<string, CredentialInput> = Record<string, CredentialInput>
> = CredentialsConfig<C> & { options: CredentialsConfig<C> }
/** /**
* The Credentials provider allows you to handle signing in with arbitrary credentials, * The Credentials provider allows you to handle signing in with arbitrary credentials,
* such as a username and password, domain, or two factor authentication or hardware device (e.g. YubiKey U2F / FIDO). * such as a username and password, domain, or two factor authentication or hardware device (e.g. YubiKey U2F / FIDO).

View File

@@ -1,77 +1,21 @@
import type { OAuthConfig, OAuthUserConfig } from "./index.js" import type { OAuthConfig, OAuthUserConfig } from "./index.js"
/**
* Corresponds to the user structure documented here:
* https://discord.com/developers/docs/resources/user#user-object-user-structure
*/
export interface DiscordProfile extends Record<string, any> { export interface DiscordProfile extends Record<string, any> {
/** the user's id (i.e. the numerical snowflake) */ accent_color: number
id: string avatar: string
/** the user's username, not unique across the platform */ banner: string
username: string banner_color: string
/** the user's 4-digit discord-tag */
discriminator: string discriminator: string
/** email: string
* the user's avatar hash:
* https://discord.com/developers/docs/reference#image-formatting
*/
avatar: string | null
/** whether the user belongs to an OAuth2 application */
bot?: boolean
/**
* whether the user is an Official Discord System user (part of the urgent
* message system)
*/
system?: boolean
/** whether the user has two factor enabled on their account */
mfa_enabled: boolean
/**
* the user's banner hash:
* https://discord.com/developers/docs/reference#image-formatting
*/
banner: string | null
/** the user's banner color encoded as an integer representation of hexadecimal color code */
accent_color: number | null
/**
* the user's chosen language option:
* https://discord.com/developers/docs/reference#locales
*/
locale: string
/** whether the email on this account has been verified */
verified: boolean
/** the user's email */
email: string | null
/**
* the flags on a user's account:
* https://discord.com/developers/docs/resources/user#user-object-user-flags
*/
flags: number flags: number
/** id: string
* the type of Nitro subscription on a user's account:
* https://discord.com/developers/docs/resources/user#user-object-premium-types
*/
premium_type: number
/**
* the public flags on a user's account:
* https://discord.com/developers/docs/resources/user#user-object-user-flags
*/
public_flags: number
/** undocumented field; corresponds to the user's custom nickname */
display_name: string | null
/**
* undocumented field; corresponds to the Discord feature where you can e.g.
* put your avatar inside of an ice cube
*/
avatar_decoration: string | null
/**
* undocumented field; corresponds to the premium feature where you can
* select a custom banner color
*/
banner_color: string | null
/** undocumented field; the CDN URL of their profile picture */
image_url: string image_url: string
locale: string
mfa_enabled: boolean
premium_type: number
public_flags: number
username: string
verified: boolean
} }
export default function Discord<P extends DiscordProfile>( export default function Discord<P extends DiscordProfile>(

View File

@@ -65,16 +65,12 @@ export type Provider<P extends Profile = Profile> = (
| EmailConfig | EmailConfig
| CredentialsConfig | CredentialsConfig
) & { ) & {
/**
* Used to deep merge user-provided config with the default config
* @internal
*/
options: Record<string, unknown> options: Record<string, unknown>
} }
export type BuiltInProviders = Record< export type BuiltInProviders = Record<
OAuthProviderType, OAuthProviderType,
(config: Partial<OAuthConfig<any>>) => OAuthConfig<any> (options: Partial<OAuthConfig<any>>) => OAuthConfig<any>
> & > &
Record<CredentialsProviderType, typeof CredentialsProvider> & Record<CredentialsProviderType, typeof CredentialsProvider> &
Record<EmailProviderType, typeof EmailProvider> Record<EmailProviderType, typeof EmailProvider>

View File

@@ -1,166 +0,0 @@
/**
* <div style={{backgroundColor: "#000", display: "flex", justifyContent: "space-between", color: "#fff", padding: 16}}>
* <span>Built-in <b>Notion</b> integration.</span>
* <a href="https://notion.so">
* <img style={{display: "block"}} src="https://authjs.dev/img/providers/notion.svg" height="48" width="48"/>
* </a>
* </div>
*
* ---
* @module providers/notion
*/
import type { OAuthConfig, OAuthUserConfig } from "."
export interface Person extends Record<string, any> {
email: string
}
// https://developers.notion.com/reference/user
export interface User extends Record<string, any> {
object: "user" | "bot"
id: string
type: string
name: string
avatar_url: null | string
person: Person
owner?: {
type: "workspace" | "user"
workspace: string
}
workspace_name?: string | null
}
export interface Owner {
type: string
user: User
}
// Notion responds with an access_token + some additional information, which we define here
// More info - https://developers.notion.com/docs/authorization#step-4-notion-responds-with-an-access_token-and-some-additional-information
export interface NotionProfile extends Record<string, any> {
access_token: string
bot_id: string
duplicated_template_id: string
owner?: Owner
workspace_icon: string
workspace_id: number
workspace_name: string
}
// Any config required that isn't part of the `OAuthUserConfig` spec should belong here
// For example, we must pass a `redirectUri` to the Notion API when requesting tokens, therefore we add it here
interface AdditionalConfig {
redirectUri: string
}
const NOTION_HOST = "https://api.notion.com"
const NOTION_API_VERSION = "2022-06-28"
/**
* Add Notion login to your page.
*
* ## Example
*
* ```ts
* import { Auth } from "@auth/core"
* import Notion from "@auth/core/providers/notion"
*
* const request = new Request("https://example.com")
* const response = await Auth(request, {
* providers: [Notion({ clientId: "", clientSecret: "", redirectUri: "" })],
* })
* ```
*
* ---
*
* ## Resources
* - [Notion Docs](https://developers.notion.com/docs)
* - [Notion Authorization Docs](https://developers.notion.com/docs/authorization)
* - [Notion Integrations](https://www.notion.so/my-integrations)
*
* ---
*
* ## Notes
* You need to select "Public Integration" on the configuration page to get an `oauth_id` and `oauth_secret`. Private integrations do not provide these details.
* You must provide a `clientId` and `clientSecret` to use this provider, as-well as a redirect URI (due to this being required by Notion endpoint to fetch tokens).
*
* :::tip
*
* The Notion provider comes with a [default configuration](https://github.com/nextauthjs/next-auth/blob/main/packages/core/src/providers/notion.ts).
* To override the defaults for your use case, check out [customizing a built-in OAuth provider](https://authjs.dev/guides/providers/custom-provider#override-default-options).
*
* :::
*
* :::info **Disclaimer**
*
* If you think you found a bug in the default configuration, you can [open an issue](https://authjs.dev/new/provider-issue).
*
* Auth.js strictly adheres to the specification and it cannot take responsibility for any deviation from
* the spec by the provider. You can open an issue, but if the problem is non-compliance with the spec,
* we might not pursue a resolution. You can ask for more help in [Discussions](https://authjs.dev/new/github-discussions).
*
* :::
*/
export default function NotionProvider<P extends NotionProfile>(
options: OAuthUserConfig<P> & AdditionalConfig
): OAuthConfig<P> {
return {
id: "notion",
name: "Notion",
type: "oauth",
token: {
url: `${NOTION_HOST}/v1/oauth/token`,
},
userinfo: {
url: `${NOTION_HOST}/v1/users`,
// The result of this method will be the input to the `profile` callback.
// We use a custom request handler, since we need to do things such as pass the "Notion-Version" header
// More info: https://next-auth.js.org/configuration/providers/oauth
async request(context) {
const profile = await fetch(`${NOTION_HOST}/v1/users/me`, {
headers: {
Authorization: `Bearer ${context.tokens.access_token}`,
"Notion-Version": NOTION_API_VERSION,
},
})
const {
bot: {
owner: { user },
},
} = await profile.json()
return user
},
},
authorization: {
params: {
client_id: options.clientId,
response_type: "code",
owner: "user",
redirect_uri: options.redirectUri,
},
url: `${NOTION_HOST}/v1/oauth/authorize`,
},
async profile(profile, tokens) {
return {
id: profile.id,
name: profile.name,
email: profile.person.email,
image: profile.avatar_url,
}
},
style: {
logo: "/notion.svg",
logoDark: "/notion.svg",
bg: "#fff",
text: "#000",
bgDark: "#fff",
textDark: "#000",
},
options,
}
}

View File

@@ -0,0 +1,69 @@
// THIS FILE IS AUTOGENERATED. DO NOT EDIT.
export type OAuthProviderType =
| "42-school"
| "apple"
| "atlassian"
| "auth0"
| "authentik"
| "azure-ad-b2c"
| "azure-ad"
| "battlenet"
| "box"
| "boxyhq-saml"
| "bungie"
| "cognito"
| "coinbase"
| "credentials"
| "discord"
| "dropbox"
| "duende-identity-server6"
| "email"
| "eveonline"
| "facebook"
| "faceit"
| "foursquare"
| "freshbooks"
| "fusionauth"
| "github"
| "gitlab"
| "google"
| "hubspot"
| "identity-server4"
| "index"
| "instagram"
| "kakao"
| "keycloak"
| "line"
| "linkedin"
| "mailchimp"
| "mailru"
| "medium"
| "naver"
| "netlify"
| "oauth-types.js"
| "oauth"
| "okta"
| "onelogin"
| "osso"
| "osu"
| "patreon"
| "pinterest"
| "pipedrive"
| "reddit"
| "salesforce"
| "slack"
| "spotify"
| "strava"
| "todoist"
| "trakt"
| "twitch"
| "twitter"
| "united-effects"
| "vk"
| "wikimedia"
| "wordpress"
| "workos"
| "yandex"
| "zitadel"
| "zoho"
| "zoom"

View File

@@ -42,8 +42,6 @@ interface AdvancedEndpointHandler<P extends UrlParams, C, R> {
* You should **try to avoid using advanced options** unless you are very comfortable using them. * You should **try to avoid using advanced options** unless you are very comfortable using them.
*/ */
request?: EndpointRequest<C, R, P> request?: EndpointRequest<C, R, P>
/** @internal */
conform?: (response: Response) => Awaitable<Response | undefined>
} }
/** Either an URL (containing all the parameters) or an object with more granular control. */ /** Either an URL (containing all the parameters) or an object with more granular control. */
@@ -66,7 +64,7 @@ export type TokenEndpointHandler = EndpointHandler<
params: CallbackParamsType params: CallbackParamsType
/** /**
* When using this custom flow, make sure to do all the necessary security checks. * When using this custom flow, make sure to do all the necessary security checks.
* This object contains parameters you have to match against the request to make sure it is valid. * Thist object contains parameters you have to match against the request to make sure it is valid.
*/ */
checks: OAuthChecks checks: OAuthChecks
}, },
@@ -186,11 +184,7 @@ export type OAuthConfigInternal<Profile> = Omit<
OAuthEndpointType OAuthEndpointType
> & { > & {
authorization?: { url: URL } authorization?: { url: URL }
token?: { token?: { url: URL; request?: TokenEndpointHandler["request"] }
url: URL
request?: TokenEndpointHandler["request"]
conform?: TokenEndpointHandler["conform"]
}
userinfo?: { url: URL; request?: UserinfoEndpointHandler["request"] } userinfo?: { url: URL; request?: UserinfoEndpointHandler["request"] }
} & Pick<Required<OAuthConfig<Profile>>, "clientId" | "checks" | "profile"> } & Pick<Required<OAuthConfig<Profile>>, "clientId" | "checks" | "profile">

View File

@@ -1,4 +1,4 @@
import type { OIDCConfig, OIDCUserConfig } from "./index.js" import type { OAuthConfig, OAuthUserConfig } from "./index.js"
export interface TwitchProfile extends Record<string, any> { export interface TwitchProfile extends Record<string, any> {
sub: string sub: string
@@ -7,52 +7,26 @@ export interface TwitchProfile extends Record<string, any> {
picture: string picture: string
} }
export default function Twitch( export default function Twitch<P extends TwitchProfile>(
config: OIDCUserConfig<TwitchProfile> options: OAuthUserConfig<P>
): OIDCConfig<TwitchProfile> { ): OAuthConfig<P> {
return { return {
issuer: "https://id.twitch.tv/oauth2", issuer: "https://id.twitch.tv/oauth2",
id: "twitch", id: "twitch",
name: "Twitch", name: "Twitch",
type: "oidc", type: "oidc",
client: { token_endpoint_auth_method: "client_secret_post" },
authorization: { authorization: {
params: { params: {
scope: "openid user:read:email", scope: "openid user:read:email",
claims: { claims: {
id_token: { email: null, picture: null, preferred_username: null }, id_token: {
email: null,
picture: null,
preferred_username: null,
},
}, },
}, },
}, },
token: {
async conform(response) {
const body = await response.json()
if (response.ok) {
if (typeof body.scope === "string") {
console.warn(
"'scope' is a string. Redundant workaround, please open an issue."
)
} else if (Array.isArray(body.scope)) {
body.scope = body.scope.join(" ")
return new Response(JSON.stringify(body), response)
} else if ("scope" in body) {
delete body.scope
return new Response(JSON.stringify(body), response)
}
} else {
const { message: error_description, error } = body
if (typeof error !== "string") {
return new Response(
JSON.stringify({ error: "invalid_request", error_description }),
response
)
}
console.warn(
"Response has 'error'. Redundant workaround, please open an issue."
)
}
},
},
style: { style: {
logo: "/twitch.svg", logo: "/twitch.svg",
logoDark: "/twitch-dark.svg", logoDark: "/twitch-dark.svg",
@@ -61,6 +35,6 @@ export default function Twitch(
bgDark: "#65459B", bgDark: "#65459B",
textDark: "#fff", textDark: "#fff",
}, },
options: config, options,
} }
} }

View File

@@ -203,7 +203,7 @@ export interface CallbacksOptions<P = Profile, A = Account> {
* Its content is forwarded to the `session` callback, * Its content is forwarded to the `session` callback,
* where you can control what should be returned to the client. * where you can control what should be returned to the client.
* Anything else will be kept inaccessible from the client. * Anything else will be kept inaccessible from the client.
* *
* Returning `null` will invalidate the JWT session by clearing * Returning `null` will invalidate the JWT session by clearing
* the user's cookies. You'll still have to monitor and invalidate * the user's cookies. You'll still have to monitor and invalidate
* unexpired tokens from future requests yourself to prevent * unexpired tokens from future requests yourself to prevent
@@ -220,7 +220,7 @@ export interface CallbacksOptions<P = Profile, A = Account> {
account?: A | null account?: A | null
profile?: P profile?: P
isNewUser?: boolean isNewUser?: boolean
}) => Awaitable<JWT | null> }) => Awaitable<JWT|null>
} }
/** [Documentation](https://authjs.dev/reference/configuration/auth-config#cookies) */ /** [Documentation](https://authjs.dev/reference/configuration/auth-config#cookies) */
@@ -452,7 +452,7 @@ export interface ResponseInternal<
status?: number status?: number
headers?: Headers | HeadersInit headers?: Headers | HeadersInit
body?: Body body?: Body
redirect?: string redirect?: URL | string
cookies?: Cookie[] cookies?: Cookie[]
} }

View File

@@ -1,6 +1,6 @@
{ {
"name": "@auth/sveltekit", "name": "@auth/sveltekit",
"version": "0.2.0", "version": "0.1.12",
"description": "Authentication for SvelteKit.", "description": "Authentication for SvelteKit.",
"keywords": [ "keywords": [
"authentication", "authentication",
@@ -32,7 +32,7 @@
"test:unit": "vitest" "test:unit": "vitest"
}, },
"devDependencies": { "devDependencies": {
"@playwright/test": "1.29.2", "@playwright/test": "^1.28.1",
"@sveltejs/adapter-auto": "^1.0.0", "@sveltejs/adapter-auto": "^1.0.0",
"@sveltejs/kit": "^1.0.0", "@sveltejs/kit": "^1.0.0",
"@sveltejs/package": "^1.0.0", "@sveltejs/package": "^1.0.0",

View File

@@ -1,16 +1,8 @@
// After build, copy the files in ./package to the root directory, excluding the package.json file. // After build, copy the files in ./package to the root directory, excluding the package.json file.
import fs from "fs/promises" import fs from "fs/promises"
import path from "path" import path from "path"
let __dirname = path.dirname(new URL(import.meta.url).pathname) const __dirname = path.dirname(new URL(import.meta.url).pathname)
// The above hack to polyfill "__dirname" for ESM does not work on Windows computers,
// so we might have to manually perform more steps.
__dirname = __dirname.split(path.sep).join(path.posix.sep)
if (__dirname.match(/^\/\w:\//)) {
__dirname = __dirname.slice(3) // Remove the drive prefix.
}
const root = path.join(__dirname, "..") const root = path.join(__dirname, "..")
const pkgDir = path.join(root, "package") const pkgDir = path.join(root, "package")

View File

@@ -81,7 +81,7 @@
* return { * return {
* session: await event.locals.getSession() * session: await event.locals.getSession()
* }; * };
* }; * };
* ``` * ```
* *
* What you return in the function `LayoutServerLoad` will be available inside the `$page` store, in the `data` property: `$page.data`. * What you return in the function `LayoutServerLoad` will be available inside the `$page` store, in the `data` property: `$page.data`.
@@ -106,7 +106,7 @@
* return {}; * return {};
* }; * };
* ``` * ```
* *
* :::danger * :::danger
* Make sure to ALWAYS grab the session information from the parent instead of using the store in the case of a `PageLoad`. * Make sure to ALWAYS grab the session information from the parent instead of using the store in the case of a `PageLoad`.
* Not doing so can lead to users being able to incorrectly access protected information in the case the `+layout.server.ts` does not run for that page load. * Not doing so can lead to users being able to incorrectly access protected information in the case the `+layout.server.ts` does not run for that page load.
@@ -130,14 +130,14 @@
* The handle hook, available in `hooks.server.ts`, is a function that receives ALL requests sent to your SvelteKit webapp. * The handle hook, available in `hooks.server.ts`, is a function that receives ALL requests sent to your SvelteKit webapp.
* You may intercept them inside the handle hook, add and modify things in the request, block requests, etc. * You may intercept them inside the handle hook, add and modify things in the request, block requests, etc.
* Some readers may notice we are already using this handle hook for SvelteKitAuth which returns a handle itself, so we are going to use SvelteKit's sequence to provide middleware-like functions that set the handle hook. * Some readers may notice we are already using this handle hook for SvelteKitAuth which returns a handle itself, so we are going to use SvelteKit's sequence to provide middleware-like functions that set the handle hook.
* *
* ```ts * ```ts
* import { SvelteKitAuth } from '@auth/sveltekit'; * import { SvelteKitAuth } from '@auth/sveltekit';
* import GitHub from '@auth/core/providers/github'; * import GitHub from '@auth/core/providers/github';
* import { GITHUB_ID, GITHUB_SECRET } from '$env/static/private'; * import { GITHUB_ID, GITHUB_SECRET } from '$env/static/private';
* import { redirect, type Handle } from '@sveltejs/kit'; * import { redirect, type Handle } from '@sveltejs/kit';
* import { sequence } from '@sveltejs/kit/hooks'; * import { sequence } from '@sveltejs/kit/hooks';
* *
* async function authorization({ event, resolve }) { * async function authorization({ event, resolve }) {
* // Protect any routes under /authenticated * // Protect any routes under /authenticated
* if (event.url.pathname.startsWith('/authenticated')) { * if (event.url.pathname.startsWith('/authenticated')) {
@@ -146,14 +146,14 @@
* throw redirect(303, '/auth'); * throw redirect(303, '/auth');
* } * }
* } * }
* *
* // If the request is still here, just proceed as normally * // If the request is still here, just proceed as normally
* const result = await resolve(event, { * const result = await resolve(event, {
* transformPageChunk: ({ html }) => html * transformPageChunk: ({ html }) => html
* }); * });
* return result; * return result;
* } * }
* *
* // First handle authentication, then authorization * // First handle authentication, then authorization
* // Each function acts as a middleware, receiving the request handle * // Each function acts as a middleware, receiving the request handle
* // And returning a handle which gets passed to the next function * // And returning a handle which gets passed to the next function
@@ -183,7 +183,7 @@
* PRs to improve this documentation are welcome! See [this file](https://github.com/nextauthjs/next-auth/blob/main/packages/frameworks-sveltekit/src/lib/index.ts). * PRs to improve this documentation are welcome! See [this file](https://github.com/nextauthjs/next-auth/blob/main/packages/frameworks-sveltekit/src/lib/index.ts).
* ::: * :::
* *
* @module index * @module main
*/ */
/// <reference types="@sveltejs/kit" /> /// <reference types="@sveltejs/kit" />

View File

@@ -0,0 +1,23 @@
diff --git a/dist/theme.js b/dist/theme.js
index 1483a4b4ec69583aa3086eac83b2b31ae8bb6777..c30e7a4f7785fc230099e8b904040dd4aa57c38e 100644
--- a/dist/theme.js
+++ b/dist/theme.js
@@ -221,12 +221,12 @@ class MarkdownTheme extends typedoc_1.Theme {
directory: 'enums',
template: this.getReflectionTemplate(),
},
- {
- kind: [typedoc_1.ReflectionKind.Class],
- isLeaf: false,
- directory: 'classes',
- template: this.getReflectionTemplate(),
- },
+ // {
+ // kind: [typedoc_1.ReflectionKind.Class],
+ // isLeaf: false,
+ // directory: 'classes',
+ // template: this.getReflectionTemplate(),
+ // },
{
kind: [typedoc_1.ReflectionKind.Interface],
isLeaf: false,

1816
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -25,10 +25,6 @@
"dependsOn": ["^build"], "dependsOn": ["^build"],
"outputs": ["lib/**", "providers/**", "*.js", "*.d.ts", "*.d.ts.map"] "outputs": ["lib/**", "providers/**", "*.js", "*.d.ts", "*.d.ts.map"]
}, },
"@auth/sveltekit#build": {
"dependsOn": ["^build"],
"outputs": ["client.*", "index.*"]
},
"clean": { "clean": {
"cache": false "cache": false
}, },
@@ -38,15 +34,8 @@
"test": { "test": {
"outputs": [] "outputs": []
}, },
"e2e": {
"outputs": ["playwright-report/**"]
},
"@next-auth/upstash-redis-adapter#test": { "@next-auth/upstash-redis-adapter#test": {
"env": ["UPSTASH_REDIS_KEY", "UPSTASH_REDIS_URL"] "env": ["UPSTASH_REDIS_KEY", "UPSTASH_REDIS_URL"]
},
"docs#dev": {
"dependsOn": ["^build"],
"cache": false
} }
} }
} }