mirror of
https://github.com/SrIzan10/next-auth.git
synced 2026-05-01 10:55:20 +00:00
Compare commits
18 Commits
next-auth@
...
next-auth@
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2510f74809 | ||
|
|
27b2519b84 | ||
|
|
5f15b0704a | ||
|
|
e4573ffff5 | ||
|
|
4ce1951a2b | ||
|
|
c95531d651 | ||
|
|
654d52bb56 | ||
|
|
b72d7be9be | ||
|
|
76fcc4e70c | ||
|
|
4cacf504dd | ||
|
|
50eb23f626 | ||
|
|
d813c00b3e | ||
|
|
fc4448a85a | ||
|
|
16f781c091 | ||
|
|
ebfdaece0e | ||
|
|
64a190e549 | ||
|
|
e11f898c10 | ||
|
|
dcb11da2e2 |
7
.github/sync.yml
vendored
7
.github/sync.yml
vendored
@@ -1,7 +0,0 @@
|
||||
# This is a legacy example pushed from the v4 branch
|
||||
nextauthjs/next-auth-example:
|
||||
- source: apps/example-nextjs
|
||||
dest: .
|
||||
deleteOrphaned: true
|
||||
- .github/FUNDING.yml
|
||||
- LICENSE
|
||||
18
.github/workflows/sync-examples.yml
vendored
18
.github/workflows/sync-examples.yml
vendored
@@ -1,18 +0,0 @@
|
||||
name: Sync Example Repositories
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- v4
|
||||
workflow_dispatch:
|
||||
jobs:
|
||||
sync:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout Repository
|
||||
uses: actions/checkout@v3
|
||||
- name: Run GitHub File Sync
|
||||
# Can update to v1 when https://github.com/BetaHuhn/repo-file-sync-action/issues/168 is resolved
|
||||
uses: BetaHuhn/repo-file-sync-action@v1.16.5
|
||||
with:
|
||||
GH_PAT: ${{ secrets.GH_PAT_CLASSIC }}
|
||||
SKIP_PR: true
|
||||
@@ -6,16 +6,18 @@ title: Adapters
|
||||
Visit the [authjs.dev](https://authjs.dev/reference/adapters) page for the up-to-date documentation.
|
||||
|
||||
- [Dgraph](https://authjs.dev/reference/adapter/dgraph)
|
||||
- [Drizzle](https://authjs.dev/reference/adapter/drizzle)
|
||||
- [DynamoDB](https://authjs.dev/reference/adapter/dynamodb)
|
||||
- [Fauna](https://authjs.dev/reference/adapter/fauna)
|
||||
- [Firebase](https://authjs.dev/reference/adapter/firebase)
|
||||
- [MongoDB](https://authjs.dev/reference/adapter/mongodb)
|
||||
- [Prisma](https://authjs.dev/reference/adapter/prisma)
|
||||
- [TypeORM](https://authjs.dev/reference/adapter/typeorm)
|
||||
- [kysely](https://authjs.dev/reference/adapter/kysely)
|
||||
- [MikroORM](https://authjs.dev/reference/adapter/mikro-orm)
|
||||
- [MongoDB](https://authjs.dev/reference/adapter/mongodb)
|
||||
- [neo4j](https://authjs.dev/reference/adapter/neo4j)
|
||||
- [Prisma](https://authjs.dev/reference/adapter/prisma)
|
||||
- [PouchDB](https://authjs.dev/reference/adapter/pouchdb)
|
||||
- [Sequelize](https://authjs.dev/reference/adapter/sequelize)
|
||||
- [Supabase](https://authjs.dev/reference/adapter/supabase)
|
||||
- [TypeORM](https://authjs.dev/reference/adapter/typeorm)
|
||||
- [Upstash Redis](https://authjs.dev/reference/adapter/upstash-redis)
|
||||
- [Xata](https://authjs.dev/reference/adapter/xata)
|
||||
|
||||
@@ -2,6 +2,27 @@
|
||||
|
||||
## `getServerSession`
|
||||
|
||||
:::tip
|
||||
You can create a helper function so you don't need to pass `authOptions` around:
|
||||
|
||||
```ts title=auth.ts
|
||||
import type { GetServerSidePropsContext, NextApiRequest, NextApiResponse } from "next"
|
||||
import type { NextAuthOptions } from "next-auth"
|
||||
import { getServerSession } from "next-auth"
|
||||
|
||||
// You'll need to import and pass this
|
||||
// to `NextAuth` in `app/api/auth/[...nextauth]/route.ts`
|
||||
export const config = {
|
||||
providers: [], // rest of your config
|
||||
} satisfies NextAuthOptions
|
||||
|
||||
// Use it in server contexts
|
||||
export function auth(...args: [GetServerSidePropsContext["req"], GetServerSidePropsContext["res"]] | [NextApiRequest, NextApiResponse] | []) {
|
||||
return getServerSession(...args, config)
|
||||
}
|
||||
```
|
||||
:::
|
||||
|
||||
When calling from the server-side i.e. in Route Handlers, React Server Components, API routes or in `getServerSideProps`, we recommend using this function instead of `getSession` to retrieve the `session` object. This method is especially useful when you are using NextAuth.js with a database. This method can _drastically_ reduce response time when used over `getSession` on server-side, due to avoiding an extra `fetch` to an API Route (this is generally [not recommended in Next.js](https://nextjs.org/docs/basic-features/data-fetching/get-server-side-props#getserversideprops-or-api-routes)). In addition, `getServerSession` will correctly update the cookie expiry time and update the session content if `callbacks.jwt` or `callbacks.session` changed something.
|
||||
|
||||
`getServerSession` requires passing the same object you would pass to `NextAuth` when initializing NextAuth.js. To do so, you can export your NextAuth.js options in the following way:
|
||||
@@ -157,22 +178,27 @@ Callbacks are asynchronous functions you can use to control what happens when an
|
||||
|
||||
#### Description
|
||||
|
||||
Specify URLs to be used if you want to create custom sign in, and error pages. Pages specified will override the corresponding built-in page.
|
||||
Specify URLs to be used if you want to create custom sign-in and error pages. The pages specified will override the corresponding built-in page.
|
||||
|
||||
:::info
|
||||
This should match the `pages` configuration that's found in `[...nextauth].ts`.
|
||||
The `pages` configuration should match the same configuration in `[...nextauth].ts`. This is so that the `next-auth` Middleware is aware of your custom pages, so it won't end up redirecting to itself when an unauthenticated condition is met.
|
||||
:::
|
||||
|
||||
#### Example (default value)
|
||||
|
||||
```js
|
||||
pages: {
|
||||
signIn: '/api/auth/signin',
|
||||
error: '/api/auth/error',
|
||||
}
|
||||
import { withAuth } from "next-auth/middleware"
|
||||
|
||||
export default withAuth({
|
||||
// Matches the pages config in `[...nextauth]`
|
||||
pages: {
|
||||
signIn: '/login',
|
||||
error: '/error',
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
See the documentation for the [pages option](/configuration/pages) for more information.
|
||||
For more information, see the documentation for the [pages option](/configuration/pages).
|
||||
|
||||
---
|
||||
|
||||
@@ -182,7 +208,7 @@ See the documentation for the [pages option](/configuration/pages) for more info
|
||||
|
||||
#### Description
|
||||
|
||||
The same `secret` used in the [NextAuth.js config](/configuration/options#options).
|
||||
The same `secret` is used in the [NextAuth.js config](/configuration/options#options).
|
||||
|
||||
#### Example (default value)
|
||||
|
||||
@@ -255,7 +281,7 @@ import withAuth from "next-auth/middleware"
|
||||
import { authOptions } from "pages/api/auth/[...nextauth]";
|
||||
|
||||
export default withAuth({
|
||||
jwt: { decode: authOptions.jwt },
|
||||
jwt: { decode: authOptions.jwt?.decode },
|
||||
callbacks: {
|
||||
authorized: ({ token }) => !!token,
|
||||
},
|
||||
|
||||
@@ -21,7 +21,7 @@ This error occurs when the `SessionProvider` Context has a problem fetching sess
|
||||
|
||||
#### CLIENT_FETCH_ERROR
|
||||
|
||||
If you see `CLIENT_FETCH_ERROR` make sure you have configured the `NEXTAUTH_URL` environment variable.
|
||||
This can happen for multiple reasons. Make sure that you [configured](/configuration/initialization) NextAuth.js correctly, and if you used [`NEXTAUTH_URL`](https://next-auth.js.org/configuration/options#nextauth_url) that it's correctly set.
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -203,7 +203,7 @@ export default NextAuth({
|
||||
jwt({ token, trigger, session }) {
|
||||
if (trigger === "update" && session?.name) {
|
||||
// Note, that `session` can be any arbitrary object, remember to validate it!
|
||||
token.name = session
|
||||
token.name = session.name
|
||||
}
|
||||
return token
|
||||
}
|
||||
@@ -519,6 +519,10 @@ where `data.url` is the validated URL you can redirect the user to without any f
|
||||
|
||||
## SessionProvider
|
||||
|
||||
:::note
|
||||
If you are using the App Router, we encourage you to use [`getServerSession`](/configuration/nextjs#getserversession) in server contexts instead. (`SessionProvider` *can* be used in the App Router, which might be the easier choice if you are migrating from pages.)
|
||||
:::
|
||||
|
||||
Using the supplied `<SessionProvider>` allows instances of `useSession()` to share the session object across components, by using [React Context](https://react.dev/learn/passing-data-deeply-with-context) under the hood. It also takes care of keeping the session updated and synced between tabs/windows.
|
||||
|
||||
```jsx title="pages/_app.js"
|
||||
|
||||
@@ -76,6 +76,7 @@ Instances of `useSession` will then have access to the session data and status.
|
||||
|
||||
:::tip
|
||||
Check out the [client documentation](/getting-started/client) to see how you can improve the user experience and page performance by using the NextAuth.js client.
|
||||
If you are using the Next.js App Router, please note that `<SessionProvider />` requires a client component and therefore cannot be put inside the root layout. For more details, check out the [Next.js documentation](https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts).
|
||||
:::
|
||||
|
||||
### Frontend - Add React Hook
|
||||
|
||||
@@ -118,7 +118,7 @@ Once you have saved your schema, use the Prisma CLI to generate the Prisma Clien
|
||||
npx prisma generate
|
||||
```
|
||||
|
||||
To configure you database to use the new schema (i.e. create tables and columns) use the `prisma migrate` command:
|
||||
To configure your database to use the new schema (i.e. create tables and columns) use the `prisma migrate` command:
|
||||
|
||||
```
|
||||
npx prisma migrate dev
|
||||
|
||||
@@ -168,7 +168,7 @@ export default function App({
|
||||
|
||||
## Security
|
||||
|
||||
If you think you have found a vulnerability (or not sure) in NextAuth.js or any of the related packages (i.e. Adapters), we ask you to have a read of our [Security Policy](https://github.com/nextauthjs/next-auth/blob/main/SECURITY.md) to reach out responsibly. Please do not open Pull Requests/Issues/Discussions before consulting with us.
|
||||
If you think you have found a vulnerability (or not sure) in NextAuth.js or any of the related packages (i.e. Adapters), we ask you to have a read of our [Security Policy](https://github.com/nextauthjs/next-auth/security/policy) to reach out responsibly. Please do not open Pull Requests/Issues/Discussions before consulting with us.
|
||||
|
||||
## Acknowledgments
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "next-auth",
|
||||
"version": "4.23.1",
|
||||
"version": "4.23.2",
|
||||
"description": "Authentication for Next.js",
|
||||
"homepage": "https://next-auth.js.org",
|
||||
"repository": "https://github.com/nextauthjs/next-auth.git",
|
||||
@@ -9,7 +9,7 @@
|
||||
"Balázs Orbán <info@balazsorban.com>",
|
||||
"Nico Domino <yo@ndo.dev>",
|
||||
"Lluis Agusti <hi@llu.lu>",
|
||||
"Thang Huu Vu <thvu@hey.com>"
|
||||
"Thang Huu Vu <hi@thvu.dev>"
|
||||
],
|
||||
"main": "index.js",
|
||||
"module": "index.js",
|
||||
|
||||
@@ -114,7 +114,7 @@ export function defaultCookies(useSecureCookies: boolean): CookiesOptions {
|
||||
path: "/",
|
||||
secure: useSecureCookies,
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,22 +161,22 @@ export class SessionStore {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* The JWT Session or database Session ID
|
||||
* constructed from the cookie chunks.
|
||||
*/
|
||||
get value() {
|
||||
// Sort the chunks by their keys before joining
|
||||
const sortedKeys = Object.keys(this.#chunks).sort((a, b) => {
|
||||
const aSuffix = parseInt(a.split(".").pop() || "0")
|
||||
const bSuffix = parseInt(b.split(".").pop() || "0")
|
||||
get value() {
|
||||
// Sort the chunks by their keys before joining
|
||||
const sortedKeys = Object.keys(this.#chunks).sort((a, b) => {
|
||||
const aSuffix = parseInt(a.split(".").pop() ?? "0")
|
||||
const bSuffix = parseInt(b.split(".").pop() ?? "0")
|
||||
|
||||
return aSuffix - bSuffix
|
||||
});
|
||||
return aSuffix - bSuffix
|
||||
})
|
||||
|
||||
// Use the sorted keys to join the chunks in the correct order
|
||||
return sortedKeys.map(key => this.#chunks[key]).join("")
|
||||
}
|
||||
// Use the sorted keys to join the chunks in the correct order
|
||||
return sortedKeys.map((key) => this.#chunks[key]).join("")
|
||||
}
|
||||
|
||||
/** Given a cookie, return a list of cookies, chunked to fit the allowed cookie size. */
|
||||
#chunk(cookie: Cookie): Cookie[] {
|
||||
|
||||
@@ -36,7 +36,7 @@ export interface JWTDecodeParams {
|
||||
export interface JWTOptions {
|
||||
/**
|
||||
* The secret used to encode/decode the NextAuth.js issued JWT.
|
||||
* @deprecated Set the `NEXTAUTH_SECRET` environment vairable or
|
||||
* @deprecated Set the `NEXTAUTH_SECRET` environment variable or
|
||||
* use the top-level `secret` option instead
|
||||
*/
|
||||
secret: string
|
||||
|
||||
@@ -101,6 +101,7 @@ async function NextAuthRouteHandler(
|
||||
response.headers.delete("Location")
|
||||
response.headers.set("Content-Type", "application/json")
|
||||
return new Response(JSON.stringify({ url: redirect }), {
|
||||
status: internalResponse.status,
|
||||
headers: response.headers,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -247,7 +247,7 @@ export async function signIn<
|
||||
isCredentials ? "callback" : "signin"
|
||||
}/${provider}`
|
||||
|
||||
const _signInUrl = `${signInUrl}?${new URLSearchParams(authorizationParams)}`
|
||||
const _signInUrl = `${signInUrl}${authorizationParams ? `?${new URLSearchParams(authorizationParams)}` : ""}`
|
||||
|
||||
const res = await fetch(_signInUrl, {
|
||||
method: "post",
|
||||
|
||||
Reference in New Issue
Block a user