mirror of
https://github.com/SrIzan10/next-auth.git
synced 2026-05-01 10:55:20 +00:00
Compare commits
1 Commits
@next-auth
...
@next-auth
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
46304838af |
11
.github/workflows/release.yml
vendored
11
.github/workflows/release.yml
vendored
@@ -21,7 +21,7 @@ jobs:
|
|||||||
- name: Install pnpm
|
- name: Install pnpm
|
||||||
uses: pnpm/action-setup@v2.2.1
|
uses: pnpm/action-setup@v2.2.1
|
||||||
with:
|
with:
|
||||||
version: 7.5.1
|
version: 6.32.8
|
||||||
- name: Setup Node
|
- name: Setup Node
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
@@ -55,7 +55,7 @@ jobs:
|
|||||||
- name: Install pnpm
|
- name: Install pnpm
|
||||||
uses: pnpm/action-setup@v2.2.1
|
uses: pnpm/action-setup@v2.2.1
|
||||||
with:
|
with:
|
||||||
version: 7.5.1
|
version: 6.32.8
|
||||||
- name: Setup Node
|
- name: Setup Node
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
@@ -69,8 +69,7 @@ jobs:
|
|||||||
git config --global user.name "Balázs Orbán"
|
git config --global user.name "Balázs Orbán"
|
||||||
pnpm release
|
pnpm release
|
||||||
env:
|
env:
|
||||||
RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }}
|
|
||||||
NPM_TOKEN_PKG: ${{ secrets.NPM_TOKEN_PKG }}
|
NPM_TOKEN_PKG: ${{ secrets.NPM_TOKEN_PKG }}
|
||||||
NPM_TOKEN_ORG: ${{ secrets.NPM_TOKEN_ORG }}
|
NPM_TOKEN_ORG: ${{ secrets.NPM_TOKEN_ORG }}
|
||||||
release-pr:
|
release-pr:
|
||||||
@@ -85,7 +84,7 @@ jobs:
|
|||||||
- name: Install pnpm
|
- name: Install pnpm
|
||||||
uses: pnpm/action-setup@v2.2.1
|
uses: pnpm/action-setup@v2.2.1
|
||||||
with:
|
with:
|
||||||
version: 7.5.1
|
version: 6.32.8
|
||||||
- name: Setup Node
|
- name: Setup Node
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
@@ -104,7 +103,7 @@ jobs:
|
|||||||
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> .npmrc
|
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> .npmrc
|
||||||
pnpm publish --no-git-checks --access public --tag experimental
|
pnpm publish --no-git-checks --access public --tag experimental
|
||||||
env:
|
env:
|
||||||
NPM_TOKEN: ${{ secrets.NPM_TOKEN_PKG }}
|
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
|
||||||
- name: Comment version on PR
|
- name: Comment version on PR
|
||||||
uses: NejcZdovc/comment-pr@v1
|
uses: NejcZdovc/comment-pr@v1
|
||||||
with:
|
with:
|
||||||
|
|||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -30,7 +30,7 @@ packages/next-auth/providers
|
|||||||
packages/next-auth/src/providers/oauth-types.ts
|
packages/next-auth/src/providers/oauth-types.ts
|
||||||
packages/next-auth/client
|
packages/next-auth/client
|
||||||
packages/next-auth/css
|
packages/next-auth/css
|
||||||
packages/next-auth/utils
|
packages/next-auth/lib
|
||||||
packages/next-auth/core
|
packages/next-auth/core
|
||||||
packages/next-auth/jwt
|
packages/next-auth/jwt
|
||||||
packages/next-auth/react
|
packages/next-auth/react
|
||||||
@@ -44,7 +44,6 @@ packages/next-auth/middleware.js
|
|||||||
# Development app
|
# Development app
|
||||||
apps/dev/src/css
|
apps/dev/src/css
|
||||||
apps/dev/prisma/migrations
|
apps/dev/prisma/migrations
|
||||||
apps/dev/typeorm
|
|
||||||
|
|
||||||
# VS
|
# VS
|
||||||
/.vs/slnx.sqlite-journal
|
/.vs/slnx.sqlite-journal
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ Anyone can be a contributor. Either you found a typo, or you have an awesome fea
|
|||||||
- The latest changes are always in `main`, so please make your Pull Request against that branch.
|
- The latest changes are always in `main`, so please make your Pull Request against that branch.
|
||||||
- Pull Requests should be raised for any change
|
- Pull Requests should be raised for any change
|
||||||
- Pull Requests need approval of a [core contributor](https://next-auth.js.org/contributors#core-team) before merging
|
- Pull Requests need approval of a [core contributor](https://next-auth.js.org/contributors#core-team) before merging
|
||||||
- We use ESLint/Prettier for linting/formatting, so please run `pnpm lint:fix` before committing to make resolving conflicts easier (VSCode users, check out [this ESLint extension](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) and [this Prettier extension](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode) to fix lint and formatting issues in development)
|
- We use ESLint/Prettier for linting/formatting, so please run `yarn lint:fix` before committing to make resolving conflicts easier (VSCode users, check out [this ESLint extension](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) and [this Prettier extension](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode) to fix lint and formatting issues in development)
|
||||||
- We encourage you to test your changes, and if you have the opportunity, please make those tests part of the Pull Request
|
- We encourage you to test your changes, and if you have the opportunity, please make those tests part of the Pull Request
|
||||||
- If you add new functionality, please provide the corresponding documentation as well and make it part of the Pull Request
|
- If you add new functionality, please provide the corresponding documentation as well and make it part of the Pull Request
|
||||||
|
|
||||||
@@ -37,7 +37,7 @@ cd next-auth
|
|||||||
1. Install packages. Developing requires Node.js v16:
|
1. Install packages. Developing requires Node.js v16:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
pnpm install
|
yarn
|
||||||
```
|
```
|
||||||
|
|
||||||
3. Populate `.env.local`:
|
3. Populate `.env.local`:
|
||||||
@@ -55,7 +55,7 @@ cp .env.local.example .env.local
|
|||||||
4. Start the developer application/server:
|
4. Start the developer application/server:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
pnpm dev
|
yarn dev:app
|
||||||
```
|
```
|
||||||
Your developer application will be available on `http://localhost:3000`
|
Your developer application will be available on `http://localhost:3000`
|
||||||
|
|
||||||
@@ -65,7 +65,7 @@ If you need an example project to link to, you can use [next-auth-example](https
|
|||||||
|
|
||||||
#### Hot reloading
|
#### Hot reloading
|
||||||
|
|
||||||
When running `pnpm dev`, you start a Next.js developer server on `http://localhost:3000`, which includes hot reloading out-of-the-box. Make changes on any of the files in `src` and see the changes immediately.
|
When running `yarn dev:app`, you start a Next.js developer server on `http://localhost:3000`, which includes hot reloading out of the box. Make changes on any of the files in `src` and see the changes immediately.
|
||||||
|
|
||||||
> NOTE: When working on CSS, you will have to manually refresh the page after changes. The reason for this is our pages using CSS are server-side rendered (using API routes). (Improving this through a PR is very welcome!)
|
> NOTE: When working on CSS, you will have to manually refresh the page after changes. The reason for this is our pages using CSS are server-side rendered (using API routes). (Improving this through a PR is very welcome!)
|
||||||
|
|
||||||
@@ -75,7 +75,7 @@ When running `pnpm dev`, you start a Next.js developer server on `http://localho
|
|||||||
|
|
||||||
If you think your custom provider might be useful to others, we encourage you to open a PR and add it to the built-in list so others can discover it much more easily! You only need to add two changes:
|
If you think your custom provider might be useful to others, we encourage you to open a PR and add it to the built-in list so others can discover it much more easily! You only need to add two changes:
|
||||||
|
|
||||||
1. Add your config: [`src/providers/{provider}.js`](https://github.com/nextauthjs/next-auth/tree/main/packages/next-auth/src/providers) (Make sure you use a named default export, like `export default function YourProvider`!)
|
1. Add your config: [`src/providers/{provider}.js`](https://github.com/nextauthjs/next-auth/tree/main/src/providers) (Make sure you use a named default export, like `export default function YourProvider`!)
|
||||||
2. Add provider documentation: [`www/docs/providers/{provider}.md`](https://github.com/nextauthjs/next-auth/tree/main/www/docs/providers)
|
2. Add provider documentation: [`www/docs/providers/{provider}.md`](https://github.com/nextauthjs/next-auth/tree/main/www/docs/providers)
|
||||||
|
|
||||||
That's it! 🎉 Others will be able to discover this provider much more easily now!
|
That's it! 🎉 Others will be able to discover this provider much more easily now!
|
||||||
@@ -88,13 +88,13 @@ If you would like to contribute to an existing database adapter or help create a
|
|||||||
|
|
||||||
#### Testing
|
#### Testing
|
||||||
|
|
||||||
Tests can be run with `pnpm test`.
|
Tests can be run with `yarn test`.
|
||||||
|
|
||||||
Automated tests are currently crude and limited in functionality, but improvements are in development.
|
Automated tests are currently crude and limited in functionality, but improvements are in development.
|
||||||
|
|
||||||
## For maintainers
|
## For maintainers
|
||||||
|
|
||||||
We use [a custom script](https://github.com/nextauthjs/next-auth/blob/main/scripts/release/index.ts) together with [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0) to automate releases. This makes the maintenance process easier and less error-prone. Please study the "Conventional Commits" site to understand how to write a good commit message.
|
We use [a custom script](https://github.com/nextauthjs/next-auth/tree/main/scripts/index.ts) together with [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0) to automate releases. This makes the maintenance process easier and less error-prone. Please study the "Conventional Commits" site to understand how to write a good commit message.
|
||||||
|
|
||||||
When accepting Pull Requests, make sure the following:
|
When accepting Pull Requests, make sure the following:
|
||||||
|
|
||||||
@@ -103,9 +103,9 @@ When accepting Pull Requests, make sure the following:
|
|||||||
- Rewrite the commit message to conform to the `Conventional Commits` style.
|
- Rewrite the commit message to conform to the `Conventional Commits` style.
|
||||||
- Using `fix` releases a patch (x.x.1)
|
- Using `fix` releases a patch (x.x.1)
|
||||||
- Using `feat` releases a minor (x.1.x)
|
- Using `feat` releases a minor (x.1.x)
|
||||||
- Using `feat` when `BREAKING CHANGE` is present in the commit message releases a major (1.x.x)
|
- Using `feat` when `BREAKING CHANGE` is present in the commit messgae releases a major (1.x.x)
|
||||||
- Optionally link issues the PR will resolve (You can add "close" in front of the issue numbers to close the issues automatically, when the PR is merged. `semantic-release` will also comment back to connected issues and PRs, notifying the users that a feature is added/bug fixed, etc.)
|
- Optionally link issues the PR will resolve (You can add "close" in front of the issue numbers to close the issues automatically, when the PR is merged. `semantic-release` will also comment back to connected issues and PRs, notifying the users that a feature is added/bug fixed, etc.)
|
||||||
|
|
||||||
### Skipping a release
|
### Skipping a release
|
||||||
|
|
||||||
If a commit contains `[skip release]` in their message, it will be excluded from the commit analysis and won't participate in the release type determination. This is useful, if the PR being merged should not trigger a new `npm` release.
|
If a commit contains `[skip release]` in their message will be excluded from the commit analysis and won't participate in the release type determination. This is useful, if the PR being merged should not trigger a new `npm` release.
|
||||||
@@ -50,6 +50,3 @@ DATABASE_URL=
|
|||||||
BOXYHQSAML_ISSUER="https://jackson-demo.boxyhq.com"
|
BOXYHQSAML_ISSUER="https://jackson-demo.boxyhq.com"
|
||||||
BOXYHQSAML_ID="tenant=boxyhq.com&product=saml-demo.boxyhq.com"
|
BOXYHQSAML_ID="tenant=boxyhq.com&product=saml-demo.boxyhq.com"
|
||||||
BOXYHQSAML_SECRET="dummy"
|
BOXYHQSAML_SECRET="dummy"
|
||||||
|
|
||||||
WIKIMEDIA_ID=
|
|
||||||
WIKIMEDIA_SECRET=
|
|
||||||
@@ -16,25 +16,21 @@
|
|||||||
},
|
},
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@next-auth/fauna-adapter": "workspace:*",
|
"@next-auth/fauna-adapter": "^1.0.1",
|
||||||
"@next-auth/prisma-adapter": "workspace:*",
|
"@next-auth/prisma-adapter": "^1.0.1",
|
||||||
"@next-auth/typeorm-legacy-adapter": "workspace:*",
|
"@prisma/client": "^3.10.0",
|
||||||
"@prisma/client": "^3",
|
|
||||||
"faunadb": "^4",
|
|
||||||
"next": "12.2.0",
|
|
||||||
"nodemailer": "^6",
|
|
||||||
"react": "^18",
|
|
||||||
"react-dom": "^18"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"@types/react": "^18.0.15",
|
|
||||||
"@types/react-dom": "^18.0.6",
|
|
||||||
"concurrently": "^7",
|
|
||||||
"cpx": "^1.5.0",
|
"cpx": "^1.5.0",
|
||||||
"fake-smtp-server": "^0.8.0",
|
"fake-smtp-server": "^0.8.0",
|
||||||
"pg": "^8.7.3",
|
"faunadb": "^4.4.1",
|
||||||
"prisma": "^3",
|
"next": "^12.1.0",
|
||||||
"sqlite3": "^5.0.8",
|
"nodemailer": "^6.7.2",
|
||||||
"typeorm": "0.3.7"
|
"react": "^17.0.2",
|
||||||
|
"react-dom": "^17.0.2"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/react": "^17.0.37",
|
||||||
|
"@types/react-dom": "^17.0.11",
|
||||||
|
"concurrently": "^7.1.0",
|
||||||
|
"prisma": "^3.10.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,11 +4,10 @@ import GitHubProvider from "next-auth/providers/github"
|
|||||||
import Auth0Provider from "next-auth/providers/auth0"
|
import Auth0Provider from "next-auth/providers/auth0"
|
||||||
import KeycloakProvider from "next-auth/providers/keycloak"
|
import KeycloakProvider from "next-auth/providers/keycloak"
|
||||||
import TwitterProvider, {
|
import TwitterProvider, {
|
||||||
// TwitterLegacy as TwitterLegacyProvider,
|
TwitterLegacy as TwitterLegacyProvider,
|
||||||
} from "next-auth/providers/twitter"
|
} from "next-auth/providers/twitter"
|
||||||
import CredentialsProvider from "next-auth/providers/credentials"
|
import CredentialsProvider from "next-auth/providers/credentials"
|
||||||
import IDS4Provider from "next-auth/providers/identity-server4"
|
import IDS4Provider from "next-auth/providers/identity-server4"
|
||||||
import DuendeIDS6Provider from "next-auth/providers/duende-identity-server6"
|
|
||||||
import Twitch from "next-auth/providers/twitch"
|
import Twitch from "next-auth/providers/twitch"
|
||||||
import GoogleProvider from "next-auth/providers/google"
|
import GoogleProvider from "next-auth/providers/google"
|
||||||
import FacebookProvider from "next-auth/providers/facebook"
|
import FacebookProvider from "next-auth/providers/facebook"
|
||||||
@@ -32,39 +31,20 @@ import PatreonProvider from "next-auth/providers/patreon"
|
|||||||
import TraktProvider from "next-auth/providers/trakt"
|
import TraktProvider from "next-auth/providers/trakt"
|
||||||
import WorkOSProvider from "next-auth/providers/workos"
|
import WorkOSProvider from "next-auth/providers/workos"
|
||||||
import BoxyHQSAMLProvider from "next-auth/providers/boxyhq-saml"
|
import BoxyHQSAMLProvider from "next-auth/providers/boxyhq-saml"
|
||||||
import WikimediaProvider from "next-auth/providers/wikimedia"
|
|
||||||
import VkProvider from "next-auth/providers/vk"
|
|
||||||
|
|
||||||
// TypeORM
|
|
||||||
// import { TypeORMLegacyAdapter } from "@next-auth/typeorm-legacy-adapter"
|
|
||||||
// const adapter = TypeORMLegacyAdapter({
|
|
||||||
// type: "sqlite",
|
|
||||||
// name: "next-auth-test-memory",
|
|
||||||
// database: "./typeorm/dev.db",
|
|
||||||
// synchronize: true,
|
|
||||||
// })
|
|
||||||
|
|
||||||
// // Prisma
|
|
||||||
// import { PrismaAdapter } from "@next-auth/prisma-adapter"
|
// import { PrismaAdapter } from "@next-auth/prisma-adapter"
|
||||||
// import { PrismaClient } from "@prisma/client"
|
// import { PrismaClient } from "@prisma/client"
|
||||||
// const prisma = new PrismaClient()
|
// const prisma = new PrismaClient()
|
||||||
// const adapter = PrismaAdapter(prisma)
|
// const adapter = PrismaAdapter(prisma)
|
||||||
|
|
||||||
// // Fauna
|
|
||||||
// import { Client as FaunaClient } from "faunadb"
|
// import { Client as FaunaClient } from "faunadb"
|
||||||
// import { FaunaAdapter } from "@next-auth/fauna-adapter"
|
// import { FaunaAdapter } from "@next-auth/fauna-adapter"
|
||||||
|
|
||||||
// const client = new FaunaClient({
|
// const client = new FaunaClient({
|
||||||
// secret: process.env.FAUNA_SECRET,
|
// secret: process.env.FAUNA_SECRET,
|
||||||
// domain: process.env.FAUNA_DOMAIN,
|
// domain: process.env.FAUNA_DOMAIN,
|
||||||
// })
|
// })
|
||||||
// const adapter = FaunaAdapter(client)
|
// const adapter = FaunaAdapter(client)
|
||||||
|
|
||||||
// // Dummy
|
|
||||||
// const adapter: any = {
|
|
||||||
// getUserByEmail: (email) => ({ id: "1", email, emailVerified: null }),
|
|
||||||
// createVerificationToken: (token) => token,
|
|
||||||
// }
|
|
||||||
|
|
||||||
export const authOptions: NextAuthOptions = {
|
export const authOptions: NextAuthOptions = {
|
||||||
// adapter,
|
// adapter,
|
||||||
providers: [
|
providers: [
|
||||||
@@ -167,11 +147,6 @@ export const authOptions: NextAuthOptions = {
|
|||||||
clientSecret: process.env.IDS4_SECRET,
|
clientSecret: process.env.IDS4_SECRET,
|
||||||
issuer: process.env.IDS4_ISSUER,
|
issuer: process.env.IDS4_ISSUER,
|
||||||
}),
|
}),
|
||||||
DuendeIDS6Provider({
|
|
||||||
clientId: "interactive.confidential",
|
|
||||||
clientSecret: "secret",
|
|
||||||
issuer: "https://demo.duendesoftware.com",
|
|
||||||
}),
|
|
||||||
DiscordProvider({
|
DiscordProvider({
|
||||||
clientId: process.env.DISCORD_ID,
|
clientId: process.env.DISCORD_ID,
|
||||||
clientSecret: process.env.DISCORD_SECRET,
|
clientSecret: process.env.DISCORD_SECRET,
|
||||||
@@ -227,18 +202,10 @@ export const authOptions: NextAuthOptions = {
|
|||||||
clientSecret: process.env.WORKOS_SECRET,
|
clientSecret: process.env.WORKOS_SECRET,
|
||||||
}),
|
}),
|
||||||
BoxyHQSAMLProvider({
|
BoxyHQSAMLProvider({
|
||||||
issuer: process.env.BOXYHQSAML_ISSUER ?? "https://example.com",
|
issuer: process.env.BOXYHQSAML_ISSUER,
|
||||||
clientId: process.env.BOXYHQSAML_ID,
|
clientId: process.env.BOXYHQSAML_ID,
|
||||||
clientSecret: process.env.BOXYHQSAML_SECRET,
|
clientSecret: process.env.BOXYHQSAML_SECRET,
|
||||||
}),
|
}),
|
||||||
WikimediaProvider({
|
|
||||||
clientId: process.env.WIKIMEDIA_ID,
|
|
||||||
clientSecret: process.env.WIKIMEDIA_SECRET,
|
|
||||||
}),
|
|
||||||
VkProvider({
|
|
||||||
clientId: process.env.VK_ID,
|
|
||||||
clientSecret: process.env.VK_SECRET
|
|
||||||
}),
|
|
||||||
],
|
],
|
||||||
debug: true,
|
debug: true,
|
||||||
theme: {
|
theme: {
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
// This is an example of to protect an API route
|
// This is an example of to protect an API route
|
||||||
import { unstable_getServerSession } from "next-auth/next"
|
import { getSession } from "next-auth/react"
|
||||||
|
|
||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
const session = await unstable_getServerSession(req, res, options)
|
const session = await getSession({ req })
|
||||||
|
|
||||||
if (session) {
|
if (session) {
|
||||||
res.send({
|
res.send({
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
// This is an example of how to access a session from an API route
|
// This is an example of how to access a session from an API route
|
||||||
import { unstable_getServerSession } from "next-auth/next"
|
import { getSession } from "next-auth/react"
|
||||||
|
|
||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
const session = await unstable_getServerSession(req, res, authOptions)
|
const session = await getSession({ req })
|
||||||
res.send(JSON.stringify(session, null, 2))
|
res.send(JSON.stringify(session, null, 2))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
export { default } from "next-auth/middleware"
|
export { default } from "next-auth/middleware"
|
||||||
|
|
||||||
export const config = { matcher: ["/middleware-protected"] }
|
|
||||||
|
|
||||||
// Other ways to use this middleware
|
// Other ways to use this middleware
|
||||||
|
|
||||||
// import withAuth from "next-auth/middleware"
|
// import withAuth from "next-auth/middleware"
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
// This is an example of how to protect content using server rendering
|
// This is an example of how to protect content using server rendering
|
||||||
import { unstable_getServerSession } from "next-auth/next"
|
import { getServerSession } from "next-auth/next"
|
||||||
import { authOptions } from "./api/auth/[...nextauth]"
|
import { authOptions } from "./api/auth/[...nextauth]"
|
||||||
import Layout from "../components/layout"
|
import Layout from "../components/layout"
|
||||||
import AccessDenied from "../components/access-denied"
|
import AccessDenied from "../components/access-denied"
|
||||||
@@ -26,11 +26,7 @@ export default function Page({ content, session }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function getServerSideProps(context) {
|
export async function getServerSideProps(context) {
|
||||||
const session = await unstable_getServerSession(
|
const session = await getServerSession(context, authOptions)
|
||||||
context.req,
|
|
||||||
context.res,
|
|
||||||
authOptions
|
|
||||||
)
|
|
||||||
let content = null
|
let content = null
|
||||||
|
|
||||||
if (session) {
|
if (session) {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { unstable_getServerSession } from "next-auth/next"
|
import { getSession } from "next-auth/react"
|
||||||
import Layout from "../components/layout"
|
import Layout from "../components/layout"
|
||||||
|
|
||||||
export default function Page() {
|
export default function Page() {
|
||||||
@@ -11,17 +11,13 @@ export default function Page() {
|
|||||||
<Layout>
|
<Layout>
|
||||||
<h1>Server Side Rendering</h1>
|
<h1>Server Side Rendering</h1>
|
||||||
<p>
|
<p>
|
||||||
This page uses the <strong>unstable_getServerSession()</strong> method
|
This page uses the universal <strong>getSession()</strong> method in{" "}
|
||||||
in <strong>getServerSideProps()</strong>.
|
<strong>getServerSideProps()</strong>.
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Using <strong>unstable_getServerSession()</strong> in{" "}
|
Using <strong>getSession()</strong> in{" "}
|
||||||
<strong>getServerSideProps()</strong> is currently the recommended
|
<strong>getServerSideProps()</strong> is the recommended approach if you
|
||||||
approach, although the API may still change, if you need to support
|
need to support Server Side Rendering with authentication.
|
||||||
Server Side Rendering with authentication.
|
|
||||||
</p>
|
|
||||||
<p>
|
|
||||||
Using <strong>getSession()</strong> is still recommended on the client.
|
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
The advantage of Server Side Rendering is this page does not require
|
The advantage of Server Side Rendering is this page does not require
|
||||||
@@ -39,11 +35,7 @@ export default function Page() {
|
|||||||
export async function getServerSideProps(context) {
|
export async function getServerSideProps(context) {
|
||||||
return {
|
return {
|
||||||
props: {
|
props: {
|
||||||
session: await unstable_getServerSession(
|
session: await getSession(context),
|
||||||
contex.req,
|
|
||||||
contex.res,
|
|
||||||
authOptions
|
|
||||||
),
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ You **can** skip configuring a database and come back to it later if you want.
|
|||||||
For more information about setting up a database, please check out the following links:
|
For more information about setting up a database, please check out the following links:
|
||||||
|
|
||||||
* Docs: [next-auth.js.org/adapters/overview](https://next-auth.js.org/adapters/overview)
|
* Docs: [next-auth.js.org/adapters/overview](https://next-auth.js.org/adapters/overview)
|
||||||
|
* Adapters Repo: [nextauthjs/adapters](https://github.com/nextauthjs/adapters)
|
||||||
|
|
||||||
### 3. Configure Authentication Providers
|
### 3. Configure Authentication Providers
|
||||||
|
|
||||||
|
|||||||
@@ -12,9 +12,9 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"dotenv": "^16.0.0",
|
"dotenv": "^16.0.0",
|
||||||
"gatsby": "next",
|
"gatsby": "next",
|
||||||
"next-auth": "latest",
|
"next-auth": "^4.2.1",
|
||||||
"react": "^18",
|
"react": "^17.0.2",
|
||||||
"react-dom": "^18"
|
"react-dom": "^17.0.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"vercel": "^23.1.2"
|
"vercel": "^23.1.2"
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ You **can** skip configuring a database and come back to it later if you want.
|
|||||||
For more information about setting up a database, please check out the following links:
|
For more information about setting up a database, please check out the following links:
|
||||||
|
|
||||||
* Docs: [next-auth.js.org/adapters/overview](https://next-auth.js.org/adapters/overview)
|
* Docs: [next-auth.js.org/adapters/overview](https://next-auth.js.org/adapters/overview)
|
||||||
|
* Adapters Repo: [nextauthjs/adapters](https://github.com/nextauthjs/adapters)
|
||||||
|
|
||||||
### 3. Configure Authentication Providers
|
### 3. Configure Authentication Providers
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
import { withAuth } from "next-auth/middleware"
|
|
||||||
|
|
||||||
// More on how NextAuth.js middleware works: https://next-auth.js.org/configuration/nextjs#middleware
|
|
||||||
export default withAuth({
|
|
||||||
callbacks: {
|
|
||||||
authorized: ({ req, token }) =>
|
|
||||||
// /admin requires admin role, but /me only requires the user to be logged in.
|
|
||||||
req.nextUrl.pathname !== "/admin" || token?.userRole === "admin",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
export const config = { matcher: ["/admin", "/me"] }
|
|
||||||
@@ -1,15 +1,19 @@
|
|||||||
{
|
{
|
||||||
|
"name": "next-auth-example",
|
||||||
|
"version": "0.0.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"description": "An example project for NextAuth.js with Next.js",
|
"description": "An example project for NextAuth.js",
|
||||||
"repository": "https://github.com/nextauthjs/next-auth-example.git",
|
"repository": "https://github.com/nextauthjs/next-auth-example.git",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/nextauthjs/next-auth/issues"
|
"url": "https://github.com/nextauthjs/next-auth/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://next-auth-example.vercel.app",
|
"homepage": "https://next-auth-example.vercel.app",
|
||||||
|
"main": "",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "next",
|
"dev": "next",
|
||||||
"build": "next build",
|
"build": "next build",
|
||||||
"start": "next start"
|
"start": "next start",
|
||||||
|
"types": "tsc --noEmit"
|
||||||
},
|
},
|
||||||
"author": "Iain Collins <me@iaincollins.com>",
|
"author": "Iain Collins <me@iaincollins.com>",
|
||||||
"contributors": [
|
"contributors": [
|
||||||
@@ -17,16 +21,20 @@
|
|||||||
"Nico Domino <yo@ndo.dev>",
|
"Nico Domino <yo@ndo.dev>",
|
||||||
"Lluis Agusti <hi@llu.lu>"
|
"Lluis Agusti <hi@llu.lu>"
|
||||||
],
|
],
|
||||||
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"next": "latest",
|
"next": "^12.0.11-canary.4",
|
||||||
"next-auth": "latest",
|
"next-auth": "latest",
|
||||||
"nodemailer": "^6",
|
"nodemailer": "^6.6.3",
|
||||||
"react": "^18.2.0",
|
"react": "^17.0.2",
|
||||||
"react-dom": "^18.2.0"
|
"react-dom": "^17.0.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^17",
|
"@types/node": "^17.0.14",
|
||||||
"@types/react": "^18.0.15",
|
"@types/react": "^17.0.39",
|
||||||
"typescript": "^4"
|
"typescript": "^4.5.5"
|
||||||
|
},
|
||||||
|
"prettier": {
|
||||||
|
"semi": false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { SessionProvider } from "next-auth/react"
|
import { SessionProvider } from "next-auth/react"
|
||||||
import "./styles.css"
|
|
||||||
|
|
||||||
import type { AppProps } from "next/app"
|
import type { AppProps } from "next/app"
|
||||||
|
import "./styles.css"
|
||||||
|
|
||||||
// Use of the <SessionProvider> is mandatory to allow components that call
|
// Use of the <SessionProvider> is mandatory to allow components that call
|
||||||
// `useSession()` anywhere in your application to access the `session` object.
|
// `useSession()` anywhere in your application to access the `session` object.
|
||||||
|
|||||||
8
apps/example-nextjs/pages/admin/_middleware.ts
Normal file
8
apps/example-nextjs/pages/admin/_middleware.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import { withAuth } from "next-auth/middleware"
|
||||||
|
|
||||||
|
// More on how NextAuth.js middleware works: https://next-auth.js.org/configuration/nextjs#middleware
|
||||||
|
export default withAuth({
|
||||||
|
callbacks: {
|
||||||
|
authorized: ({ token }) => token?.userRole === "admin",
|
||||||
|
},
|
||||||
|
})
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import Layout from "../components/layout"
|
import Layout from "../../components/layout"
|
||||||
|
|
||||||
export default function Page() {
|
export default function Page() {
|
||||||
return (
|
return (
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import NextAuth, { NextAuthOptions } from "next-auth"
|
import NextAuth from "next-auth"
|
||||||
import GoogleProvider from "next-auth/providers/google"
|
import GoogleProvider from "next-auth/providers/google"
|
||||||
import FacebookProvider from "next-auth/providers/facebook"
|
import FacebookProvider from "next-auth/providers/facebook"
|
||||||
import GithubProvider from "next-auth/providers/github"
|
import GithubProvider from "next-auth/providers/github"
|
||||||
@@ -9,7 +9,7 @@ import Auth0Provider from "next-auth/providers/auth0"
|
|||||||
|
|
||||||
// For more information on each option (and a full list of options) go to
|
// For more information on each option (and a full list of options) go to
|
||||||
// https://next-auth.js.org/configuration/options
|
// https://next-auth.js.org/configuration/options
|
||||||
export const authOptions: NextAuthOptions = {
|
export default NextAuth({
|
||||||
// https://next-auth.js.org/configuration/providers/oauth
|
// https://next-auth.js.org/configuration/providers/oauth
|
||||||
providers: [
|
providers: [
|
||||||
/* EmailProvider({
|
/* EmailProvider({
|
||||||
@@ -60,6 +60,4 @@ export const authOptions: NextAuthOptions = {
|
|||||||
return token
|
return token
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
})
|
||||||
|
|
||||||
export default NextAuth(authOptions)
|
|
||||||
|
|||||||
@@ -1,14 +1,10 @@
|
|||||||
// This is an example of how to read a JSON Web Token from an API route
|
// This is an example of how to read a JSON Web Token from an API route
|
||||||
import { getToken } from "next-auth/jwt"
|
import { getToken } from "next-auth/jwt"
|
||||||
|
|
||||||
import type { NextApiRequest, NextApiResponse } from "next"
|
import type { NextApiRequest, NextApiResponse } from "next"
|
||||||
|
|
||||||
const secret = process.env.NEXTAUTH_SECRET
|
const secret = process.env.NEXTAUTH_SECRET
|
||||||
|
|
||||||
export default async function handler(
|
export default async (req: NextApiRequest, res: NextApiResponse) => {
|
||||||
req: NextApiRequest,
|
|
||||||
res: NextApiResponse
|
|
||||||
) {
|
|
||||||
const token = await getToken({ req, secret })
|
const token = await getToken({ req, secret })
|
||||||
res.send(JSON.stringify(token, null, 2))
|
res.send(JSON.stringify(token, null, 2))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +1,18 @@
|
|||||||
// This is an example of to protect an API route
|
// This is an example of to protect an API route
|
||||||
import { unstable_getServerSession } from "next-auth/next"
|
import { getSession } from "next-auth/react"
|
||||||
import { authOptions } from "../auth/[...nextauth]"
|
|
||||||
|
|
||||||
import type { NextApiRequest, NextApiResponse } from "next"
|
import type { NextApiRequest, NextApiResponse } from "next"
|
||||||
|
|
||||||
export default async function handler(
|
export default async (req: NextApiRequest, res: NextApiResponse) => {
|
||||||
req: NextApiRequest,
|
const session = await getSession({ req })
|
||||||
res: NextApiResponse
|
|
||||||
) {
|
|
||||||
const session = await unstable_getServerSession(req, res, authOptions)
|
|
||||||
|
|
||||||
if (session) {
|
if (session) {
|
||||||
return res.send({
|
res.send({
|
||||||
content:
|
content:
|
||||||
"This is protected content. You can access this content because you are signed in.",
|
"This is protected content. You can access this content because you are signed in.",
|
||||||
})
|
})
|
||||||
}
|
} else {
|
||||||
|
|
||||||
res.send({
|
res.send({
|
||||||
error: "You must be signed in to view the protected content on this page.",
|
error: "You must be signed in to view the protected content on this page.",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,13 +1,8 @@
|
|||||||
// This is an example of how to access a session from an API route
|
// This is an example of how to access a session from an API route
|
||||||
import { unstable_getServerSession } from "next-auth"
|
import { getSession } from "next-auth/react"
|
||||||
import { authOptions } from "../auth/[...nextauth]"
|
|
||||||
|
|
||||||
import type { NextApiRequest, NextApiResponse } from "next"
|
import type { NextApiRequest, NextApiResponse } from "next"
|
||||||
|
|
||||||
export default async function handler(
|
export default async (req: NextApiRequest, res: NextApiResponse) => {
|
||||||
req: NextApiRequest,
|
const session = await getSession({ req })
|
||||||
res: NextApiResponse
|
|
||||||
) {
|
|
||||||
const session = await unstable_getServerSession(req, res, authOptions)
|
|
||||||
res.send(JSON.stringify(session, null, 2))
|
res.send(JSON.stringify(session, null, 2))
|
||||||
}
|
}
|
||||||
|
|||||||
2
apps/example-nextjs/pages/me/_middleware.ts
Normal file
2
apps/example-nextjs/pages/me/_middleware.ts
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
// More on how NextAuth.js middleware works: https://next-auth.js.org/configuration/nextjs#middleware
|
||||||
|
export { default } from "next-auth/middleware"
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { useSession } from "next-auth/react"
|
import { useSession } from "next-auth/react"
|
||||||
import Layout from "../components/layout"
|
import Layout from "../../components/layout"
|
||||||
|
|
||||||
export default function MePage() {
|
export default function MePage() {
|
||||||
const { data } = useSession()
|
const { data } = useSession()
|
||||||
@@ -1,25 +1,26 @@
|
|||||||
import { unstable_getServerSession } from "next-auth/next"
|
import { useSession, getSession } from "next-auth/react"
|
||||||
import { authOptions } from "./api/auth/[...nextauth]"
|
|
||||||
import Layout from "../components/layout"
|
import Layout from "../components/layout"
|
||||||
|
import type { NextPageContext } from "next"
|
||||||
|
|
||||||
import type { GetServerSidePropsContext } from "next"
|
export default function ServerSidePage() {
|
||||||
import type { Session } from "next-auth"
|
|
||||||
|
|
||||||
export default function ServerSidePage({ session }: { session: Session }) {
|
|
||||||
// As this page uses Server Side Rendering, the `session` will be already
|
// As this page uses Server Side Rendering, the `session` will be already
|
||||||
// populated on render without needing to go through a loading stage.
|
// populated on render without needing to go through a loading stage.
|
||||||
|
// This is possible because of the shared context configured in `_app.js` that
|
||||||
|
// is used by `useSession()`.
|
||||||
|
const { data: session, status } = useSession()
|
||||||
|
const loading = status === "loading"
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Layout>
|
<Layout>
|
||||||
<h1>Server Side Rendering</h1>
|
<h1>Server Side Rendering</h1>
|
||||||
<p>
|
<p>
|
||||||
This page uses the <strong>unstable_getServerSession()</strong> method
|
This page uses the universal <strong>getSession()</strong> method in{" "}
|
||||||
in <strong>unstable_getServerSideProps()</strong>.
|
<strong>getServerSideProps()</strong>.
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Using <strong>unstable_getServerSession()</strong> in{" "}
|
Using <strong>getSession()</strong> in{" "}
|
||||||
<strong>unstable_getServerSideProps()</strong> is the recommended
|
<strong>getServerSideProps()</strong> is the recommended approach if you
|
||||||
approach if you need to support Server Side Rendering with
|
need to support Server Side Rendering with authentication.
|
||||||
authentication.
|
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
The advantage of Server Side Rendering is this page does not require
|
The advantage of Server Side Rendering is this page does not require
|
||||||
@@ -29,20 +30,15 @@ export default function ServerSidePage({ session }: { session: Session }) {
|
|||||||
The disadvantage of Server Side Rendering is that this page is slower to
|
The disadvantage of Server Side Rendering is that this page is slower to
|
||||||
render.
|
render.
|
||||||
</p>
|
</p>
|
||||||
<pre>{JSON.stringify(session, null, 2)}</pre>
|
|
||||||
</Layout>
|
</Layout>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Export the `session` prop to use sessions with Server Side Rendering
|
// Export the `session` prop to use sessions with Server Side Rendering
|
||||||
export async function getServerSideProps(context: GetServerSidePropsContext) {
|
export async function getServerSideProps(context: NextPageContext) {
|
||||||
return {
|
return {
|
||||||
props: {
|
props: {
|
||||||
session: await unstable_getServerSession(
|
session: await getSession(context),
|
||||||
context.req,
|
|
||||||
context.res,
|
|
||||||
authOptions
|
|
||||||
),
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,7 @@
|
|||||||
"type": "module",
|
"type": "module",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"cookie": "0.4.1",
|
"cookie": "0.4.1",
|
||||||
"next-auth": "workspace:*"
|
"next-auth": "^4.3.3"
|
||||||
},
|
},
|
||||||
"prettier": {
|
"prettier": {
|
||||||
"semi": false,
|
"semi": false,
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ async function SKNextAuthHandler(
|
|||||||
query: Object.fromEntries(url.searchParams),
|
query: Object.fromEntries(url.searchParams),
|
||||||
headers: request.headers,
|
headers: request.headers,
|
||||||
method: request.method,
|
method: request.method,
|
||||||
cookies: cookie.parse(request.headers.get("cookie") ?? ""),
|
cookies: cookie.parse(request.headers.get("cookie")),
|
||||||
action: nextauth[0] as NextAuthAction,
|
action: nextauth[0] as NextAuthAction,
|
||||||
providerId: nextauth[1],
|
providerId: nextauth[1],
|
||||||
error: nextauth[1],
|
error: nextauth[1],
|
||||||
@@ -91,7 +91,7 @@ export async function getServerSession(
|
|||||||
host: import.meta.env.VITE_NEXTAUTH_URL,
|
host: import.meta.env.VITE_NEXTAUTH_URL,
|
||||||
action: "session",
|
action: "session",
|
||||||
method: "GET",
|
method: "GET",
|
||||||
cookies: cookie.parse(request.headers.get("cookie") ?? ""),
|
cookies: cookie.parse(request.headers.get("cookie")),
|
||||||
headers: request.headers,
|
headers: request.headers,
|
||||||
},
|
},
|
||||||
options,
|
options,
|
||||||
|
|||||||
@@ -1232,10 +1232,10 @@ natural-compare@^1.4.0:
|
|||||||
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
|
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
|
||||||
integrity sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=
|
integrity sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=
|
||||||
|
|
||||||
"next-auth@workspace:*":
|
next-auth@^4.3.3:
|
||||||
version "4.9.0"
|
version "4.3.3"
|
||||||
resolved "https://registry.yarnpkg.com/next-auth/-/next-auth-4.9.0.tgz#0d8cabcb22a976744131a2e68d5f08756f322593"
|
resolved "https://registry.yarnpkg.com/next-auth/-/next-auth-4.3.3.tgz#5ff892e73648a0f33c2af0e9d7cafda729f63ae7"
|
||||||
integrity sha512-/4S5dFeyNg2nXlD7g/Sh5A4WZWnUMDpEf8x/x+gzmAf5cAY2SjDM6sLk9u4XRmsndsxQpIMWDw03sUTAD+Yzog==
|
integrity sha512-bUs+oOOPT18Pq/+4v9q4PA/DGoVoAX6jwY7RTfE/akFXwlny+y/mNS6lPSUwpqcHjljqBaq34PQA3+01SdOOPw==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@babel/runtime" "^7.16.3"
|
"@babel/runtime" "^7.16.3"
|
||||||
"@panva/hkdf" "^1.0.1"
|
"@panva/hkdf" "^1.0.1"
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ This is the Dgraph Adapter for [`next-auth`](https://next-auth.js.org).
|
|||||||
|
|
||||||
1. Install the necessary packages
|
1. Install the necessary packages
|
||||||
|
|
||||||
```bash npm2yarn2pnpm
|
```bash npm2yarn
|
||||||
npm install next-auth @next-auth/dgraph-adapter
|
npm install next-auth @next-auth/dgraph-adapter
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -226,22 +226,22 @@ database you must customize next-auth `encode` and `decode` functions, as the de
|
|||||||
further customize the jwt with roles if you want to implement [`RBAC logic`](https://dgraph.io/docs/graphql/authorization/directive/#role-based-access-control).
|
further customize the jwt with roles if you want to implement [`RBAC logic`](https://dgraph.io/docs/graphql/authorization/directive/#role-based-access-control).
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import * as jwt from "jsonwebtoken"
|
import * as jwt from "jsonwebtoken";
|
||||||
export default NextAuth({
|
export default NextAuth({
|
||||||
session: {
|
session: {
|
||||||
strategy: "jwt",
|
strategy: "jwt"
|
||||||
},
|
},
|
||||||
jwt: {
|
jwt: {
|
||||||
secret: process.env.SECRET,
|
secret: process.env.SECRET,
|
||||||
encode: async ({ secret, token }) => {
|
encode: async ({ secret, token }) => {
|
||||||
return jwt.sign({...token, userId: token.id}, secret, {
|
return jwt.sign({...token, userId: token.id}, secret, {
|
||||||
algorithm: "HS256",
|
algorithm: "HS256",
|
||||||
expiresIn: 30 * 24 * 60 * 60, // 30 days
|
expiresIn: 30 * 24 * 60 * 60; // 30 days
|
||||||
})
|
});
|
||||||
},
|
},
|
||||||
decode: async ({ secret, token }) => {
|
decode: async ({ secret, token }) => {
|
||||||
return jwt.verify(token, secret, { algorithms: ["HS256"] })
|
return jwt.verify(token, secret, { algorithms: ["HS256"] });
|
||||||
},
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ You can find the full schema in the table structure section below.
|
|||||||
|
|
||||||
1. Install `next-auth` and `@next-auth/dynamodb-adapter`
|
1. Install `next-auth` and `@next-auth/dynamodb-adapter`
|
||||||
|
|
||||||
```bash npm2yarn2pnpm
|
```bash npm2yarn
|
||||||
npm install next-auth @next-auth/dynamodb-adapter
|
npm install next-auth @next-auth/dynamodb-adapter
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ You can find the Fauna schema and seed information in the docs at [next-auth.js.
|
|||||||
|
|
||||||
1. Install the necessary packages
|
1. Install the necessary packages
|
||||||
|
|
||||||
```bash npm2yarn2pnpm
|
```bash npm2yarn
|
||||||
npm install next-auth @next-auth/fauna-adapter faunadb
|
npm install next-auth @next-auth/fauna-adapter faunadb
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -5,14 +5,18 @@ title: Firebase
|
|||||||
|
|
||||||
# Firebase
|
# Firebase
|
||||||
|
|
||||||
This is the Firebase (Firestore) Adapter for [`next-auth`](https://next-auth.js.org). This package can only be used in conjunction with the primary `next-auth` package. It is not a standalone package.
|
:::warning
|
||||||
|
This adapter is still experimental and does not work with NextAuth.js 4 or newer. If you would like to help out upgrading it, please visit [this PR](https://github.com/nextauthjs/next-auth/pull/3873)
|
||||||
|
:::
|
||||||
|
|
||||||
|
This is the Firebase Adapter for [`next-auth`](https://next-auth.js.org). This package can only be used in conjunction with the primary `next-auth` package. It is not a standalone package.
|
||||||
|
|
||||||
## Getting Started
|
## Getting Started
|
||||||
|
|
||||||
1. Install the necessary packages
|
1. Install the necessary packages
|
||||||
|
|
||||||
```bash npm2yarn2pnpm
|
```bash npm2yarn
|
||||||
npm install next-auth @next-auth/firebase-adapter
|
npm install next-auth @next-auth/firebase-adapter@experimental
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Add this adapter to your `pages/api/auth/[...nextauth].js` next-auth configuration object.
|
2. Add this adapter to your `pages/api/auth/[...nextauth].js` next-auth configuration object.
|
||||||
@@ -20,7 +24,14 @@ npm install next-auth @next-auth/firebase-adapter
|
|||||||
```javascript title="pages/api/auth/[...nextauth].js"
|
```javascript title="pages/api/auth/[...nextauth].js"
|
||||||
import NextAuth from "next-auth"
|
import NextAuth from "next-auth"
|
||||||
import GoogleProvider from "next-auth/providers/google"
|
import GoogleProvider from "next-auth/providers/google"
|
||||||
import { FirestoreAdapter } from "@next-auth/firebase-adapter"
|
import { FirebaseAdapter } from "@next-auth/firebase-adapter"
|
||||||
|
|
||||||
|
import firebase from "firebase/app"
|
||||||
|
import "firebase/firestore"
|
||||||
|
|
||||||
|
const firestore = (
|
||||||
|
firebase.apps[0] ?? firebase.initializeApp(/* your config */)
|
||||||
|
).firestore()
|
||||||
|
|
||||||
// For more information on each option (and a full list of options) go to
|
// For more information on each option (and a full list of options) go to
|
||||||
// https://next-auth.js.org/configuration/options
|
// https://next-auth.js.org/configuration/options
|
||||||
@@ -32,19 +43,9 @@ export default NextAuth({
|
|||||||
clientSecret: process.env.GOOGLE_SECRET,
|
clientSecret: process.env.GOOGLE_SECRET,
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
adapter: FirestoreAdapter({
|
adapter: FirebaseAdapter(firestore),
|
||||||
apiKey: process.env.FIREBASE_API_KEY,
|
...
|
||||||
appId: process.env.FIREBASE_APP_ID,
|
})
|
||||||
authDomain: process.env.FIREBASE_AUTH_DOMAIN,
|
|
||||||
databaseURL: process.env.FIREBASE_DATABASE_URL,
|
|
||||||
projectId: process.env.FIREBASE_PROJECT_ID,
|
|
||||||
storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
|
|
||||||
messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID,
|
|
||||||
// Optional emulator config (see below for options)
|
|
||||||
emulator: {},
|
|
||||||
}),
|
|
||||||
// ...
|
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Options
|
## Options
|
||||||
@@ -68,21 +69,6 @@ const firebaseConfig = {
|
|||||||
|
|
||||||
See [firebase.google.com/docs/web/setup](https://firebase.google.com/docs/web/setup) for more details.
|
See [firebase.google.com/docs/web/setup](https://firebase.google.com/docs/web/setup) for more details.
|
||||||
|
|
||||||
You can optionally pass in emulator options to automatically connect to your local Firebase emulator.
|
|
||||||
|
|
||||||
```js
|
|
||||||
FirestoreAdapter({
|
|
||||||
// ...
|
|
||||||
// Passing in an enable object will enable the emulator
|
|
||||||
emulator: {
|
|
||||||
// Optional host, defaults to `localhost`
|
|
||||||
host: 'localhost',
|
|
||||||
// Optional port, defaults to `3001`
|
|
||||||
port: 3001,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
```
|
|
||||||
|
|
||||||
:::tip **From Firebase**
|
:::tip **From Firebase**
|
||||||
|
|
||||||
**Caution**: We do not recommend manually modifying an app's Firebase config file or object. If you initialize an app with invalid or missing values for any of these required "Firebase options", then your end users may experience serious issues.
|
**Caution**: We do not recommend manually modifying an app's Firebase config file or object. If you initialize an app with invalid or missing values for any of these required "Firebase options", then your end users may experience serious issues.
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ title: MikroORM
|
|||||||
|
|
||||||
To use this Adapter, you need to install Mikro ORM, the driver that suits your database, and the separate `@next-auth/mikro-orm-adapter` package:
|
To use this Adapter, you need to install Mikro ORM, the driver that suits your database, and the separate `@next-auth/mikro-orm-adapter` package:
|
||||||
|
|
||||||
```bash npm2yarn2pnpm
|
```bash npm2yarn
|
||||||
npm install next-auth @next-auth/mikro-orm-adapter @mikro-orm/core @mikro-orm/[YOUR DRIVER]
|
npm install next-auth @next-auth/mikro-orm-adapter @mikro-orm/core @mikro-orm/[YOUR DRIVER]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ The MongoDB adapter does not handle connections automatically, so you will have
|
|||||||
|
|
||||||
1. Install the necessary packages
|
1. Install the necessary packages
|
||||||
|
|
||||||
```bash npm2yarn2pnpm
|
```bash npm2yarn
|
||||||
npm install next-auth @next-auth/mongodb-adapter mongodb
|
npm install next-auth @next-auth/mongodb-adapter mongodb
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ This is the Neo4j Adapter for [`next-auth`](https://next-auth.js.org). This pack
|
|||||||
|
|
||||||
1. Install the necessary packages
|
1. Install the necessary packages
|
||||||
|
|
||||||
```bash npm2yarn2pnpm
|
```bash npm2yarn
|
||||||
npm install next-auth @next-auth/neo4j-adapter neo4j-driver
|
npm install next-auth @next-auth/neo4j-adapter neo4j-driver
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ Depending on your architecture you can use PouchDB's http adapter to reach any d
|
|||||||
|
|
||||||
1. Install `next-auth` and `@next-auth/pouchdb-adapter`
|
1. Install `next-auth` and `@next-auth/pouchdb-adapter`
|
||||||
|
|
||||||
```bash npm2yarn2pnpm
|
```bash npm2yarn
|
||||||
npm install next-auth @next-auth/pouchdb-adapter
|
npm install next-auth @next-auth/pouchdb-adapter
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ title: Prisma
|
|||||||
|
|
||||||
To use this Adapter, you need to install Prisma Client, Prisma CLI, and the separate `@next-auth/prisma-adapter` package:
|
To use this Adapter, you need to install Prisma Client, Prisma CLI, and the separate `@next-auth/prisma-adapter` package:
|
||||||
|
|
||||||
```bash npm2yarn2pnpm
|
```bash npm2yarn
|
||||||
npm install next-auth @prisma/client @next-auth/prisma-adapter
|
npm install next-auth @prisma/client @next-auth/prisma-adapter
|
||||||
npm install prisma --save-dev
|
npm install prisma --save-dev
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ This is the Sequelize Adapter for [`next-auth`](https://next-auth.js.org).
|
|||||||
|
|
||||||
1. Install the necessary packages
|
1. Install the necessary packages
|
||||||
|
|
||||||
```bash npm2yarn2pnpm
|
```bash npm2yarn
|
||||||
npm install next-auth @next-auth/sequelize-adapter sequelize
|
npm install next-auth @next-auth/sequelize-adapter sequelize
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -5,25 +5,21 @@ title: TypeORM
|
|||||||
|
|
||||||
# TypeORM
|
# TypeORM
|
||||||
|
|
||||||
This Adapter is used to support SQL-flavored databases (like SQLite, MySQL, MSSQL, MariaDB, CockroachDB, etc.) through [TypeORM](https://typeorm.io).
|
This Adapter is used to support SQL-flavored databases (like SQLite, MySQL, MSSQL, MariaDB, CockroachDB, etc.) through [TypeORM](https://typeorm.io), and mostly kept around for legacy reasons. (See the warning below.)
|
||||||
|
|
||||||
:::note
|
:::note
|
||||||
If you previously used this Adapter with MongoDB, check out the [MongoDB Adapter](/adapters/mongodb) instead.
|
If you previously used this Adapter with MongoDB, check out the [MongoDB Adapter](/adapters/mongodb) instead.
|
||||||
:::
|
:::
|
||||||
|
|
||||||
:::note
|
:::warning
|
||||||
In the future, we might split up this adapter to support single flavors of SQL for easier maintenance and reduced bundle size.
|
In the future, we might split up this adapter to support single flavors of SQL for easier maintenance and reduced bundle size.
|
||||||
:::
|
:::
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
:::warning
|
|
||||||
[`typeorm`](https://github.com/typeorm/typeorm) is still in active development and has not yet published a stable release. Because of this, you can expect breaking changes in minor versions. This adapter expects `typeorm@0.3.7` and is not validated against previous or future releases.
|
|
||||||
:::
|
|
||||||
|
|
||||||
To use this Adapter, you need to install the following packages:
|
To use this Adapter, you need to install the following packages:
|
||||||
|
|
||||||
```bash npm2yarn2pnpm
|
```bash npm2yarn
|
||||||
npm install next-auth @next-auth/typeorm-legacy-adapter typeorm
|
npm install next-auth @next-auth/typeorm-legacy-adapter typeorm
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -40,7 +36,7 @@ export default NextAuth({
|
|||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
`TypeORMLegacyAdapter` takes either a connection string, or a [`DataSourceOptions`](https://github.com/typeorm/typeorm/blob/master/docs/data-source-options.md) object as its first parameter.
|
`TypeORMLegacyAdapter` takes either a connection string, or a [`ConnectionOptions`](https://github.com/typeorm/typeorm/blob/master/docs/connection-options.md) object as its first parameter.
|
||||||
|
|
||||||
## Custom models
|
## Custom models
|
||||||
|
|
||||||
@@ -221,9 +217,9 @@ For example, you can add the naming convention option to the connection object i
|
|||||||
import NextAuth from "next-auth"
|
import NextAuth from "next-auth"
|
||||||
import { TypeORMLegacyAdapter } from "@next-auth/typeorm-legacy-adapter"
|
import { TypeORMLegacyAdapter } from "@next-auth/typeorm-legacy-adapter"
|
||||||
import { SnakeNamingStrategy } from 'typeorm-naming-strategies'
|
import { SnakeNamingStrategy } from 'typeorm-naming-strategies'
|
||||||
|
import { ConnectionOptions } from "typeorm"
|
||||||
|
|
||||||
export default NextAuth({
|
const connection: ConnectionOptions = {
|
||||||
adapter: TypeORMLegacyAdapter({
|
|
||||||
type: "mysql",
|
type: "mysql",
|
||||||
host: "localhost",
|
host: "localhost",
|
||||||
port: 3306,
|
port: 3306,
|
||||||
@@ -231,7 +227,10 @@ export default NextAuth({
|
|||||||
password: "test",
|
password: "test",
|
||||||
database: "test",
|
database: "test",
|
||||||
namingStrategy: new SnakeNamingStrategy()
|
namingStrategy: new SnakeNamingStrategy()
|
||||||
}),
|
}
|
||||||
|
|
||||||
|
export default NextAuth({
|
||||||
|
adapter: TypeORMLegacyAdapter(connection),
|
||||||
...
|
...
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ title: Upstash Redis
|
|||||||
|
|
||||||
To use this Adapter, you need to install `@upstash/redis` and `@next-auth/upstash-redis-adapter` package:
|
To use this Adapter, you need to install `@upstash/redis` and `@next-auth/upstash-redis-adapter` package:
|
||||||
|
|
||||||
```bash npm2yarn2pnpm
|
```bash npm2yarn
|
||||||
npm install @upstash/redis @next-auth/upstash-redis-adapter
|
npm install @upstash/redis @next-auth/upstash-redis-adapter
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ The redirect callback may be invoked more than once in the same flow.
|
|||||||
This callback is called whenever a JSON Web Token is created (i.e. at sign
|
This callback is called whenever a JSON Web Token is created (i.e. at sign
|
||||||
in) or updated (i.e whenever a session is accessed in the client). The returned value will be [encrypted](/configuration/options#jwt), and it is stored in a cookie.
|
in) or updated (i.e whenever a session is accessed in the client). The returned value will be [encrypted](/configuration/options#jwt), and it is stored in a cookie.
|
||||||
|
|
||||||
Requests to `/api/auth/signin`, `/api/auth/session` and calls to `getSession()`, `unstable_getServerSession()`, `useSession()` will invoke this function, but only if you are using a [JWT session](/configuration/options#session). This method is not invoked when you persist sessions in a database.
|
Requests to `/api/auth/signin`, `/api/auth/session` and calls to `getSession()`, `useSession()` will invoke this function, but only if you are using a [JWT session](/configuration/options#session). This method is not invoked when you persist sessions in a database.
|
||||||
|
|
||||||
- As with database persisted session expiry times, token expiry time is extended whenever a session is active.
|
- As with database persisted session expiry times, token expiry time is extended whenever a session is active.
|
||||||
- The arguments _user_, _account_, _profile_ and _isNewUser_ are only passed the first time this callback is called on a new session, after the user signs in. In subsequent calls, only `token` will be available.
|
- The arguments _user_, _account_, _profile_ and _isNewUser_ are only passed the first time this callback is called on a new session, after the user signs in. In subsequent calls, only `token` will be available.
|
||||||
|
|||||||
@@ -53,7 +53,6 @@ The message object will contain:
|
|||||||
|
|
||||||
- `user`: The user object from your adapter.
|
- `user`: The user object from your adapter.
|
||||||
- `account`: The object returned from the provider.
|
- `account`: The object returned from the provider.
|
||||||
- `profile`: The object returned from the `profile` callback of the OAuth provider.
|
|
||||||
|
|
||||||
### session
|
### session
|
||||||
|
|
||||||
|
|||||||
@@ -1,73 +1,5 @@
|
|||||||
# Next.js
|
# Next.js
|
||||||
|
|
||||||
## `unstable_getServerSession`
|
|
||||||
|
|
||||||
:::warning
|
|
||||||
This feature is experimental and may be removed or changed in the future.
|
|
||||||
:::
|
|
||||||
|
|
||||||
When calling from server-side i.e. in 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` 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, `unstable_getServerSession` will correctly update the cookie expiry time and update the session content if `callbacks.jwt` or `callbacks.session` changed something.
|
|
||||||
|
|
||||||
Otherwise, if you only want to get the session token, see [`getToken`](/tutorials/securing-pages-and-api-routes#using-gettoken).
|
|
||||||
|
|
||||||
`unstable_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:
|
|
||||||
|
|
||||||
In `[...nextauth.js]`:
|
|
||||||
```ts
|
|
||||||
import { NextAuth } from 'next-auth'
|
|
||||||
import type { NextAuthOptions } from 'next-auth'
|
|
||||||
|
|
||||||
export const authOptions: NextAuthOptions = {
|
|
||||||
// your configs
|
|
||||||
}
|
|
||||||
|
|
||||||
export default NextAuth(authOptions);
|
|
||||||
```
|
|
||||||
|
|
||||||
In `getServerSideProps`:
|
|
||||||
```js
|
|
||||||
import { authOptions } from 'pages/api/[...nextauth]'
|
|
||||||
import { unstable_getServerSession } from "next-auth/next"
|
|
||||||
|
|
||||||
export async function getServerSideProps(context) {
|
|
||||||
const session = await unstable_getServerSession(context.req, context.res, authOptions)
|
|
||||||
|
|
||||||
if (!session) {
|
|
||||||
return {
|
|
||||||
redirect: {
|
|
||||||
destination: '/',
|
|
||||||
permanent: false,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
props: {
|
|
||||||
session,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
In API routes:
|
|
||||||
```js
|
|
||||||
import { authOptions } from 'pages/api/[...nextauth]'
|
|
||||||
import { unstable_getServerSession } from "next-auth/next"
|
|
||||||
|
|
||||||
|
|
||||||
export async function handler(req, res) {
|
|
||||||
const session = await unstable_getServerSession(req, res, authOptions)
|
|
||||||
|
|
||||||
if (!session) {
|
|
||||||
res.status(401).json({ message: "You must be logged in." });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.json({
|
|
||||||
message: 'Success',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Middleware
|
## Middleware
|
||||||
|
|
||||||
You can use a Next.js Middleware with NextAuth.js to protect your site.
|
You can use a Next.js Middleware with NextAuth.js to protect your site.
|
||||||
@@ -80,34 +12,56 @@ You can get the `withAuth` middleware function from `next-auth/middleware` eithe
|
|||||||
|
|
||||||
### Prerequisites
|
### Prerequisites
|
||||||
|
|
||||||
You must set the same secret in the middleware that you use in NextAuth. The easiest way is to set the [`NEXTAUTH_SECRET`](/configuration/options#nextauth_secret) environment variable. It will be picked up by both the [NextAuth config](/configuration/options#options), as well as the middleware config.
|
You must set the [`NEXTAUTH_SECRET`](/configuration/options#nextauth_secret) environment variable when using this middleware. If you are using the [`secret` option](/configuration/options#secret) this value must match.
|
||||||
|
|
||||||
Alternatively, you can provide the secret using the [`secret`](#secret) option in the middleware config.
|
**We strongly recommend** replacing the `secret` value completely with this `NEXTAUTH_SECRET` environment variable. This environment variable will be picked up by both the [NextAuth config](/configuration/options#options), as well as the middleware config.
|
||||||
|
|
||||||
**We strongly recommend** replacing the `secret` value completely with this `NEXTAUTH_SECRET` environment variable.
|
|
||||||
|
|
||||||
### Basic usage
|
### Basic usage
|
||||||
|
|
||||||
The most simple usage is when you want to require authentication for your entire site. You can add a `middleware.js` file with the following:
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
export { default } from "next-auth/middleware"
|
import withAuth from "next-auth/middleware"
|
||||||
|
// or
|
||||||
|
import { withAuth } from "next-auth/middleware"
|
||||||
```
|
```
|
||||||
|
|
||||||
That's it! Your application is now secured. 🎉
|
### Custom JWT decode method
|
||||||
|
|
||||||
If you only want to secure certain pages, export a `config` object with a `matcher`:
|
If you have custom jwt decode method set in `[...nextauth].ts`, you must also pass the same `decode` method to `withAuth` in order to read the custom-signed JWT correctly. You may want to extract the encode/decode logic to a separate function for consistency.
|
||||||
|
|
||||||
```js
|
`[...nextauth].ts`
|
||||||
export { default } from "next-auth/middleware"
|
```ts
|
||||||
|
import jwt from "jsonwebtoken";
|
||||||
|
|
||||||
export const config = { matcher: ["/dashboard"] }
|
export default NextAuth({
|
||||||
|
providers: [...],
|
||||||
|
secret: /* Please use `process.env.NEXTAUTH_SECRET` */,
|
||||||
|
jwt: {
|
||||||
|
encode: async ({ secret, token }) => {
|
||||||
|
return jwt.sign(token as any, secret);
|
||||||
|
},
|
||||||
|
decode: async ({ secret, token }) => {
|
||||||
|
return jwt.verify(token as string, secret) as any;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
Now you will still be able to visit every page, but only `/dashboard` will require authentication.
|
Any `_middleware.ts`
|
||||||
|
```ts
|
||||||
If a user is not logged in, the default behavior is to redirect them to the sign-in page.
|
import withAuth from "next-auth/middleware"
|
||||||
|
import jwt from "jsonwebtoken";
|
||||||
|
|
||||||
|
export default withAuth({
|
||||||
|
jwt: {
|
||||||
|
decode: async ({ secret, token }) => {
|
||||||
|
return jwt.verify(token, secret) as any;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
callbacks: {
|
||||||
|
authorized: ({ token }) => !!token,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
```
|
||||||
---
|
---
|
||||||
### `callbacks`
|
### `callbacks`
|
||||||
|
|
||||||
@@ -150,40 +104,46 @@ See the documentation for the [pages option](/configuration/pages) for more info
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### `secret`
|
### Examples
|
||||||
|
|
||||||
- **Required**: _No_
|
`withAuth` is very flexible, there are multiple ways to use it.
|
||||||
|
|
||||||
#### Description
|
|
||||||
|
|
||||||
The same `secret` used in the [NextAuth config](/configuration/options#options).
|
|
||||||
|
|
||||||
#### Example (default value)
|
|
||||||
|
|
||||||
```js
|
|
||||||
secret: process.env.NEXTAUTH_SECRET
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### Advanced usage
|
|
||||||
|
|
||||||
NextAuth.js Middleware is very flexible, there are multiple ways to use it.
|
|
||||||
|
|
||||||
:::note
|
:::note
|
||||||
If you do not define the options, NextAuth.js will use the default values for the omitted options.
|
If you do not define the options, NextAuth.js will use the default values for the omitted options.
|
||||||
:::
|
:::
|
||||||
|
|
||||||
|
#### default re-export
|
||||||
|
|
||||||
|
```js title="pages/_middleware.js"
|
||||||
|
export { default } from "next-auth/middleware"
|
||||||
|
```
|
||||||
|
|
||||||
|
With this one line, when someone tries to load any of your pages, they will have to be logged-in first. Otherwise, they are redirected to the login page. It will assume that you are using the `NEXTAUTH_SECRET` environment variable.
|
||||||
|
|
||||||
|
#### default `withAuth` export
|
||||||
|
|
||||||
|
```js title="pages/admin/_middleware.js"
|
||||||
|
import { withAuth } from "next-auth/middleware"
|
||||||
|
|
||||||
|
export default withAuth({
|
||||||
|
callbacks: {
|
||||||
|
authorized: ({ token }) => token?.role === "admin",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
With the above code, you just made sure that only user's with the `admin` role can access any of the pages under the `/admin` route. (Including nested routes as well, like `/admin/settings` etc.).
|
||||||
|
|
||||||
#### wrap middleware
|
#### wrap middleware
|
||||||
|
|
||||||
```ts title="middleware.ts"
|
```ts title="pages/admin/_middleware.ts"
|
||||||
import type { NextRequest } from "next/server"
|
import type { NextRequest } from "next/server"
|
||||||
import type { JWT } from "next-auth/jwt"
|
import type { JWT } from "next-auth/jwt"
|
||||||
|
|
||||||
import { withAuth } from "next-auth/middleware"
|
import { withAuth } from "next-auth/middleware"
|
||||||
|
|
||||||
export default withAuth(
|
export default withAuth(
|
||||||
// `withAuth` can augment your Request with the user's token.
|
function middleware(req: NextRequest & { nextauth: { token: JWT } }) {
|
||||||
function middleware(req: NextRequest & { nextauth: { token: JWT | null } }) {
|
|
||||||
console.log(req.nextauth.token)
|
console.log(req.nextauth.token)
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -192,53 +152,12 @@ export default withAuth(
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
export const config = { matcher: ["/admin"] }
|
|
||||||
```
|
```
|
||||||
|
|
||||||
The `middleware` function will only be invoked if the `authorized` callback returns `true`.
|
The `middleware` function will only be invoked if the `authorized` callback returns `true`.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
#### Custom JWT decode method
|
|
||||||
|
|
||||||
If you have a custom jwt decode method set in `[...nextauth].ts`, you must also pass the same `decode` method to `withAuth` in order to read the custom-signed JWT correctly. You may want to extract the encode/decode logic to a separate function for consistency.
|
|
||||||
|
|
||||||
``
|
|
||||||
```ts title="/api/auth/[...nextauth].ts"
|
|
||||||
import type { NextAuthOptions } from "next-auth"
|
|
||||||
import NextAuth from "next-auth"
|
|
||||||
import jwt from "jsonwebtoken"
|
|
||||||
|
|
||||||
export const authOptions: NextAuthOptions = {
|
|
||||||
providers: [...],
|
|
||||||
jwt: {
|
|
||||||
async encode({ secret, token }) {
|
|
||||||
return jwt.sign(token, secret)
|
|
||||||
},
|
|
||||||
async decode({ secret, token }) {
|
|
||||||
return jwt.verify(token, secret)
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
export default NextAuth(authOptions)
|
|
||||||
```
|
|
||||||
|
|
||||||
And:
|
|
||||||
|
|
||||||
```ts title="middleware.ts"
|
|
||||||
import withAuth from "next-auth/middleware"
|
|
||||||
import { authOptions } from "pages/api/auth/[...nextauth]";
|
|
||||||
|
|
||||||
export default withAuth({
|
|
||||||
jwt: { decode: authOptions.jwt },
|
|
||||||
callbacks: {
|
|
||||||
authorized: ({ token }) => !!token,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
### Caveats
|
### Caveats
|
||||||
|
|
||||||
- Currently only supports session verification, as parts of the sign-in code need to run in a Node.js environment. In the future, we would like to make sure that NextAuth.js can fully run at the [Edge](https://nextjs.org/docs/api-reference/edge-runtime)
|
- Currently only supports session verification, as parts of the sign-in code need to run in a Node.js environment. In the future, we would like to make sure that NextAuth.js can fully run at the [Edge](https://nextjs.org/docs/api-reference/edge-runtime)
|
||||||
|
|||||||
@@ -13,22 +13,19 @@ When deploying to production, set the `NEXTAUTH_URL` environment variable to the
|
|||||||
NEXTAUTH_URL=https://example.com
|
NEXTAUTH_URL=https://example.com
|
||||||
```
|
```
|
||||||
|
|
||||||
If your Next.js application uses a custom base path, specify the route to the API endpoint in full. More informations about the usage of custom base path [here](/getting-started/client#custom-base-path).
|
If your Next.js application uses a custom base path, specify the route to the API endpoint in full.
|
||||||
|
|
||||||
_e.g. `NEXTAUTH_URL=https://example.com/custom-route/api/auth`_
|
_e.g. `NEXTAUTH_URL=https://example.com/custom-route/api/auth`_
|
||||||
|
|
||||||
:::tip
|
|
||||||
When you're using a custom base path, you will need to pass the `basePath` page prop to the `<SessionProvider>`. More informations [here](/getting-started/client#custom-base-path).
|
|
||||||
:::
|
|
||||||
|
|
||||||
:::note
|
:::note
|
||||||
Using [System Environment Variables](https://vercel.com/docs/concepts/projects/environment-variables#system-environment-variables) we automatically detect when you deploy to [Vercel](https://vercel.com) so you don't have to define this variable. Make sure **Automatically expose System Environment Variables** is checked in your Project Settings.
|
Using [System Environment Variables](https://vercel.com/docs/concepts/projects/environment-variables#system-environment-variables) we automatically detect when you deploy to [Vercel](https://vercel.com) so you don't have to define this variable. Make sure **Automatically expose System Environment Variables** is checked in your Project Settings.
|
||||||
:::
|
:::
|
||||||
|
|
||||||
### NEXTAUTH_SECRET
|
### NEXTAUTH_SECRET
|
||||||
|
|
||||||
Used to encrypt the NextAuth.js JWT, and to hash [email verification tokens](/adapters/models#verification-token). This is the default value for the `secret` option in [NextAuth](/configuration/options#secret) and [Middleware](/configuration/nextjs#secret).
|
Used to encrypt the NextAuth.js JWT, and to hash [email verification tokens](/adapters/models#verification-token). This is the default value for the [`secret`](/configuration/options#secret) option. The `secret` option might be removed in the future in favor of this.
|
||||||
|
|
||||||
|
If you are using [Middleware](/configuration/nextjs#prerequisites) this environment variable must be set.
|
||||||
|
|
||||||
### NEXTAUTH_URL_INTERNAL
|
### NEXTAUTH_URL_INTERNAL
|
||||||
|
|
||||||
@@ -369,14 +366,11 @@ Changes the color scheme theme of [pages](/configuration/pages) as well as allow
|
|||||||
|
|
||||||
In addition, you can define a logo URL in `theme.logo` which will be rendered above the main card in the default signin/signout/error/verify-request pages, as well as a `theme.brandColor` which will affect the accent color of these pages.
|
In addition, you can define a logo URL in `theme.logo` which will be rendered above the main card in the default signin/signout/error/verify-request pages, as well as a `theme.brandColor` which will affect the accent color of these pages.
|
||||||
|
|
||||||
The sign-in button's background color will match the `brandColor` and defaults to `"#346df1"`. The text color is `#fff` by default, but if your brand color gives a weak contrast, correct it with the `buttonText` color option.
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
theme: {
|
theme: {
|
||||||
colorScheme: "auto", // "auto" | "dark" | "light"
|
colorScheme: "auto", // "auto" | "dark" | "light"
|
||||||
brandColor: "", // Hex color code
|
brandColor: "", // Hex color code
|
||||||
logo: "", // Absolute URL to image
|
logo: "" // Absolute URL to image
|
||||||
buttonText: "" // Hex color code
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -350,7 +350,7 @@ providers: [
|
|||||||
|
|
||||||
## Built-in providers
|
## Built-in providers
|
||||||
|
|
||||||
NextAuth.js comes with a set of built-in providers. You can find them [here](https://github.com/nextauthjs/next-auth/tree/main/packages/next-auth/src/providers). Each built-in provider has its own documentation page:
|
NextAuth.js comes with a set of built-in providers. You can find them [here](https://github.com/nextauthjs/next-auth/tree/main/src/providers). Each built-in provider has its own documentation page:
|
||||||
|
|
||||||
<div className="provider-name-list">
|
<div className="provider-name-list">
|
||||||
{Object.entries(require("../../../providers.json"))
|
{Object.entries(require("../../../providers.json"))
|
||||||
|
|||||||
@@ -126,7 +126,7 @@ function Auth({ children }) {
|
|||||||
// if `{ required: true }` is supplied, `status` can only be "loading" or "authenticated"
|
// if `{ required: true }` is supplied, `status` can only be "loading" or "authenticated"
|
||||||
const { status } = useSession({ required: true })
|
const { status } = useSession({ required: true })
|
||||||
|
|
||||||
if (status === "loading") {
|
if (status === 'loading') {
|
||||||
return <div>Loading...</div>
|
return <div>Loading...</div>
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,19 +161,13 @@ See repository [`README`](https://github.com/nextauthjs/react-query) for more de
|
|||||||
## getSession()
|
## getSession()
|
||||||
|
|
||||||
- Client Side: **Yes**
|
- Client Side: **Yes**
|
||||||
- Server Side: **No** (See: [`unstable_getServerSession()`](/configuration/nextjs#unstable_getserversession)
|
- Server Side: **Yes**
|
||||||
|
|
||||||
NextAuth.js provides a `getSession()` helper which should be called **client side only** to return the current active session.
|
NextAuth.js provides a `getSession()` method which can be called client or server side to return a session.
|
||||||
|
|
||||||
On the server side, **this is still available to use**, however, we recommend using `unstable_getServerSession` going forward. The idea behind this is to avoid an additional unnecessary `fetch` call on the server side. For more information, please check out [this issue](https://github.com/nextauthjs/next-auth/issues/1535).
|
It calls `/api/auth/session` and returns a promise with a session object, or null if no session exists.
|
||||||
|
|
||||||
:::note
|
#### Client Side Example
|
||||||
The `unstable_getServerSession` only has the prefix `unstable_` at the moment, because the API may change in the future. There are no known bugs at the moment and it is safe to use. If you discover any issues, please do report them as a [GitHub Issue](https://github.com/nextauthjs/next-auth/issues) and we will patch them as soon as possible.
|
|
||||||
:::
|
|
||||||
|
|
||||||
This helper is helpful in case you want to read the session outside of the context of React.
|
|
||||||
|
|
||||||
When called, `getSession()` will send a request to `/api/auth/session` and returns a promise with a [session object](https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/core/types.ts#L407-L425), or `null` if no session exists.
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
async function myFunction() {
|
async function myFunction() {
|
||||||
@@ -182,7 +176,23 @@ async function myFunction() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Read the tutorial [securing pages and API routes](/tutorials/securing-pages-and-api-routes) to know how to fetch the session in server side calls using `unstable_getServerSession()`.
|
#### Server Side Example
|
||||||
|
|
||||||
|
```js
|
||||||
|
import { getSession } from "next-auth/react"
|
||||||
|
|
||||||
|
export default async (req, res) => {
|
||||||
|
const session = await getSession({ req })
|
||||||
|
/* ... */
|
||||||
|
res.end()
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
:::note
|
||||||
|
When calling `getSession()` server side, you need to pass `{req}` or `context` object.
|
||||||
|
:::
|
||||||
|
|
||||||
|
The tutorial [securing pages and API routes](/tutorials/securing-pages-and-api-routes) shows how to use `getSession()` in server side calls.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -244,7 +254,7 @@ export default async (req, res) => {
|
|||||||
```
|
```
|
||||||
|
|
||||||
:::note
|
:::note
|
||||||
Unlike and `getCsrfToken()`, when calling `getProviders()` server side, you don't need to pass anything, just as calling it client side.
|
Unlike `getSession()` and `getCsrfToken()`, when calling `getProviders()` server side, you don't need to pass anything, just as calling it client side.
|
||||||
:::
|
:::
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -426,15 +436,14 @@ If you pass the `session` page prop to the `<SessionProvider>` – as in the exa
|
|||||||
This only works on pages where you provide the correct `pageProps`, however. This is normally done in `getInitialProps` or `getServerSideProps` on an individual page basis like so:
|
This only works on pages where you provide the correct `pageProps`, however. This is normally done in `getInitialProps` or `getServerSideProps` on an individual page basis like so:
|
||||||
|
|
||||||
```js title="pages/index.js"
|
```js title="pages/index.js"
|
||||||
import { unstable_getServerSession } from "next-auth/next"
|
import { getSession } from "next-auth/react"
|
||||||
import { authOptions } from './api/auth/[...nextauth]'
|
|
||||||
|
|
||||||
...
|
...
|
||||||
|
|
||||||
export async function getServerSideProps({ req, res }) {
|
export async function getServerSideProps(ctx) {
|
||||||
return {
|
return {
|
||||||
props: {
|
props: {
|
||||||
session: await unstable_getServerSession(req, res, authOptions)
|
session: await getSession(ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -446,7 +455,7 @@ If every one of your pages needs to be protected, you can do this in `getInitial
|
|||||||
|
|
||||||
The session state is automatically synchronized across all open tabs/windows and they are all updated whenever they gain or lose focus or the state changes (e.g. a user signs in or out) when `refetchOnWindowFocus` is `true`.
|
The session state is automatically synchronized across all open tabs/windows and they are all updated whenever they gain or lose focus or the state changes (e.g. a user signs in or out) when `refetchOnWindowFocus` is `true`.
|
||||||
|
|
||||||
If you have session expiry times of 30 days (the default) or more then you probably don't need to change any of the default options in the Provider. If you need to, you can trigger an update of the session object across all tabs/windows by calling [`getSession()`](/getting-started/client#getsession) from a client side function.
|
If you have session expiry times of 30 days (the default) or more then you probably don't need to change any of the default options in the Provider. If you need to, you can trigger an update of the session object across all tabs/windows by calling `getSession()` from a client side function.
|
||||||
|
|
||||||
However, if you need to customize the session behavior and/or are using short session expiry times, you can pass options to the provider to customize the behavior of the `useSession()` hook.
|
However, if you need to customize the session behavior and/or are using short session expiry times, you can pass options to the provider to customize the behavior of the `useSession()` hook.
|
||||||
|
|
||||||
@@ -506,29 +515,3 @@ However, if it was set to `false`, it stops re-fetching the session and the comp
|
|||||||
:::note
|
:::note
|
||||||
See [**the Next.js documentation**](https://nextjs.org/docs/advanced-features/custom-app) for more information on **\_app.js** in Next.js applications.
|
See [**the Next.js documentation**](https://nextjs.org/docs/advanced-features/custom-app) for more information on **\_app.js** in Next.js applications.
|
||||||
:::
|
:::
|
||||||
|
|
||||||
### Custom base path
|
|
||||||
When your Next.js application uses a custom base path, set the `NEXTAUTH_URL` environment variable to the route to the API endpoint in full - as in the example below and as explained [here](/configuration/options#nextauth_url).
|
|
||||||
|
|
||||||
Also, make sure to pass the `basePath` page prop to the `<SessionProvider>` – as in the example below – so your custom base path is fully configured and used by NextAuth.js.
|
|
||||||
|
|
||||||
#### Example
|
|
||||||
In this example, the custom base path used is `/custom-route`.
|
|
||||||
|
|
||||||
```
|
|
||||||
NEXTAUTH_URL=https://example.com/custom-route/api/auth
|
|
||||||
```
|
|
||||||
|
|
||||||
```jsx title="pages/_app.js"
|
|
||||||
import { SessionProvider } from "next-auth/react"
|
|
||||||
export default function App({
|
|
||||||
Component,
|
|
||||||
pageProps: { session, ...pageProps },
|
|
||||||
}) {
|
|
||||||
return (
|
|
||||||
<SessionProvider session={session} basePath="/custom-route/api/auth">
|
|
||||||
<Component {...pageProps} />
|
|
||||||
</SessionProvider>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
```
|
|
||||||
@@ -93,14 +93,13 @@ You can use the `useSession` hook from anywhere in your application (e.g. in a h
|
|||||||
|
|
||||||
### Backend - API Route
|
### Backend - API Route
|
||||||
|
|
||||||
To protect an API Route, you can use the [`unstable_getServerSession()`](/configuration/nextjs#unstable_getserversession) method.
|
To protect an API Route, you can use the [`getSession()`](/getting-started/client#getsession) method in the NextAuth.js client.
|
||||||
|
|
||||||
```javascript title="pages/api/restricted.js" showLineNumbers
|
```javascript title="pages/api/restricted.js" showLineNumbers
|
||||||
import { unstable_getServerSession } from "next-auth/next"
|
import { getSession } from "next-auth/react"
|
||||||
import { authOptions } from "./api/auth/[...nextauth]"
|
|
||||||
|
|
||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
const session = await unstable_getServerSession(req, res, authOptions)
|
const session = await getSession({ req })
|
||||||
|
|
||||||
if (session) {
|
if (session) {
|
||||||
res.send({
|
res.send({
|
||||||
@@ -144,7 +143,7 @@ callbacks: {
|
|||||||
...
|
...
|
||||||
```
|
```
|
||||||
|
|
||||||
Now whenever you call [`getSession`](/getting-started/client#getsession) or [`useSession`](/getting-started/client#usesession), the data object which is returned will include the `accessToken` value.
|
Now whenever you call `getSession` or `useSession`, the data object which is returned will include the `accessToken` value.
|
||||||
|
|
||||||
```jsx title="components/accessToken.jsx" showLineNumbers
|
```jsx title="components/accessToken.jsx" showLineNumbers
|
||||||
import { useSession, signIn, signOut } from "next-auth/react"
|
import { useSession, signIn, signOut } from "next-auth/react"
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ We encourage users to try it out and report any and all issues they come across.
|
|||||||
|
|
||||||
You can upgrade to the new version by running:
|
You can upgrade to the new version by running:
|
||||||
|
|
||||||
```bash npm2yarn2pnpm
|
```bash npm2yarn
|
||||||
npm install next-auth
|
npm install next-auth
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -124,74 +124,67 @@ providers: [
|
|||||||
The following code shows the complete source for the built-in `sendVerificationRequest()` method:
|
The following code shows the complete source for the built-in `sendVerificationRequest()` method:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import { createTransport } from "nodemailer"
|
import nodemailer from "nodemailer"
|
||||||
|
|
||||||
async function sendVerificationRequest(params) {
|
async function sendVerificationRequest({
|
||||||
const { identifier, url, provider, theme } = params
|
identifier: email,
|
||||||
|
url,
|
||||||
|
provider: { server, from },
|
||||||
|
}) {
|
||||||
const { host } = new URL(url)
|
const { host } = new URL(url)
|
||||||
// NOTE: You are not required to use `nodemailer`, use whatever you want.
|
const transport = nodemailer.createTransport(server)
|
||||||
const transport = createTransport(provider.server)
|
await transport.sendMail({
|
||||||
const result = await transport.sendMail({
|
to: email,
|
||||||
to: identifier,
|
from,
|
||||||
from: provider.from,
|
|
||||||
subject: `Sign in to ${host}`,
|
subject: `Sign in to ${host}`,
|
||||||
text: text({ url, host }),
|
text: text({ url, host }),
|
||||||
html: html({ url, host, theme }),
|
html: html({ url, host, email }),
|
||||||
})
|
})
|
||||||
const failed = result.rejected.concat(result.pending).filter(Boolean)
|
|
||||||
if (failed.length) {
|
|
||||||
throw new Error(`Email(s) (${failed.join(", ")}) could not be sent`)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// Email HTML body
|
||||||
* Email HTML body
|
function html({ url, host, email }: Record<"url" | "host" | "email", string>) {
|
||||||
* Insert invisible space into domains from being turned into a hyperlink by email
|
// Insert invisible space into domains and email address to prevent both the
|
||||||
* clients like Outlook and Apple mail, as this is confusing because it seems
|
// email address and the domain from being turned into a hyperlink by email
|
||||||
* like they are supposed to click on it to sign in.
|
// clients like Outlook and Apple mail, as this is confusing because it seems
|
||||||
*
|
// like they are supposed to click on their email address to sign in.
|
||||||
* @note We don't add the email address to avoid needing to escape it, if you do, remember to sanitize it!
|
const escapedEmail = `${email.replace(/\./g, "​.")}`
|
||||||
*/
|
const escapedHost = `${host.replace(/\./g, "​.")}`
|
||||||
function html(params: { url: string; host: string; theme: Theme }) {
|
|
||||||
const { url, host, theme } = params
|
|
||||||
|
|
||||||
const escapedHost = host.replace(/\./g, "​.")
|
// Some simple styling options
|
||||||
|
const backgroundColor = "#f9f9f9"
|
||||||
const brandColor = theme.brandColor || "#346df1"
|
const textColor = "#444444"
|
||||||
const color = {
|
const mainBackgroundColor = "#ffffff"
|
||||||
background: "#f9f9f9",
|
const buttonBackgroundColor = "#346df1"
|
||||||
text: "#444",
|
const buttonBorderColor = "#346df1"
|
||||||
mainBackground: "#fff",
|
const buttonTextColor = "#ffffff"
|
||||||
buttonBackground: brandColor,
|
|
||||||
buttonBorder: brandColor,
|
|
||||||
buttonText: theme.buttonText || "#fff",
|
|
||||||
}
|
|
||||||
|
|
||||||
return `
|
return `
|
||||||
<body style="background: ${color.background};">
|
<body style="background: ${backgroundColor};">
|
||||||
<table width="100%" border="0" cellspacing="20" cellpadding="0"
|
<table width="100%" border="0" cellspacing="0" cellpadding="0">
|
||||||
style="background: ${color.mainBackground}; max-width: 600px; margin: auto; border-radius: 10px;">
|
|
||||||
<tr>
|
<tr>
|
||||||
<td align="center"
|
<td align="center" style="padding: 10px 0px 20px 0px; font-size: 22px; font-family: Helvetica, Arial, sans-serif; color: ${textColor};">
|
||||||
style="padding: 10px 0px; font-size: 22px; font-family: Helvetica, Arial, sans-serif; color: ${color.text};">
|
<strong>${escapedHost}</strong>
|
||||||
Sign in to <strong>${escapedHost}</strong>
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
<table width="100%" border="0" cellspacing="20" cellpadding="0" style="background: ${mainBackgroundColor}; max-width: 600px; margin: auto; border-radius: 10px;">
|
||||||
|
<tr>
|
||||||
|
<td align="center" style="padding: 10px 0px 0px 0px; font-size: 18px; font-family: Helvetica, Arial, sans-serif; color: ${textColor};">
|
||||||
|
Sign in as <strong>${escapedEmail}</strong>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td align="center" style="padding: 20px 0;">
|
<td align="center" style="padding: 20px 0;">
|
||||||
<table border="0" cellspacing="0" cellpadding="0">
|
<table border="0" cellspacing="0" cellpadding="0">
|
||||||
<tr>
|
<tr>
|
||||||
<td align="center" style="border-radius: 5px;" bgcolor="${color.buttonBackground}"><a href="${url}"
|
<td align="center" style="border-radius: 5px;" bgcolor="${buttonBackgroundColor}"><a href="${url}" target="_blank" style="font-size: 18px; font-family: Helvetica, Arial, sans-serif; color: ${buttonTextColor}; text-decoration: none; border-radius: 5px; padding: 10px 20px; border: 1px solid ${buttonBorderColor}; display: inline-block; font-weight: bold;">Sign in</a></td>
|
||||||
target="_blank"
|
|
||||||
style="font-size: 18px; font-family: Helvetica, Arial, sans-serif; color: ${color.buttonText}; text-decoration: none; border-radius: 5px; padding: 10px 20px; border: 1px solid ${color.buttonBorder}; display: inline-block; font-weight: bold;">Sign
|
|
||||||
in</a></td>
|
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td align="center"
|
<td align="center" style="padding: 0px 0px 10px 0px; font-size: 16px; line-height: 22px; font-family: Helvetica, Arial, sans-serif; color: ${textColor};">
|
||||||
style="padding: 0px 0px 10px 0px; font-size: 16px; line-height: 22px; font-family: Helvetica, Arial, sans-serif; color: ${color.text};">
|
|
||||||
If you did not request this email you can safely ignore it.
|
If you did not request this email you can safely ignore it.
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
@@ -200,8 +193,8 @@ function html(params: { url: string; host: string; theme: Theme }) {
|
|||||||
`
|
`
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Email Text body (fallback for email clients that don't render HTML, e.g. feature phones) */
|
// Email Text body (fallback for email clients that don't render HTML, e.g. feature phones)
|
||||||
function text({ url, host }: { url: string; host: string }) {
|
function text({ url, host }: Record<"url" | "host", string>) {
|
||||||
return `Sign in to ${host}\n${url}\n\n`
|
return `Sign in to ${host}\n${url}\n\n`
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ https://github.com/settings/apps
|
|||||||
|
|
||||||
The **GitHub Provider** comes with a set of default options:
|
The **GitHub Provider** comes with a set of default options:
|
||||||
|
|
||||||
- [GitHub Provider options](https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/providers/github.ts)
|
- [GitHub Provider options](https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/providers/github.js)
|
||||||
|
|
||||||
You can override any of the options to suit your own use case.
|
You can override any of the options to suit your own use case.
|
||||||
|
|
||||||
@@ -30,8 +30,8 @@ import GitHubProvider from "next-auth/providers/github";
|
|||||||
...
|
...
|
||||||
providers: [
|
providers: [
|
||||||
GitHubProvider({
|
GitHubProvider({
|
||||||
clientId: process.env.GITHUB_ID,
|
clientId: process.env.GITHUB_CLIENT_ID,
|
||||||
clientSecret: process.env.GITHUB_SECRET
|
clientSecret: process.env.GITHUB_CLIENT_SECRET
|
||||||
})
|
})
|
||||||
]
|
]
|
||||||
...
|
...
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ title: Overview
|
|||||||
|
|
||||||
Authentication Providers in **NextAuth.js** are services that can be used to sign in a user.
|
Authentication Providers in **NextAuth.js** are services that can be used to sign in a user.
|
||||||
|
|
||||||
There are four ways a user can be signed in:
|
There's four ways a user can be signed in:
|
||||||
|
|
||||||
- [Using a built-in OAuth Provider](/configuration/providers/oauth) (e.g Github, Twitter, Google, etc...)
|
- [Using a built-in OAuth Provider](/configuration/providers/oauth) (e.g Github, Twitter, Google, etc...)
|
||||||
- [Using a custom OAuth Provider](/configuration/providers/oauth#using-a-custom-provider)
|
- [Using a custom OAuth Provider](/configuration/providers/oauth#using-a-custom-provider)
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ https://api.slack.com/docs/sign-in-with-slack
|
|||||||
https://api.slack.com/apps
|
https://api.slack.com/apps
|
||||||
|
|
||||||
:::warning
|
:::warning
|
||||||
Slack requires that the redirect URL of your app uses `https`, even for local development. An easy workaround for this is using a service like [`ngrok`](https://ngrok.com) that creates a secure tunnel to your app, using `https`. Remember to set the url as `NEXTAUTH_URL` as well.
|
Slack requires you that the redirect URL of your app uses `https`, even for local development. An easy workaround for this is using a service like [`ngrok`](https://ngrok.com) that creates a secure tunnel to your app, using `https`. Remember to set the url as `NEXTAUTH_URL` as well.
|
||||||
:::
|
:::
|
||||||
|
|
||||||

|

|
||||||
|
|||||||
@@ -1,50 +0,0 @@
|
|||||||
---
|
|
||||||
id: wikimedia
|
|
||||||
title: Wikimedia
|
|
||||||
---
|
|
||||||
|
|
||||||
## Documentation
|
|
||||||
|
|
||||||
https://www.mediawiki.org/wiki/Extension:OAuth
|
|
||||||
|
|
||||||
This provider also supports all Wikimedia projects:
|
|
||||||
|
|
||||||
- Wikipedia
|
|
||||||
- Wikidata
|
|
||||||
- Wikibooks
|
|
||||||
- Wiktionary
|
|
||||||
- etc..
|
|
||||||
|
|
||||||
Please be aware that Wikimedia accounts do not have to have an associated email address. So you may want to add check if the user has an email address before allowing them to login.
|
|
||||||
|
|
||||||
## Configuration
|
|
||||||
|
|
||||||
1. Go to and accept the Consumer Registration doc: https://meta.wikimedia.org/wiki/Special:OAuthConsumerRegistration
|
|
||||||
2. Request a new OAuth 2.0 consumer to get the `clientId` and `clientSecret`: https://meta.wikimedia.org/wiki/Special:OAuthConsumerRegistration/propose/oauth2
|
|
||||||
2a. Add the following redirect URL into the console `http://<your-next-app-url>/api/auth/callback/wikimedia`
|
|
||||||
2b. Do not check the box next to `This consumer is only for [your username]`
|
|
||||||
2c. Unless you explicitly need a larger scope, feel free to select the radio button labelled `User identity verification only - no ability to read pages or act on the users behalf.`
|
|
||||||
|
|
||||||
After registration, you can initally test your application only with your own Wikimedia account. You may have to wait several days for the application to be approved for it to be used by everyone.
|
|
||||||
|
|
||||||
## Options
|
|
||||||
|
|
||||||
The **Wikimedia Provider** comes with a set of default options:
|
|
||||||
|
|
||||||
- [Wikimedia Provider options](https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/providers/wikimedia.ts)
|
|
||||||
|
|
||||||
You can override any of the options to suit your own use case.
|
|
||||||
|
|
||||||
## Example
|
|
||||||
|
|
||||||
```js
|
|
||||||
import WikimediaProvider from "next-auth/providers/wikimedia";
|
|
||||||
...
|
|
||||||
providers: [
|
|
||||||
WikimediaProvider({
|
|
||||||
clientId: process.env.WIKIMEDIA_CLIENT_ID,
|
|
||||||
clientSecret: process.env.WIKIMEDIA_CLIENT_SECRET
|
|
||||||
})
|
|
||||||
]
|
|
||||||
...
|
|
||||||
```
|
|
||||||
@@ -15,7 +15,7 @@ https://dashboard.workos.com
|
|||||||
|
|
||||||
The **WorkOS Provider** comes with a set of default options:
|
The **WorkOS Provider** comes with a set of default options:
|
||||||
|
|
||||||
- [WorkOS Provider options](https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/providers/workos.ts)
|
- [WorkOS Provider options](https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/providers/workos.js)
|
||||||
|
|
||||||
You can override any of the options to suit your own use case.
|
You can override any of the options to suit your own use case.
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ NextAuth.js provides the ability to setup a [custom Credential provider](/config
|
|||||||
|
|
||||||
You will need an additional dependency, `ldapjs`, which you can install by running
|
You will need an additional dependency, `ldapjs`, which you can install by running
|
||||||
|
|
||||||
```bash npm2yarn2pnpm
|
```bash npm2yarn
|
||||||
npm install ldapjs
|
npm install ldapjs
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -40,29 +40,12 @@ export default function Page() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Next.js (Middleware)
|
|
||||||
|
|
||||||
With NextAuth.js 4.2.0 and Next.js 12, you can now protect your pages via the middleware pattern more easily. If you would like to protect all pages, you can create a `_middleware.js` file in your root `pages` directory which looks like this.
|
|
||||||
|
|
||||||
```js title="/middleware.js"
|
|
||||||
export { default } from "next-auth/middleware"
|
|
||||||
```
|
|
||||||
|
|
||||||
Otherwise, if you only want to protect a subset of pages, you could put it in a subdirectory as well, for example in `/pages/admin/_middleware.js` would protect all pages under `/admin`.
|
|
||||||
|
|
||||||
For the time being, the `withAuth` middleware only supports `"jwt"` as [session strategy](https://next-auth.js.org/configuration/options#session).
|
|
||||||
|
|
||||||
More details can be found [here](https://next-auth.js.org/configuration/nextjs#middleware).
|
|
||||||
|
|
||||||
### Server Side
|
### Server Side
|
||||||
|
|
||||||
You can protect server side rendered pages using the `unstable_getServerSession` method. This is different from the old `getSession()` method, in that it does not do an extra fetch out over the internet to confirm data from itself, increasing performance significantly.
|
You can protect server side rendered pages using the `getSession()` method.
|
||||||
|
|
||||||
You need to add this to every server rendered page you want to protect. Be aware, `unstable_getServerSession` takes slightly different arguments than the method it is replacing, `getSession`.
|
|
||||||
|
|
||||||
```js title="pages/server-side-example.js"
|
```js title="pages/server-side-example.js"
|
||||||
import { useSession, unstable_getServerSession } from "next-auth/next"
|
import { useSession, getSession } from "next-auth/react"
|
||||||
import { authOptions } from "./api/auth/[...nextauth]"
|
|
||||||
|
|
||||||
export default function Page() {
|
export default function Page() {
|
||||||
const { data: session } = useSession()
|
const { data: session } = useSession()
|
||||||
@@ -83,11 +66,7 @@ export default function Page() {
|
|||||||
export async function getServerSideProps(context) {
|
export async function getServerSideProps(context) {
|
||||||
return {
|
return {
|
||||||
props: {
|
props: {
|
||||||
session: await unstable_getServerSession(
|
session: await getSession(context),
|
||||||
context.req,
|
|
||||||
context.res,
|
|
||||||
authOptions
|
|
||||||
),
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -115,16 +94,15 @@ export default function App({
|
|||||||
|
|
||||||
## Securing API Routes
|
## Securing API Routes
|
||||||
|
|
||||||
### Using unstable_getServerSession()
|
### Using getSession()
|
||||||
|
|
||||||
You can protect API routes using the `unstable_getServerSession()` method.
|
You can protect API routes using the `getSession()` method.
|
||||||
|
|
||||||
```js title="pages/api/get-session-example.js"
|
```js title="pages/api/get-session-example.js"
|
||||||
import { unstable_getServerSession } from "next-auth/next"
|
import { getSession } from "next-auth/react"
|
||||||
import { authOptions } from "./api/auth/[...nextauth]"
|
|
||||||
|
|
||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
const session = await unstable_getServerSession(req, res, authOptions)
|
const session = await getSession({ req })
|
||||||
if (session) {
|
if (session) {
|
||||||
// Signed in
|
// Signed in
|
||||||
console.log("Session", JSON.stringify(session, null, 2))
|
console.log("Session", JSON.stringify(session, null, 2))
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ To test an implementation of NextAuth.js, we encourage you to use [Cypress](http
|
|||||||
|
|
||||||
To get started, install the dependencies:
|
To get started, install the dependencies:
|
||||||
|
|
||||||
```bash npm2yarn2pnpm
|
```bash npm2yarn
|
||||||
npm install --save-dev cypress cypress-social-logins @testing-library/cypress
|
npm install --save-dev cypress cypress-social-logins @testing-library/cypress
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -33,10 +33,6 @@ In development, we generate a `secret` based on your configuration for convenien
|
|||||||
|
|
||||||
Twitter OAuth 2.0 is currently in beta as certain changes might still be necessary. This is not covered by semver. See the docs https://next-auth.js.org/providers/twitter#oauth-2
|
Twitter OAuth 2.0 is currently in beta as certain changes might still be necessary. This is not covered by semver. See the docs https://next-auth.js.org/providers/twitter#oauth-2
|
||||||
|
|
||||||
#### EXPERIMENTAL_API
|
|
||||||
|
|
||||||
Some APIs are still experimental; they may be changed or removed in the future. Use at your own risk.
|
|
||||||
|
|
||||||
## Adapter
|
## Adapter
|
||||||
|
|
||||||
### ADAPTER_TYPEORM_UPDATING_ENTITIES
|
### ADAPTER_TYPEORM_UPDATING_ENTITIES
|
||||||
|
|||||||
@@ -155,9 +155,9 @@ module.exports = {
|
|||||||
showLastUpdateAuthor: true,
|
showLastUpdateAuthor: true,
|
||||||
showLastUpdateTime: true,
|
showLastUpdateTime: true,
|
||||||
remarkPlugins: [
|
remarkPlugins: [
|
||||||
require("@sapphire/docusaurus-plugin-npm2yarn2pnpm").npm2yarn2pnpm,
|
|
||||||
require("remark-github"),
|
require("remark-github"),
|
||||||
require("mdx-mermaid"),
|
require("mdx-mermaid"),
|
||||||
|
[require("@docusaurus/remark-plugin-npm2yarn"), { sync: true }],
|
||||||
],
|
],
|
||||||
versions: {
|
versions: {
|
||||||
current: {
|
current: {
|
||||||
|
|||||||
@@ -19,11 +19,11 @@
|
|||||||
"generate-providers": "node ./scripts/generate-providers.js"
|
"generate-providers": "node ./scripts/generate-providers.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@docusaurus/core": "^2.0.0-beta.21",
|
"@docusaurus/core": "^2.0.0-beta.20",
|
||||||
"@docusaurus/preset-classic": "^2.0.0-beta.21",
|
"@docusaurus/preset-classic": "^2.0.0-beta.20",
|
||||||
"@docusaurus/theme-common": "2.0.0-beta.21",
|
"@docusaurus/remark-plugin-npm2yarn": "^2.0.0-beta.20",
|
||||||
|
"@docusaurus/theme-common": "2.0.0-beta.20",
|
||||||
"@mdx-js/react": "1.6.22",
|
"@mdx-js/react": "1.6.22",
|
||||||
"@sapphire/docusaurus-plugin-npm2yarn2pnpm": "1.1.3",
|
|
||||||
"classnames": "^2.3.1",
|
"classnames": "^2.3.1",
|
||||||
"mdx-mermaid": "^1.2.2",
|
"mdx-mermaid": "^1.2.2",
|
||||||
"mermaid": "^9.0.1",
|
"mermaid": "^9.0.1",
|
||||||
|
|||||||
@@ -1,23 +1,4 @@
|
|||||||
{
|
{
|
||||||
"headers": [
|
|
||||||
{
|
|
||||||
"source": "/(.*)",
|
|
||||||
"headers": [
|
|
||||||
{
|
|
||||||
"key": "X-Content-Type-Options",
|
|
||||||
"value": "nosniff"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"key": "X-Frame-Options",
|
|
||||||
"value": "DENY"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"key": "X-XSS-Protection",
|
|
||||||
"value": "1; mode=block"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"redirects": [
|
"redirects": [
|
||||||
{
|
{
|
||||||
"source": "/schemas/models",
|
"source": "/schemas/models",
|
||||||
|
|||||||
@@ -181,7 +181,7 @@ If you think your custom provider might be useful to others, we encourage you to
|
|||||||
|
|
||||||
You only need to add two changes:
|
You only need to add two changes:
|
||||||
|
|
||||||
1. Add your config: [`src/providers/{provider}.js`](https://github.com/nextauthjs/next-auth/tree/main/packages/next-auth/src/providers)<br />
|
1. Add your config: [`src/providers/{provider}.js`](https://github.com/nextauthjs/next-auth/tree/main/src/providers)<br />
|
||||||
• make sure you use a named default export, like this: `export default function YourProvider`
|
• make sure you use a named default export, like this: `export default function YourProvider`
|
||||||
2. Add provider documentation: [`www/docs/providers/{provider}.md`](https://github.com/nextauthjs/next-auth/tree/ead715219a5d7a6e882a6ba27fa56b03954d062d/www/docs/providers)
|
2. Add provider documentation: [`www/docs/providers/{provider}.md`](https://github.com/nextauthjs/next-auth/tree/ead715219a5d7a6e882a6ba27fa56b03954d062d/www/docs/providers)
|
||||||
3. Add it to our [provider types](https://github.com/nextauthjs/next-auth/blob/ead715219a5d7a6e882a6ba27fa56b03954d062d/types/providers.d.ts) (for TS projects)<br />
|
3. Add it to our [provider types](https://github.com/nextauthjs/next-auth/blob/ead715219a5d7a6e882a6ba27fa56b03954d062d/types/providers.d.ts) (for TS projects)<br />
|
||||||
|
|||||||
@@ -1,53 +0,0 @@
|
|||||||
---
|
|
||||||
id: duende-identityserver6
|
|
||||||
title: DuendeIdentityServer6
|
|
||||||
---
|
|
||||||
|
|
||||||
## Documentation
|
|
||||||
|
|
||||||
https://docs.duendesoftware.com/identityserver/v6
|
|
||||||
|
|
||||||
## Options
|
|
||||||
|
|
||||||
The **DuendeIdentityServer6 Provider** comes with a set of default options:
|
|
||||||
|
|
||||||
- [DuendeIdentityServer6 Provider options](https://github.com/nextauthjs/next-auth/tree/main/packages/next-auth/src/providers/duende-identity-server6.ts)
|
|
||||||
|
|
||||||
You can override any of the options to suit your own use case.
|
|
||||||
|
|
||||||
## Example
|
|
||||||
|
|
||||||
```js
|
|
||||||
import DuendeIDS6Provider from "next-auth/providers/duende-identity-server6"
|
|
||||||
|
|
||||||
...
|
|
||||||
providers: [
|
|
||||||
DuendeIDS6Provider({
|
|
||||||
clientId: process.env.DUENDE_IDS6_ID,
|
|
||||||
clientSecret: process.env.DUENDE_IDS6_SECRET,
|
|
||||||
issuer: process.env.DUENDE_IDS6_ISSUER,
|
|
||||||
})
|
|
||||||
]
|
|
||||||
...
|
|
||||||
```
|
|
||||||
|
|
||||||
## Demo IdentityServer
|
|
||||||
|
|
||||||
The configuration below is for the demo server at https://demo.duendesoftware.com/
|
|
||||||
|
|
||||||
If you want to try it out, you can copy and paste the configuration below.
|
|
||||||
|
|
||||||
You can sign in to the demo service with either <b>bob/bob</b> or <b>alice/alice</b>.
|
|
||||||
|
|
||||||
```js
|
|
||||||
import DuendeIDS6Provider from "next-auth/providers/duende-identity-server6"
|
|
||||||
...
|
|
||||||
providers: [
|
|
||||||
DuendeIDS6Provider({
|
|
||||||
clientId: "interactive.confidential",
|
|
||||||
clientSecret: "secret",
|
|
||||||
issuer: "https://demo.duendesoftware.com",
|
|
||||||
})
|
|
||||||
]
|
|
||||||
...
|
|
||||||
```
|
|
||||||
@@ -15,7 +15,7 @@ https://github.com/settings/apps
|
|||||||
|
|
||||||
The **Github Provider** comes with a set of default options:
|
The **Github Provider** comes with a set of default options:
|
||||||
|
|
||||||
- [Github Provider options](https://github.com/nextauthjs/next-auth/blob/main/src/providers/github.ts)
|
- [Github Provider options](https://github.com/nextauthjs/next-auth/blob/main/src/providers/github.js)
|
||||||
|
|
||||||
You can override any of the options to suit your own use case.
|
You can override any of the options to suit your own use case.
|
||||||
|
|
||||||
|
|||||||
@@ -54,6 +54,6 @@ providers: [
|
|||||||
clientSecret: "secret",
|
clientSecret: "secret",
|
||||||
protection: "pkce"
|
protection: "pkce"
|
||||||
})
|
})
|
||||||
]
|
}
|
||||||
...
|
...
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -36,9 +36,8 @@
|
|||||||
"pretty-quick": "^3.1.2",
|
"pretty-quick": "^3.1.2",
|
||||||
"semver": "7.3.5",
|
"semver": "7.3.5",
|
||||||
"stream-to-array": "2.3.0",
|
"stream-to-array": "2.3.0",
|
||||||
"ts-node": "10.8.2",
|
"ts-node": "10.5.0",
|
||||||
"turbo": "1.3.1",
|
"turbo": "^1.2.5",
|
||||||
"type-fest": "2.16.0",
|
|
||||||
"typescript": "^4.5.2"
|
"typescript": "^4.5.2"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
@@ -98,7 +97,7 @@
|
|||||||
"**/tests",
|
"**/tests",
|
||||||
"**/__tests__"
|
"**/__tests__"
|
||||||
],
|
],
|
||||||
"packageManager": "pnpm@7.5.1",
|
"packageManager": "pnpm@6.32.8",
|
||||||
"funding": [
|
"funding": [
|
||||||
{
|
{
|
||||||
"type": "github",
|
"type": "github",
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
Open Source. Full Stack. Own Your Data.
|
Open Source. Full Stack. Own Your Data.
|
||||||
</p>
|
</p>
|
||||||
<!-- <p align="center" style="align: center;">
|
<!-- <p align="center" style="align: center;">
|
||||||
<img src="https://github.com/nextauthjs/next-auth/actions/workflows/release.yml/badge.svg?branch=main" alt="CI Test" />
|
<img src="https://github.com/nextauthjs/adapters/actions/workflows/release.yml/badge.svg" alt="CI Test" />
|
||||||
<img src="https://img.shields.io/bundlephobia/minzip/@next-auth/prisma-adapter" alt="Bundle Size"/>
|
<img src="https://img.shields.io/bundlephobia/minzip/@next-auth/prisma-adapter" alt="Bundle Size"/>
|
||||||
<img src="https://img.shields.io/npm/v/@next-auth/prisma-adapter" alt="@next-auth/prisma-adapter Version" />
|
<img src="https://img.shields.io/npm/v/@next-auth/prisma-adapter" alt="@next-auth/prisma-adapter Version" />
|
||||||
</p> -->
|
</p> -->
|
||||||
@@ -150,7 +150,7 @@ type User
|
|||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
We're open to all community contributions! If you'd like to contribute in any way, please read our [Contributing Guide](https://github.com/nextauthjs/next-auth/blob/main/CONTRIBUTING.md).
|
We're open to all community contributions! If you'd like to contribute in any way, please read our [Contributing Guide](https://github.com/nextauthjs/adapters/blob/main/CONTRIBUTING.md).
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
@@ -3,9 +3,9 @@
|
|||||||
"version": "1.0.3",
|
"version": "1.0.3",
|
||||||
"description": "Dgraph adapter for next-auth.",
|
"description": "Dgraph adapter for next-auth.",
|
||||||
"homepage": "https://next-auth.js.org",
|
"homepage": "https://next-auth.js.org",
|
||||||
"repository": "https://github.com/nextauthjs/next-auth",
|
"repository": "https://github.com/nextauthjs/adapters",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/nextauthjs/next-auth/issues"
|
"url": "https://github.com/nextauthjs/adapters/issues"
|
||||||
},
|
},
|
||||||
"author": "Arnaud Derbey <arnaud@derbey.dev>",
|
"author": "Arnaud Derbey <arnaud@derbey.dev>",
|
||||||
"contributors": [],
|
"contributors": [],
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
Open Source. Full Stack. Own Your Data.
|
Open Source. Full Stack. Own Your Data.
|
||||||
</p>
|
</p>
|
||||||
<p align="center" style="align: center;">
|
<p align="center" style="align: center;">
|
||||||
<img src="https://github.com/nextauthjs/next-auth/actions/workflows/release.yml/badge.svg?branch=main" alt="Build Test" />
|
<img src="https://github.com/nextauthjs/adapters/actions/workflows/release.yml/badge.svg" alt="Build Test" />
|
||||||
<img src="https://img.shields.io/bundlephobia/minzip/@next-auth/dynamodb-adapter/latest" alt="Bundle Size"/>
|
<img src="https://img.shields.io/bundlephobia/minzip/@next-auth/dynamodb-adapter/latest" alt="Bundle Size"/>
|
||||||
<img src="https://img.shields.io/npm/v/@next-auth/dynamodb-adapter" alt="@next-auth/dynamodb-adapter Version" />
|
<img src="https://img.shields.io/npm/v/@next-auth/dynamodb-adapter" alt="@next-auth/dynamodb-adapter Version" />
|
||||||
</p>
|
</p>
|
||||||
@@ -96,7 +96,7 @@ Here is a schema of the table :
|
|||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
We're open to all community contributions! If you'd like to contribute in any way, please read our [Contributing Guide](https://github.com/nextauthjs/next-auth/blob/main/CONTRIBUTING.md).
|
We're open to all community contributions! If you'd like to contribute in any way, please read our [Contributing Guide](https://github.com/nextauthjs/adapters/blob/main/CONTRIBUTING.md).
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@next-auth/dynamodb-adapter",
|
"name": "@next-auth/dynamodb-adapter",
|
||||||
"repository": "https://github.com/nextauthjs/next-auth",
|
"repository": "https://github.com/nextauthjs/adapters",
|
||||||
"version": "1.0.3",
|
"version": "1.0.3",
|
||||||
"description": "AWS DynamoDB adapter for next-auth.",
|
"description": "AWS DynamoDB adapter for next-auth.",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
Open Source. Full Stack. Own Your Data.
|
Open Source. Full Stack. Own Your Data.
|
||||||
</p>
|
</p>
|
||||||
<p align="center" style="align: center;">
|
<p align="center" style="align: center;">
|
||||||
<img src="https://github.com/nextauthjs/next-auth/actions/workflows/release.yml/badge.svg?branch=main" alt="Build Test" />
|
<img src="https://github.com/nextauthjs/adapters/actions/workflows/release.yml/badge.svg" alt="Build Test" />
|
||||||
<a href="https://www.npmjs.com/package/@next-auth/faunadb-adapter" target="_blank"><img src="https://img.shields.io/bundlephobia/minzip/@next-auth/fauna-adapter/next" alt="Bundle Size"/></a>
|
<a href="https://www.npmjs.com/package/@next-auth/faunadb-adapter" target="_blank"><img src="https://img.shields.io/bundlephobia/minzip/@next-auth/fauna-adapter/next" alt="Bundle Size"/></a>
|
||||||
<a href="https://www.npmjs.com/package/@next-auth/faunadb-adapter" target="_blank"><img src="https://img.shields.io/npm/v/@next-auth/fauna-adapter/next" alt="@next-auth/fauna-adapter Version" /></a>
|
<a href="https://www.npmjs.com/package/@next-auth/faunadb-adapter" target="_blank"><img src="https://img.shields.io/npm/v/@next-auth/fauna-adapter/next" alt="@next-auth/fauna-adapter Version" /></a>
|
||||||
</p>
|
</p>
|
||||||
@@ -53,7 +53,7 @@ export default NextAuth({
|
|||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
We're open to all community contributions! If you'd like to contribute in any way, please read our [Contributing Guide](https://github.com/nextauthjs/next-auth/blob/main/CONTRIBUTING.md).
|
We're open to all community contributions! If you'd like to contribute in any way, please read our [Contributing Guide](https://github.com/nextauthjs/adapters/blob/main/CONTRIBUTING.md).
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"version": "1.0.3",
|
"version": "1.0.3",
|
||||||
"description": "Fauna Adapter for NextAuth",
|
"description": "Fauna Adapter for NextAuth",
|
||||||
"homepage": "https://next-auth.js.org",
|
"homepage": "https://next-auth.js.org",
|
||||||
"repository": "https://github.com/nextauthjs/next-auth",
|
"repository": "https://github.com/nextauthjs/adapters",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/nextauthjs/next-auth/issues"
|
"url": "https://github.com/nextauthjs/next-auth/issues"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
Open Source. Full Stack. Own Your Data.
|
Open Source. Full Stack. Own Your Data.
|
||||||
</p>
|
</p>
|
||||||
<p align="center" style="align: center;">
|
<p align="center" style="align: center;">
|
||||||
<img src="https://github.com/nextauthjs/next-auth/actions/workflows/release.yml/badge.svg?branch=main" alt="Build Test" />
|
<img src="https://github.com/nextauthjs/adapters/actions/workflows/release.yml/badge.svg" alt="Build Test" />
|
||||||
<img src="https://img.shields.io/bundlephobia/minzip/@next-auth/firebase-adapter/latest" alt="Bundle Size"/>
|
<img src="https://img.shields.io/bundlephobia/minzip/@next-auth/firebase-adapter/latest" alt="Bundle Size"/>
|
||||||
<img src="https://img.shields.io/npm/v/@next-auth/firebase-adapter" alt="@next-auth/firebase-adapter Version" />
|
<img src="https://img.shields.io/npm/v/@next-auth/firebase-adapter" alt="@next-auth/firebase-adapter Version" />
|
||||||
</p>
|
</p>
|
||||||
@@ -32,13 +32,14 @@ npm install next-auth @next-auth/firebase-adapter
|
|||||||
```js
|
```js
|
||||||
import NextAuth from "next-auth"
|
import NextAuth from "next-auth"
|
||||||
import Providers from "next-auth/providers"
|
import Providers from "next-auth/providers"
|
||||||
import { FirestoreAdapter } from "@next-auth/firebase-adapter"
|
import { FirebaseAdapter } from "@next-auth/firebase-adapter"
|
||||||
|
|
||||||
import { initializeApp } from "firebase/app";
|
import firebase from "firebase/app"
|
||||||
import { getFirestore } from "firebase/firestore"
|
import "firebase/firestore"
|
||||||
|
|
||||||
const app = initializeApp({ projectId: "next-auth-test" });
|
const firestore = (
|
||||||
const firestore = getFirestore(app);
|
firebase.apps[0] ?? firebase.initializeApp(/* your config */)
|
||||||
|
).firestore()
|
||||||
|
|
||||||
// For more information on each option (and a full list of options) go to
|
// For more information on each option (and a full list of options) go to
|
||||||
// https://next-auth.js.org/configuration/options
|
// https://next-auth.js.org/configuration/options
|
||||||
@@ -50,7 +51,7 @@ export default NextAuth({
|
|||||||
clientSecret: process.env.GOOGLE_SECRET,
|
clientSecret: process.env.GOOGLE_SECRET,
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
adapter: FirestoreAdapter(firestore),
|
adapter: FirebaseAdapter(firestore),
|
||||||
...
|
...
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@@ -82,7 +83,7 @@ See [firebase.google.com/docs/web/setup](https://firebase.google.com/docs/web/se
|
|||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
We're open to all community contributions! If you'd like to contribute in any way, please read our [Contributing Guide](https://github.com/nextauthjs/next-auth/blob/main/CONTRIBUTING.md).
|
We're open to all community contributions! If you'd like to contribute in any way, please read our [Contributing Guide](https://github.com/nextauthjs/adapters/blob/main/CONTRIBUTING.md).
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
module.exports = require("@next-auth/adapter-test/jest/jest-preset")
|
module.exports = require("@next-auth/adapter-test/jest.config")
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
{
|
{
|
||||||
"name": "@next-auth/firebase-adapter",
|
"name": "@next-auth/firebase-adapter",
|
||||||
"version": "1.0.0",
|
"version": "0.1.3",
|
||||||
"description": "Firebase adapter for next-auth.",
|
"description": "Firebase adapter for next-auth.",
|
||||||
"homepage": "https://next-auth.js.org",
|
"homepage": "https://next-auth.js.org",
|
||||||
"repository": "https://github.com/nextauthjs/next-auth",
|
"repository": "https://github.com/nextauthjs/adapters",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/nextauthjs/next-auth/issues"
|
"url": "https://github.com/nextauthjs/adapters/issues"
|
||||||
},
|
},
|
||||||
"author": "Ron Houben <ron.houben85@gmail.com>",
|
"author": "Ron Houben <ron.houben85@gmail.com>",
|
||||||
"contributors": [
|
"contributors": [
|
||||||
@@ -28,18 +28,18 @@
|
|||||||
"access": "public"
|
"access": "public"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build:wip": "tsc",
|
||||||
"test": "FIRESTORE_EMULATOR_HOST=localhost:8080 firebase emulators:exec --only firestore --project next-auth-test jest"
|
"test:wip": "FIRESTORE_EMULATOR_HOST=localhost:8080 firebase emulators:exec --only firestore --project next-auth-test jest"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"firebase": "^9.7.0",
|
"firebase": "^8.6.2",
|
||||||
"next-auth": "workspace:*"
|
"next-auth": "workspace:*"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@next-auth/adapter-test": "workspace:^0.0.0",
|
"@next-auth/adapter-test": "workspace:^0.0.0",
|
||||||
"@next-auth/tsconfig": "workspace:^0.0.0",
|
"@next-auth/tsconfig": "workspace:^0.0.0",
|
||||||
"firebase": "^9.7.0",
|
"firebase": "^8.6.2",
|
||||||
"firebase-tools": "^10.7.2",
|
"firebase-tools": "^9.11.0",
|
||||||
"jest": "^27.4.3",
|
"jest": "^27.4.3",
|
||||||
"next-auth": "workspace:*"
|
"next-auth": "workspace:*"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,58 +0,0 @@
|
|||||||
import { Timestamp } from "firebase/firestore"
|
|
||||||
import type {
|
|
||||||
FirestoreDataConverter,
|
|
||||||
QueryDocumentSnapshot,
|
|
||||||
WithFieldValue,
|
|
||||||
} from "firebase/firestore"
|
|
||||||
|
|
||||||
const isTimestamp = (value: unknown): value is Timestamp =>
|
|
||||||
typeof value === "object" && value !== null && value instanceof Timestamp
|
|
||||||
|
|
||||||
interface GetConverterOptions {
|
|
||||||
excludeId?: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
export const getConverter = <Document extends Record<string, unknown>>(
|
|
||||||
options?: GetConverterOptions
|
|
||||||
): FirestoreDataConverter<Document> => ({
|
|
||||||
// `PartialWithFieldValue` implicitly types `object` as `any`, so we want to explicitly type it
|
|
||||||
toFirestore(object: WithFieldValue<Document>) {
|
|
||||||
const document: Record<string, unknown> = {}
|
|
||||||
|
|
||||||
Object.keys(object).forEach((key) => {
|
|
||||||
if (object[key] !== undefined) {
|
|
||||||
document[key] = object[key]
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return document
|
|
||||||
},
|
|
||||||
// We need to explicitly type `snapshot` since it uses `DocumentData` for generic type
|
|
||||||
fromFirestore(snapshot: QueryDocumentSnapshot<Document>) {
|
|
||||||
if (!snapshot.exists()) {
|
|
||||||
return snapshot
|
|
||||||
}
|
|
||||||
|
|
||||||
let document: Document = snapshot.data()
|
|
||||||
|
|
||||||
if (!options?.excludeId) {
|
|
||||||
document = {
|
|
||||||
...document,
|
|
||||||
id: snapshot.id,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const key in document) {
|
|
||||||
const value = document[key]
|
|
||||||
|
|
||||||
if (isTimestamp(value)) {
|
|
||||||
document = {
|
|
||||||
...document,
|
|
||||||
[key]: value.toDate(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return document
|
|
||||||
},
|
|
||||||
})
|
|
||||||
@@ -1,289 +1,283 @@
|
|||||||
import { initializeApp } from "firebase/app"
|
import type firebase from "firebase"
|
||||||
import type { FirebaseOptions } from "firebase/app"
|
import { createHash, randomBytes } from "crypto"
|
||||||
|
import { Adapter } from "next-auth/adapters"
|
||||||
import {
|
import {
|
||||||
addDoc,
|
querySnapshotToObject,
|
||||||
collection,
|
docSnapshotToObject,
|
||||||
deleteDoc,
|
stripUndefined,
|
||||||
doc,
|
} from "./utils"
|
||||||
getDoc,
|
import { Profile, Session, User } from "next-auth"
|
||||||
getDocs,
|
|
||||||
getFirestore,
|
|
||||||
limit,
|
|
||||||
query,
|
|
||||||
runTransaction,
|
|
||||||
setDoc,
|
|
||||||
where,
|
|
||||||
connectFirestoreEmulator,
|
|
||||||
} from "firebase/firestore"
|
|
||||||
import type { Account } from "next-auth"
|
|
||||||
import type {
|
|
||||||
Adapter,
|
|
||||||
AdapterSession,
|
|
||||||
AdapterUser,
|
|
||||||
VerificationToken,
|
|
||||||
} from "next-auth/adapters"
|
|
||||||
|
|
||||||
import { getConverter } from "./converter"
|
interface FirebaseVerificationRequest {
|
||||||
|
id: string
|
||||||
type IndexableObject = Record<string, unknown>
|
identifier: string
|
||||||
|
token: string
|
||||||
export interface FirestoreAdapterOptions {
|
expires: Date
|
||||||
emulator?: {
|
|
||||||
host?: string
|
|
||||||
port?: number
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function FirestoreAdapter({
|
export type FirebaseSession = Session & {
|
||||||
emulator,
|
id: string
|
||||||
...firebaseOptions
|
expires: Date
|
||||||
}: FirebaseOptions & FirestoreAdapterOptions): Adapter {
|
|
||||||
const firebaseApp = initializeApp(firebaseOptions)
|
|
||||||
const db = getFirestore(firebaseApp)
|
|
||||||
|
|
||||||
if (emulator) {
|
|
||||||
connectFirestoreEmulator(
|
|
||||||
db,
|
|
||||||
emulator?.host ?? "localhost",
|
|
||||||
emulator?.port ?? 3001
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const Users = collection(db, "users").withConverter(
|
// @ts-expect-error
|
||||||
getConverter<AdapterUser>()
|
export const FirebaseAdapter: Adapter<
|
||||||
)
|
firebase.firestore.Firestore,
|
||||||
const Sessions = collection(db, "sessions").withConverter(
|
never,
|
||||||
getConverter<AdapterSession & IndexableObject>()
|
User & { id: string },
|
||||||
)
|
Profile,
|
||||||
const Accounts = collection(db, "accounts").withConverter(
|
FirebaseSession
|
||||||
getConverter<Account>()
|
> = (client) => {
|
||||||
)
|
return {
|
||||||
const VerificationTokens = collection(db, "verificationTokens").withConverter(
|
async getAdapter({ session, secret, ...appOptions }) {
|
||||||
getConverter<VerificationToken & IndexableObject>({ excludeId: true })
|
const sessionMaxAge = session.maxAge * 1000 // default is 30 days
|
||||||
)
|
const sessionUpdateAge = session.updateAge * 1000 // default is 1 day
|
||||||
|
/**
|
||||||
|
* @todo Move this to core package
|
||||||
|
* @todo Use bcrypt or a more secure method
|
||||||
|
*/
|
||||||
|
const hashToken = (token: string) =>
|
||||||
|
createHash("sha256").update(`${token}${secret}`).digest("hex")
|
||||||
|
|
||||||
return {
|
return {
|
||||||
async createUser(newUser) {
|
displayName: "FIREBASE",
|
||||||
const userRef = await addDoc(Users, newUser)
|
async createUser(profile) {
|
||||||
const userSnapshot = await getDoc(userRef)
|
const userRef = await client.collection("users").add(
|
||||||
|
stripUndefined({
|
||||||
if (userSnapshot.exists() && Users.converter) {
|
name: profile.name,
|
||||||
return Users.converter.fromFirestore(userSnapshot)
|
email: profile.email,
|
||||||
}
|
image: profile.image,
|
||||||
|
emailVerified: profile.emailVerified ?? null,
|
||||||
throw new Error("[createUser] Failed to create user")
|
})
|
||||||
},
|
|
||||||
async getUser(id) {
|
|
||||||
const userSnapshot = await getDoc(doc(Users, id))
|
|
||||||
|
|
||||||
if (userSnapshot.exists() && Users.converter) {
|
|
||||||
return Users.converter.fromFirestore(userSnapshot)
|
|
||||||
}
|
|
||||||
|
|
||||||
return null
|
|
||||||
},
|
|
||||||
async getUserByEmail(email) {
|
|
||||||
const userQuery = query(Users, where("email", "==", email), limit(1))
|
|
||||||
const userSnapshots = await getDocs(userQuery)
|
|
||||||
const userSnpashot = userSnapshots.docs[0]
|
|
||||||
|
|
||||||
if (userSnpashot?.exists() && Users.converter) {
|
|
||||||
return Users.converter.fromFirestore(userSnpashot)
|
|
||||||
}
|
|
||||||
|
|
||||||
return null
|
|
||||||
},
|
|
||||||
async getUserByAccount({ provider, providerAccountId }) {
|
|
||||||
const accountQuery = query(
|
|
||||||
Accounts,
|
|
||||||
where("provider", "==", provider),
|
|
||||||
where("providerAccountId", "==", providerAccountId),
|
|
||||||
limit(1)
|
|
||||||
)
|
)
|
||||||
const accountSnapshots = await getDocs(accountQuery)
|
const snapshot = await userRef.get()
|
||||||
const accountSnapshot = accountSnapshots.docs[0]
|
const user = docSnapshotToObject(snapshot)
|
||||||
|
return user
|
||||||
if (accountSnapshot?.exists()) {
|
|
||||||
const { userId } = accountSnapshot.data()
|
|
||||||
const userDoc = await getDoc(doc(Users, userId))
|
|
||||||
|
|
||||||
if (userDoc.exists() && Users.converter) {
|
|
||||||
return Users.converter.fromFirestore(userDoc)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null
|
|
||||||
},
|
},
|
||||||
|
|
||||||
async updateUser(partialUser) {
|
async getUser(id) {
|
||||||
const userRef = doc(Users, partialUser.id)
|
const snapshot = await client.collection("users").doc(id).get()
|
||||||
|
const user = docSnapshotToObject(snapshot)
|
||||||
|
return user
|
||||||
|
},
|
||||||
|
|
||||||
await setDoc(userRef, partialUser, { merge: true })
|
async getUserByEmail(email) {
|
||||||
|
if (!email) return null
|
||||||
|
|
||||||
const userSnapshot = await getDoc(userRef)
|
const snapshot = await client
|
||||||
|
.collection("users")
|
||||||
|
.where("email", "==", email)
|
||||||
|
.limit(1)
|
||||||
|
.get()
|
||||||
|
|
||||||
if (userSnapshot.exists() && Users.converter) {
|
const user = querySnapshotToObject(snapshot)
|
||||||
return Users.converter.fromFirestore(userSnapshot)
|
return user
|
||||||
}
|
},
|
||||||
|
|
||||||
throw new Error("[updateUser] Failed to update user")
|
async getUserByProviderAccountId(providerId, providerAccountId) {
|
||||||
|
const accountSnapshot = await client
|
||||||
|
.collection("accounts")
|
||||||
|
.where("providerId", "==", providerId)
|
||||||
|
.where("providerAccountId", "==", providerAccountId)
|
||||||
|
.limit(1)
|
||||||
|
.get()
|
||||||
|
|
||||||
|
if (accountSnapshot.empty) return null
|
||||||
|
|
||||||
|
const userId = accountSnapshot.docs[0].data().userId
|
||||||
|
|
||||||
|
const userSnapshot = await client
|
||||||
|
.collection("users")
|
||||||
|
.doc(userId)
|
||||||
|
.get()
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
||||||
|
return { ...userSnapshot.data(), id: userSnapshot.id } as any
|
||||||
|
},
|
||||||
|
|
||||||
|
async updateUser(user) {
|
||||||
|
await client
|
||||||
|
.collection("users")
|
||||||
|
.doc(user.id)
|
||||||
|
.update(stripUndefined(user))
|
||||||
|
|
||||||
|
return user
|
||||||
},
|
},
|
||||||
|
|
||||||
async deleteUser(userId) {
|
async deleteUser(userId) {
|
||||||
const userRef = doc(Users, userId)
|
await client.collection("users").doc(userId).delete()
|
||||||
const accountsQuery = query(Accounts, where("userId", "==", userId))
|
},
|
||||||
const sessionsQuery = query(Sessions, where("userId", "==", userId))
|
|
||||||
|
|
||||||
// TODO: May be better to use events instead of transactions?
|
async linkAccount(
|
||||||
await runTransaction(db, async (transaction) => {
|
userId,
|
||||||
const accounts = await getDocs(accountsQuery)
|
providerId,
|
||||||
const sessions = await getDocs(sessionsQuery)
|
providerType,
|
||||||
|
providerAccountId,
|
||||||
transaction.delete(userRef)
|
refreshToken,
|
||||||
accounts.forEach((account) => transaction.delete(account.ref))
|
accessToken,
|
||||||
sessions.forEach((session) => transaction.delete(session.ref))
|
accessTokenExpires
|
||||||
|
) {
|
||||||
|
const accountRef = await client.collection("accounts").add(
|
||||||
|
stripUndefined({
|
||||||
|
userId,
|
||||||
|
providerId,
|
||||||
|
providerType,
|
||||||
|
providerAccountId,
|
||||||
|
refreshToken,
|
||||||
|
accessToken,
|
||||||
|
accessTokenExpires,
|
||||||
})
|
})
|
||||||
},
|
|
||||||
|
|
||||||
async linkAccount(account) {
|
|
||||||
const accountRef = await addDoc(Accounts, account)
|
|
||||||
const accountSnapshot = await getDoc(accountRef)
|
|
||||||
|
|
||||||
if (accountSnapshot.exists() && Accounts.converter) {
|
|
||||||
return Accounts.converter.fromFirestore(accountSnapshot)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
async unlinkAccount({ provider, providerAccountId }) {
|
|
||||||
const accountQuery = query(
|
|
||||||
Accounts,
|
|
||||||
where("provider", "==", provider),
|
|
||||||
where("providerAccountId", "==", providerAccountId),
|
|
||||||
limit(1)
|
|
||||||
)
|
)
|
||||||
const accountSnapshots = await getDocs(accountQuery)
|
|
||||||
const accountSnapshot = accountSnapshots.docs[0]
|
|
||||||
|
|
||||||
if (accountSnapshot?.exists()) {
|
const accountSnapshot = await accountRef.get()
|
||||||
await deleteDoc(accountSnapshot.ref)
|
const account = docSnapshotToObject(accountSnapshot)
|
||||||
}
|
return account
|
||||||
},
|
},
|
||||||
|
|
||||||
async createSession(session) {
|
async unlinkAccount(userId, providerId, providerAccountId) {
|
||||||
const sessionRef = await addDoc(Sessions, session)
|
const snapshot = await client
|
||||||
const sessionSnapshot = await getDoc(sessionRef)
|
.collection("accounts")
|
||||||
|
.where("userId", "==", userId)
|
||||||
|
.where("providerId", "==", providerId)
|
||||||
|
.where("providerAccountId", "==", providerAccountId)
|
||||||
|
.limit(1)
|
||||||
|
.get()
|
||||||
|
|
||||||
if (sessionSnapshot.exists() && Sessions.converter) {
|
const accountId = snapshot.docs[0].id
|
||||||
return Sessions.converter.fromFirestore(sessionSnapshot)
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Error("[createSession] Failed to create session")
|
await client.collection("accounts").doc(accountId).delete()
|
||||||
},
|
},
|
||||||
|
|
||||||
async getSessionAndUser(sessionToken) {
|
async createSession(user) {
|
||||||
const sessionQuery = query(
|
const sessionRef = await client.collection("sessions").add({
|
||||||
Sessions,
|
userId: user.id,
|
||||||
where("sessionToken", "==", sessionToken),
|
expires: new Date(Date.now() + sessionMaxAge),
|
||||||
limit(1)
|
sessionToken: randomBytes(32).toString("hex"),
|
||||||
)
|
accessToken: randomBytes(32).toString("hex"),
|
||||||
const sessionSnapshots = await getDocs(sessionQuery)
|
})
|
||||||
const sessionSnapshot = sessionSnapshots.docs[0]
|
const snapshot = await sessionRef.get()
|
||||||
|
const session = docSnapshotToObject(snapshot)
|
||||||
|
return session
|
||||||
|
},
|
||||||
|
|
||||||
if (sessionSnapshot?.exists() && Sessions.converter) {
|
async getSession(sessionToken) {
|
||||||
const session = Sessions.converter.fromFirestore(sessionSnapshot)
|
const snapshot = await client
|
||||||
const userDoc = await getDoc(doc(Users, session.userId))
|
.collection("sessions")
|
||||||
|
.where("sessionToken", "==", sessionToken)
|
||||||
|
.limit(1)
|
||||||
|
.get()
|
||||||
|
|
||||||
if (userDoc.exists() && Users.converter) {
|
const session = querySnapshotToObject<FirebaseSession>(snapshot)
|
||||||
const user = Users.converter.fromFirestore(userDoc)
|
if (!session) return null
|
||||||
|
|
||||||
return { session, user }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// if the session has expired
|
||||||
|
if (session.expires < new Date()) {
|
||||||
|
// delete the session
|
||||||
|
await client.collection("sessions").doc(session.id).delete()
|
||||||
return null
|
return null
|
||||||
|
}
|
||||||
|
// return already existing session
|
||||||
|
return session
|
||||||
},
|
},
|
||||||
|
|
||||||
async updateSession(partialSession) {
|
async updateSession(session, force) {
|
||||||
const sessionQuery = query(
|
if (
|
||||||
Sessions,
|
!force &&
|
||||||
where("sessionToken", "==", partialSession.sessionToken),
|
Number(session.expires) - sessionMaxAge + sessionUpdateAge >
|
||||||
limit(1)
|
Date.now()
|
||||||
)
|
) {
|
||||||
const sessionSnapshots = await getDocs(sessionQuery)
|
return null
|
||||||
const sessionSnapshot = sessionSnapshots.docs[0]
|
}
|
||||||
|
|
||||||
if (sessionSnapshot?.exists()) {
|
// Update the item in the database
|
||||||
await setDoc(sessionSnapshot.ref, partialSession, { merge: true })
|
await client
|
||||||
|
.collection("sessions")
|
||||||
const sessionDoc = await getDoc(sessionSnapshot.ref)
|
.doc(session.id)
|
||||||
|
.update({
|
||||||
if (sessionDoc?.exists() && Sessions.converter) {
|
expires: new Date(Date.now() + sessionMaxAge),
|
||||||
const session = Sessions.converter.fromFirestore(sessionDoc)
|
})
|
||||||
|
|
||||||
return session
|
return session
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null
|
|
||||||
},
|
},
|
||||||
|
|
||||||
async deleteSession(sessionToken) {
|
async deleteSession(sessionToken) {
|
||||||
const sessionQuery = query(
|
const snapshot = await client
|
||||||
Sessions,
|
.collection("sessions")
|
||||||
where("sessionToken", "==", sessionToken),
|
.where("sessionToken", "==", sessionToken)
|
||||||
limit(1)
|
.limit(1)
|
||||||
)
|
.get()
|
||||||
const sessionSnapshots = await getDocs(sessionQuery)
|
|
||||||
const sessionSnapshot = sessionSnapshots.docs[0]
|
|
||||||
|
|
||||||
if (sessionSnapshot?.exists()) {
|
const session = querySnapshotToObject<FirebaseSession>(snapshot)
|
||||||
await deleteDoc(sessionSnapshot.ref)
|
if (!session) return
|
||||||
}
|
|
||||||
|
await client.collection("sessions").doc(session.id).delete()
|
||||||
},
|
},
|
||||||
|
|
||||||
async createVerificationToken(verificationToken) {
|
async createVerificationRequest(identifier, url, token, _, provider) {
|
||||||
const verificationTokenRef = await addDoc(
|
const verificationRequestRef = await client
|
||||||
VerificationTokens,
|
.collection("verificationRequests")
|
||||||
verificationToken
|
.add({
|
||||||
)
|
identifier,
|
||||||
const verificationTokenSnapshot = await getDoc(verificationTokenRef)
|
token: hashToken(token),
|
||||||
|
expires: new Date(Date.now() + provider.maxAge * 1000),
|
||||||
|
})
|
||||||
|
|
||||||
if (verificationTokenSnapshot.exists() && VerificationTokens.converter) {
|
// With the verificationCallback on a provider, you can send an email, or queue
|
||||||
const {
|
// an email to be sent, or perform some other action (e.g. send a text message)
|
||||||
id,
|
await provider.sendVerificationRequest({
|
||||||
...verificationToken
|
identifier,
|
||||||
} = VerificationTokens.converter.fromFirestore(
|
url,
|
||||||
verificationTokenSnapshot
|
token,
|
||||||
)
|
baseUrl: appOptions.baseUrl,
|
||||||
|
provider,
|
||||||
|
})
|
||||||
|
|
||||||
return verificationToken
|
const snapshot = await verificationRequestRef.get()
|
||||||
}
|
return docSnapshotToObject<FirebaseVerificationRequest>(snapshot)
|
||||||
},
|
},
|
||||||
|
|
||||||
async useVerificationToken({ identifier, token }) {
|
async getVerificationRequest(identifier, token) {
|
||||||
const verificationTokensQuery = query(
|
const snapshot = await client
|
||||||
VerificationTokens,
|
.collection("verificationRequests")
|
||||||
where("identifier", "==", identifier),
|
.where("token", "==", hashToken(token))
|
||||||
where("token", "==", token),
|
.where("identifier", "==", identifier)
|
||||||
limit(1)
|
.limit(1)
|
||||||
)
|
.get()
|
||||||
const verificationTokenSnapshots = await getDocs(verificationTokensQuery)
|
|
||||||
const verificationTokenSnapshot = verificationTokenSnapshots.docs[0]
|
|
||||||
|
|
||||||
if (verificationTokenSnapshot?.exists() && VerificationTokens.converter) {
|
const verificationRequest =
|
||||||
await deleteDoc(verificationTokenSnapshot.ref)
|
querySnapshotToObject<FirebaseVerificationRequest>(snapshot)
|
||||||
|
if (!verificationRequest) return null
|
||||||
const {
|
|
||||||
id,
|
|
||||||
...verificationToken
|
|
||||||
} = VerificationTokens.converter.fromFirestore(
|
|
||||||
verificationTokenSnapshot
|
|
||||||
)
|
|
||||||
|
|
||||||
return verificationToken
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (verificationRequest.expires < new Date()) {
|
||||||
|
// Delete verification entry so it cannot be used again
|
||||||
|
await client
|
||||||
|
.collection("verificationRequests")
|
||||||
|
.doc(verificationRequest.id)
|
||||||
|
.delete()
|
||||||
return null
|
return null
|
||||||
|
}
|
||||||
|
return verificationRequest
|
||||||
|
},
|
||||||
|
|
||||||
|
async deleteVerificationRequest(identifier, token) {
|
||||||
|
const snapshot = await client
|
||||||
|
.collection("verificationRequests")
|
||||||
|
.where("token", "==", hashToken(token))
|
||||||
|
.where("identifier", "==", identifier)
|
||||||
|
.limit(1)
|
||||||
|
.get()
|
||||||
|
|
||||||
|
const verificationRequest =
|
||||||
|
querySnapshotToObject<FirebaseVerificationRequest>(snapshot)
|
||||||
|
|
||||||
|
if (!verificationRequest) return null
|
||||||
|
|
||||||
|
await client
|
||||||
|
.collection("verificationRequests")
|
||||||
|
.doc(verificationRequest.id)
|
||||||
|
.delete()
|
||||||
|
},
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
40
packages/adapter-firebase/src/utils.ts
Normal file
40
packages/adapter-firebase/src/utils.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import type firebase from "firebase"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Takes in a snapshot and returns all of its `data()`,
|
||||||
|
* as well as `id` and `createdAt` and `updatedAt` `Date`
|
||||||
|
*/
|
||||||
|
export function docSnapshotToObject<T>(
|
||||||
|
snapshot: firebase.firestore.DocumentSnapshot<firebase.firestore.DocumentData>
|
||||||
|
): T | null {
|
||||||
|
if (!snapshot.exists) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
const data: any = snapshot.data()
|
||||||
|
if (data.expires) {
|
||||||
|
data.expires = data.expires.toDate()
|
||||||
|
}
|
||||||
|
return { id: snapshot.id, ...data }
|
||||||
|
}
|
||||||
|
|
||||||
|
export function querySnapshotToObject<T>(
|
||||||
|
snapshot: firebase.firestore.QuerySnapshot<firebase.firestore.DocumentData>
|
||||||
|
): T | null {
|
||||||
|
if (snapshot.empty) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
const doc = snapshot.docs[0]
|
||||||
|
|
||||||
|
const data: any = doc.data()
|
||||||
|
if (data.expires) {
|
||||||
|
data.expires = data.expires.toDate()
|
||||||
|
}
|
||||||
|
return { id: doc.id, ...data }
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Firebase does not like `undefined` values */
|
||||||
|
export function stripUndefined(obj: any) {
|
||||||
|
return Object.fromEntries(
|
||||||
|
Object.entries(obj).filter(([, value]) => typeof value !== "undefined")
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,77 +1,67 @@
|
|||||||
import { runBasicTests } from "@next-auth/adapter-test"
|
import { runBasicTests } from "@next-auth/adapter-test"
|
||||||
import { FirestoreAdapter } from "../src"
|
import { FirebaseAdapter } from "../src"
|
||||||
|
import { docSnapshotToObject, querySnapshotToObject } from "../src/utils"
|
||||||
|
|
||||||
import { getFirestore, connectFirestoreEmulator, terminate, collection, query, where, limit, getDocs, getDoc, doc } from "firebase/firestore"
|
import firebase from "firebase/app"
|
||||||
import { initializeApp } from "firebase/app";
|
import "firebase/firestore"
|
||||||
import { getConverter } from "../src/converter";
|
|
||||||
import type { AdapterSession, AdapterUser, VerificationToken } from "next-auth/adapters";
|
|
||||||
import type { Account } from "next-auth";
|
|
||||||
|
|
||||||
const app = initializeApp({ projectId: "next-auth-test" });
|
const firestore = (
|
||||||
const firestore = getFirestore(app);
|
firebase.apps[0] ?? firebase.initializeApp({ projectId: "next-auth-test" })
|
||||||
|
).firestore()
|
||||||
connectFirestoreEmulator(firestore, 'localhost', 8080);
|
firestore.useEmulator("localhost", 8080)
|
||||||
|
|
||||||
type IndexableObject = Record<string, unknown>;
|
|
||||||
|
|
||||||
const Users = collection(firestore, 'users').withConverter(getConverter<AdapterUser>());
|
|
||||||
const Sessions = collection(firestore, 'sessions').withConverter(getConverter<AdapterSession & IndexableObject>());
|
|
||||||
const Accounts = collection(firestore, 'accounts').withConverter(getConverter<Account>());
|
|
||||||
const VerificationTokens = collection(firestore, 'verificationTokens').withConverter(getConverter<VerificationToken & IndexableObject>({ excludeId: true }));
|
|
||||||
|
|
||||||
runBasicTests({
|
runBasicTests({
|
||||||
adapter: FirestoreAdapter({ projectId: "next-auth-test" }),
|
adapter: FirebaseAdapter(firestore),
|
||||||
db: {
|
db: {
|
||||||
async disconnect() {
|
async disconnect() {
|
||||||
await terminate(firestore);
|
await firestore.terminate()
|
||||||
},
|
},
|
||||||
async session(sessionToken) {
|
async session(sessionToken) {
|
||||||
const snapshotQuery = query(Sessions, where("sessionToken", "==", sessionToken), limit(1));
|
const snapshot = await firestore
|
||||||
const snapshots = await getDocs(snapshotQuery);
|
.collection("sessions")
|
||||||
const snapshot = snapshots.docs[0];
|
.where("sessionToken", "==", sessionToken)
|
||||||
|
.limit(1)
|
||||||
|
.get()
|
||||||
|
return querySnapshotToObject(snapshot)
|
||||||
|
},
|
||||||
|
async expireSession(sessionToken, expires) {
|
||||||
|
const snapshot = await firestore
|
||||||
|
.collection("sessions")
|
||||||
|
.where("sessionToken", "==", sessionToken)
|
||||||
|
.limit(1)
|
||||||
|
.get()
|
||||||
|
|
||||||
if (snapshot?.exists() && Sessions.converter) {
|
if (snapshot.empty) {
|
||||||
const session = Sessions.converter.fromFirestore(snapshot);
|
console.error(sessionToken, expires)
|
||||||
|
throw new Error("Could not expire session")
|
||||||
return session;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return await firestore
|
||||||
|
.collection("sessions")
|
||||||
|
.doc(snapshot.docs[0].id)
|
||||||
|
.update({ expires })
|
||||||
},
|
},
|
||||||
async user(id) {
|
async user(id) {
|
||||||
const snapshot = await getDoc(doc(Users, id));
|
const snapshot = await firestore.collection("users").doc(id).get()
|
||||||
|
return docSnapshotToObject(snapshot)
|
||||||
if (snapshot?.exists() && Users.converter) {
|
|
||||||
const user = Users.converter.fromFirestore(snapshot);
|
|
||||||
|
|
||||||
return user;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
},
|
},
|
||||||
async account({ provider, providerAccountId }) {
|
async account(providerId, providerAccountId) {
|
||||||
const snapshotQuery = query(Accounts, where("provider", "==", provider), where("providerAccountId", "==", providerAccountId), limit(1));
|
const snapshot = await firestore
|
||||||
const snapshots = await getDocs(snapshotQuery);
|
.collection("accounts")
|
||||||
const snapshot = snapshots.docs[0];
|
.where("providerId", "==", providerId)
|
||||||
|
.where("providerAccountId", "==", providerAccountId)
|
||||||
if (snapshot?.exists() && Accounts.converter) {
|
.limit(1)
|
||||||
const account = Accounts.converter.fromFirestore(snapshot);
|
.get()
|
||||||
|
return querySnapshotToObject(snapshot)
|
||||||
return account;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
},
|
},
|
||||||
async verificationToken({ identifier, token }) {
|
async verificationRequest(identifier, token) {
|
||||||
const snapshotQuery = query(VerificationTokens, where("identifier", "==", identifier), where("token", "==", token), limit(1));
|
const snapshot = await firestore
|
||||||
const snapshots = await getDocs(snapshotQuery);
|
.collection("verificationRequests")
|
||||||
const snapshot = snapshots.docs[0];
|
.where("identifier", "==", identifier)
|
||||||
|
.where("token", "==", token)
|
||||||
if (snapshot?.exists() && VerificationTokens.converter) {
|
.limit(1)
|
||||||
const verificationToken = VerificationTokens.converter.fromFirestore(snapshot);
|
.get()
|
||||||
|
return querySnapshotToObject(snapshot)
|
||||||
return verificationToken;
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
{
|
{
|
||||||
"extends": "@next-auth/tsconfig/adapters.json",
|
"extends": "@next-auth/tsconfig/base.json",
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"rootDir": "src",
|
"rootDir": "src",
|
||||||
"outDir": "dist",
|
"outDir": "dist"
|
||||||
"strict": true,
|
|
||||||
"noUncheckedIndexedAccess": true
|
|
||||||
},
|
},
|
||||||
"exclude": ["tests", "dist", "jest.config.js"]
|
"exclude": ["tests", "dist", "jest.config.js"]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
Open Source. Full Stack. Own Your Data.
|
Open Source. Full Stack. Own Your Data.
|
||||||
</p>
|
</p>
|
||||||
<p align="center" style="align: center;">
|
<p align="center" style="align: center;">
|
||||||
<img src="https://github.com/nextauthjs/next-auth/actions/workflows/release.yml/badge.svg?branch=main" alt="CI Test" />
|
<img src="https://github.com/nextauthjs/adapters/actions/workflows/release.yml/badge.svg" alt="CI Test" />
|
||||||
<a href="https://www.npmjs.com/package/@next-auth/mikro-orm-adapter" target="_blank"><img src="https://img.shields.io/bundlephobia/minzip/@next-auth/mikro-orm-adapter/next" alt="Bundle Size"/></a>
|
<a href="https://www.npmjs.com/package/@next-auth/mikro-orm-adapter" target="_blank"><img src="https://img.shields.io/bundlephobia/minzip/@next-auth/mikro-orm-adapter/next" alt="Bundle Size"/></a>
|
||||||
<a href="https://www.npmjs.com/package/@next-auth/mikro-orm-adapter" target="_blank"><img src="https://img.shields.io/npm/v/@next-auth/mikro-orm-adapter/next" alt="@next-auth/mikro-orm-adapter Version" /></a>
|
<a href="https://www.npmjs.com/package/@next-auth/mikro-orm-adapter" target="_blank"><img src="https://img.shields.io/npm/v/@next-auth/mikro-orm-adapter/next" alt="@next-auth/mikro-orm-adapter Version" /></a>
|
||||||
</p>
|
</p>
|
||||||
@@ -49,7 +49,7 @@ This is the MikroORM Adapter for [`next-auth`](https://next-auth.js.org). This p
|
|||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
We're open to all community contributions! If you'd like to contribute in any way, please read our [Contributing Guide](https://github.com/nextauthjs/next-auth/blob/main/CONTRIBUTING.md).
|
We're open to all community contributions! If you'd like to contribute in any way, please read our [Contributing Guide](https://github.com/nextauthjs/adapters/blob/main/CONTRIBUTING.md).
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"version": "2.0.1",
|
"version": "2.0.1",
|
||||||
"description": "MikroORM adapter for next-auth.",
|
"description": "MikroORM adapter for next-auth.",
|
||||||
"homepage": "https://next-auth.js.org",
|
"homepage": "https://next-auth.js.org",
|
||||||
"repository": "https://github.com/nextauthjs/next-auth",
|
"repository": "https://github.com/nextauthjs/adapters",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/nextauthjs/next-auth/issues"
|
"url": "https://github.com/nextauthjs/next-auth/issues"
|
||||||
},
|
},
|
||||||
@@ -33,21 +33,17 @@
|
|||||||
],
|
],
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"@mikro-orm/core": "^5.0.2",
|
"@mikro-orm/core": "^5.0.2",
|
||||||
"next-auth": "^4"
|
"next-auth": "workspace:*"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@mikro-orm/core": "^5.0.2",
|
"@mikro-orm/core": "^5.0.2",
|
||||||
"@mikro-orm/sqlite": "^5.0.2",
|
"@mikro-orm/sqlite": "^5.0.2",
|
||||||
"@next-auth/adapter-test": "workspace:^0.0.0",
|
"@next-auth/adapter-test": "workspace:^0.0.0",
|
||||||
"@next-auth/tsconfig": "workspace:^0.0.0",
|
"@next-auth/tsconfig": "workspace:^0.0.0",
|
||||||
"@types/uuid": "^8.3.3",
|
|
||||||
"jest": "^27.4.3",
|
"jest": "^27.4.3",
|
||||||
"next-auth": "workspace:*"
|
"next-auth": "workspace:*"
|
||||||
},
|
},
|
||||||
"jest": {
|
"jest": {
|
||||||
"preset": "@next-auth/adapter-test/jest"
|
"preset": "@next-auth/adapter-test/jest"
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"uuid": "^8.3.2"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
Open Source. Full Stack. Own Your Data.
|
Open Source. Full Stack. Own Your Data.
|
||||||
</p>
|
</p>
|
||||||
<p align="center" style="align: center;">
|
<p align="center" style="align: center;">
|
||||||
<img src="https://github.com/nextauthjs/next-auth/actions/workflows/release.yml/badge.svg?branch=main" alt="CI Test" />
|
<img src="https://github.com/nextauthjs/adapters/actions/workflows/release.yml/badge.svg" alt="CI Test" />
|
||||||
<a href="https://www.npmjs.com/package/@next-auth/mongodb-adapter" target="_blank"><img src="https://img.shields.io/bundlephobia/minzip/@next-auth/mongodb-adapter" alt="Bundle Size"/></a>
|
<a href="https://www.npmjs.com/package/@next-auth/mongodb-adapter" target="_blank"><img src="https://img.shields.io/bundlephobia/minzip/@next-auth/mongodb-adapter" alt="Bundle Size"/></a>
|
||||||
<a href="https://www.npmjs.com/package/@next-auth/mongodb-adapter" target="_blank"><img src="https://img.shields.io/npm/v/@next-auth/mongodb-adapter" alt="@next-auth/mongodb-adapter Version" /></a>
|
<a href="https://www.npmjs.com/package/@next-auth/mongodb-adapter" target="_blank"><img src="https://img.shields.io/npm/v/@next-auth/mongodb-adapter" alt="@next-auth/mongodb-adapter Version" /></a>
|
||||||
</p>
|
</p>
|
||||||
@@ -79,7 +79,7 @@ export default NextAuth({
|
|||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
We're open to all community contributions! If you'd like to contribute in any way, please read our [Contributing Guide](https://github.com/nextauthjs/next-auth/blob/main/CONTRIBUTING.md).
|
We're open to all community contributions! If you'd like to contribute in any way, please read our [Contributing Guide](https://github.com/nextauthjs/adapters/blob/main/CONTRIBUTING.md).
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"version": "1.0.3",
|
"version": "1.0.3",
|
||||||
"description": "mongoDB adapter for next-auth.",
|
"description": "mongoDB adapter for next-auth.",
|
||||||
"homepage": "https://next-auth.js.org",
|
"homepage": "https://next-auth.js.org",
|
||||||
"repository": "https://github.com/nextauthjs/next-auth",
|
"repository": "https://github.com/nextauthjs/adapters",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/nextauthjs/next-auth/issues"
|
"url": "https://github.com/nextauthjs/next-auth/issues"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
Open Source. Full Stack. Own Your Data.
|
Open Source. Full Stack. Own Your Data.
|
||||||
</p>
|
</p>
|
||||||
<p align="center" style="align: center;">
|
<p align="center" style="align: center;">
|
||||||
<img src="https://github.com/nextauthjs/next-auth/actions/workflows/release.yml/badge.svg?branch=main" alt="Canary CI Test" />
|
<img src="https://github.com/nextauthjs/adapters/actions/workflows/release.yml/badge.svg" alt="Canary CI Test" />
|
||||||
<img src="https://img.shields.io/bundlephobia/minzip/@next-auth/neo4j-adapter" alt="Bundle Size"/>
|
<img src="https://img.shields.io/bundlephobia/minzip/@next-auth/neo4j-adapter" alt="Bundle Size"/>
|
||||||
<img src="https://img.shields.io/npm/v/@next-auth/neo4j-adapter" alt="@next-auth/neo4j-adapter Version" />
|
<img src="https://img.shields.io/npm/v/@next-auth/neo4j-adapter" alt="@next-auth/neo4j-adapter Version" />
|
||||||
</p>
|
</p>
|
||||||
@@ -50,7 +50,7 @@ export default NextAuth({
|
|||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
We're open to all community contributions! If you'd like to contribute in any way, please first read our [Contributing Guide](https://github.com/nextauthjs/next-auth/blob/canary/CONTRIBUTING.md).
|
We're open to all community contributions! If you'd like to contribute in any way, please first read our [Contributing Guide](https://github.com/nextauthjs/adapters/blob/canary/CONTRIBUTING.md).
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"version": "1.0.3",
|
"version": "1.0.3",
|
||||||
"description": "neo4j adapter for next-auth.",
|
"description": "neo4j adapter for next-auth.",
|
||||||
"homepage": "https://next-auth.js.org",
|
"homepage": "https://next-auth.js.org",
|
||||||
"repository": "https://github.com/nextauthjs/next-auth",
|
"repository": "https://github.com/nextauthjs/adapters",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/nextauthjs/next-auth/issues"
|
"url": "https://github.com/nextauthjs/next-auth/issues"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
Open Source. Full Stack. Own Your Data.
|
Open Source. Full Stack. Own Your Data.
|
||||||
</p>
|
</p>
|
||||||
<p align="center" style="align: center;">
|
<p align="center" style="align: center;">
|
||||||
<img src="https://github.com/nextauthjs/next-auth/actions/workflows/release.yml/badge.svg?branch=main" alt="CI Test" />
|
<img src="https://github.com/nextauthjs/adapters/actions/workflows/release.yml/badge.svg" alt="CI Test" />
|
||||||
<img src="https://img.shields.io/bundlephobia/minzip/@next-auth/pouchdb-adapter" alt="Bundle Size"/>
|
<img src="https://img.shields.io/bundlephobia/minzip/@next-auth/pouchdb-adapter" alt="Bundle Size"/>
|
||||||
<img src="https://img.shields.io/npm/v/@next-auth/pouchdb-adapter" alt="@next-auth/pouchdb-adapter Version" />
|
<img src="https://img.shields.io/npm/v/@next-auth/pouchdb-adapter" alt="@next-auth/pouchdb-adapter Version" />
|
||||||
</p>
|
</p>
|
||||||
@@ -71,7 +71,7 @@ For more details, please see https://pouchdb.com/api.html#sync
|
|||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
We're open to all community contributions! If you'd like to contribute in any way, please read our [Contributing Guide](https://github.com/nextauthjs/next-auth/blob/main/CONTRIBUTING.md).
|
We're open to all community contributions! If you'd like to contribute in any way, please read our [Contributing Guide](https://github.com/nextauthjs/adapters/blob/main/CONTRIBUTING.md).
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"version": "0.1.3",
|
"version": "0.1.3",
|
||||||
"description": "PouchDB adapter for next-auth.",
|
"description": "PouchDB adapter for next-auth.",
|
||||||
"homepage": "https://next-auth.js.org",
|
"homepage": "https://next-auth.js.org",
|
||||||
"repository": "https://github.com/nextauthjs/next-auth",
|
"repository": "https://github.com/nextauthjs/adapters",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/nextauthjs/next-auth/issues"
|
"url": "https://github.com/nextauthjs/next-auth/issues"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
Open Source. Full Stack. Own Your Data.
|
Open Source. Full Stack. Own Your Data.
|
||||||
</p>
|
</p>
|
||||||
<p align="center" style="align: center;">
|
<p align="center" style="align: center;">
|
||||||
<img src="https://github.com/nextauthjs/next-auth/actions/workflows/release.yml/badge.svg?branch=main" alt="CI Test" />
|
<img src="https://github.com/nextauthjs/adapters/actions/workflows/release.yml/badge.svg" alt="CI Test" />
|
||||||
<a href="https://www.npmjs.com/package/@next-auth/prisma-adapter" target="_blank"><img src="https://img.shields.io/bundlephobia/minzip/@next-auth/prisma-adapter/next" alt="Bundle Size"/></a>
|
<a href="https://www.npmjs.com/package/@next-auth/prisma-adapter" target="_blank"><img src="https://img.shields.io/bundlephobia/minzip/@next-auth/prisma-adapter/next" alt="Bundle Size"/></a>
|
||||||
<a href="https://www.npmjs.com/package/@next-auth/prisma-adapter" target="_blank"><img src="https://img.shields.io/npm/v/@next-auth/prisma-adapter/next" alt="@next-auth/prisma-adapter Version" /></a>
|
<a href="https://www.npmjs.com/package/@next-auth/prisma-adapter" target="_blank"><img src="https://img.shields.io/npm/v/@next-auth/prisma-adapter/next" alt="@next-auth/prisma-adapter Version" /></a>
|
||||||
</p>
|
</p>
|
||||||
@@ -48,7 +48,7 @@ export default NextAuth({
|
|||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
We're open to all community contributions! If you'd like to contribute in any way, please read our [Contributing Guide](https://github.com/nextauthjs/next-auth/blob/main/CONTRIBUTING.md).
|
We're open to all community contributions! If you'd like to contribute in any way, please read our [Contributing Guide](https://github.com/nextauthjs/adapters/blob/main/CONTRIBUTING.md).
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"version": "1.0.3",
|
"version": "1.0.3",
|
||||||
"description": "Prisma adapter for next-auth.",
|
"description": "Prisma adapter for next-auth.",
|
||||||
"homepage": "https://next-auth.js.org",
|
"homepage": "https://next-auth.js.org",
|
||||||
"repository": "https://github.com/nextauthjs/next-auth",
|
"repository": "https://github.com/nextauthjs/adapters",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/nextauthjs/next-auth/issues"
|
"url": "https://github.com/nextauthjs/next-auth/issues"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
Open Source. Full Stack. Own Your Data.
|
Open Source. Full Stack. Own Your Data.
|
||||||
</p>
|
</p>
|
||||||
<p align="center" style="align: center;">
|
<p align="center" style="align: center;">
|
||||||
<img src="https://github.com/nextauthjs/next-auth/actions/workflows/release.yml/badge.svg?branch=main" alt="CI Test" />
|
<img src="https://github.com/nextauthjs/adapters/actions/workflows/release.yml/badge.svg" alt="CI Test" />
|
||||||
<img src="https://img.shields.io/bundlephobia/minzip/@next-auth/sequelize-adapter" alt="Bundle Size"/>
|
<img src="https://img.shields.io/bundlephobia/minzip/@next-auth/sequelize-adapter" alt="Bundle Size"/>
|
||||||
<img src="https://img.shields.io/npm/v/@next-auth/sequelize-adapter" alt="@next-auth/sequelize-adapter Version" />
|
<img src="https://img.shields.io/npm/v/@next-auth/sequelize-adapter" alt="@next-auth/sequelize-adapter Version" />
|
||||||
</p>
|
</p>
|
||||||
@@ -89,7 +89,7 @@ export default NextAuth({
|
|||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
We're open to all community contributions! If you'd like to contribute in any way, please read our [Contributing Guide](https://github.com/nextauthjs/next-auth/blob/main/CONTRIBUTING.md).
|
We're open to all community contributions! If you'd like to contribute in any way, please read our [Contributing Guide](https://github.com/nextauthjs/adapters/blob/main/CONTRIBUTING.md).
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
{
|
{
|
||||||
"name": "@next-auth/sequelize-adapter",
|
"name": "@next-auth/sequelize-adapter",
|
||||||
"version": "1.0.5",
|
"version": "1.0.3",
|
||||||
"description": "Sequelize adapter for next-auth.",
|
"description": "Sequelize adapter for next-auth.",
|
||||||
"homepage": "https://next-auth.js.org",
|
"homepage": "https://next-auth.js.org",
|
||||||
"repository": "https://github.com/nextauthjs/next-auth",
|
"repository": "https://github.com/nextauthjs/adapters",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/nextauthjs/next-auth/issues"
|
"url": "https://github.com/nextauthjs/adapters/issues"
|
||||||
},
|
},
|
||||||
"author": "github.com/luke-j",
|
"author": "github.com/luke-j",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@@ -29,7 +29,7 @@
|
|||||||
"dist"
|
"dist"
|
||||||
],
|
],
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"next-auth": "^4",
|
"next-auth": "workspace:*",
|
||||||
"sequelize": "^6.6.5"
|
"sequelize": "^6.6.5"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -1,13 +1,7 @@
|
|||||||
const swcConfig = {
|
|
||||||
jsc: {
|
|
||||||
parser: { syntax: "typescript", decorators: true },
|
|
||||||
transform: { legacyDecorator: true, decoratorMetadata: true },
|
|
||||||
},
|
|
||||||
}
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
transform: {
|
transform: {
|
||||||
".(ts|tsx)$": ["@swc/jest", swcConfig],
|
".(ts|tsx)$": "ts-jest",
|
||||||
".(js|jsx)$": ["@swc/jest", swcConfig],
|
".(js|jsx)$": "babel-jest", // jest's default
|
||||||
},
|
},
|
||||||
transformIgnorePatterns: ["[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$"],
|
transformIgnorePatterns: ["[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$"],
|
||||||
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
|
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
"@types/nodemailer": "^6.4.4",
|
"@types/nodemailer": "^6.4.4",
|
||||||
"@typescript-eslint/eslint-plugin": "^4.24.0",
|
"@typescript-eslint/eslint-plugin": "^4.24.0",
|
||||||
"@typescript-eslint/parser": "^4.24.0",
|
"@typescript-eslint/parser": "^4.24.0",
|
||||||
|
"babel-jest": "^27.4.2",
|
||||||
"eslint": "^7.27.0",
|
"eslint": "^7.27.0",
|
||||||
"eslint-config-prettier": "^8.3.0",
|
"eslint-config-prettier": "^8.3.0",
|
||||||
"eslint-config-standard-with-typescript": "^20.0.0",
|
"eslint-config-standard-with-typescript": "^20.0.0",
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
Open Source. Full Stack. Own Your Data.
|
Open Source. Full Stack. Own Your Data.
|
||||||
</p>
|
</p>
|
||||||
<p align="center" style="align: center;">
|
<p align="center" style="align: center;">
|
||||||
<img src="https://github.com/nextauthjs/next-auth/actions/workflows/release.yml/badge.svg?branch=main" alt="Canary CI Test" />
|
<img src="https://github.com/nextauthjs/adapters/actions/workflows/release.yml/badge.svg" alt="Canary CI Test" />
|
||||||
<img src="https://img.shields.io/bundlephobia/minzip/@next-auth/typeorm-legacy-adapter/canary" alt="Bundle Size"/>
|
<img src="https://img.shields.io/bundlephobia/minzip/@next-auth/typeorm-legacy-adapter/canary" alt="Bundle Size"/>
|
||||||
<img src="https://img.shields.io/npm/v/@next-auth/typeorm-legacy-adapter" alt="@next-auth/typeorm-legacy-adapter Version" />
|
<img src="https://img.shields.io/npm/v/@next-auth/typeorm-legacy-adapter" alt="@next-auth/typeorm-legacy-adapter Version" />
|
||||||
</p>
|
</p>
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
{
|
{
|
||||||
"name": "@next-auth/typeorm-legacy-adapter",
|
"name": "@next-auth/typeorm-legacy-adapter",
|
||||||
"version": "2.0.0",
|
"version": "1.0.3",
|
||||||
"description": "TypeORM (legacy) adapter for next-auth.",
|
"description": "TypeORM (legacy) adapter for next-auth.",
|
||||||
"homepage": "https://next-auth.js.org",
|
"homepage": "https://next-auth.js.org",
|
||||||
"repository": "https://github.com/nextauthjs/next-auth",
|
"repository": "https://github.com/nextauthjs/adapters",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/nextauthjs/next-auth/issues"
|
"url": "https://github.com/nextauthjs/adapters/issues"
|
||||||
},
|
},
|
||||||
"author": "Iain Collins",
|
"author": "Iain Collins",
|
||||||
"contributors": [
|
"contributors": [
|
||||||
@@ -37,25 +37,24 @@
|
|||||||
"sqlite": "tests/sqlite/test.sh"
|
"sqlite": "tests/sqlite/test.sh"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@next-auth/adapter-test": "workspace:*",
|
"@next-auth/adapter-test": "workspace:^0.0.0",
|
||||||
"@next-auth/tsconfig": "workspace:*",
|
"@next-auth/tsconfig": "workspace:^0.0.0",
|
||||||
"jest": "^27.4.3",
|
"jest": "^27.4.3",
|
||||||
"mssql": "^7.2.1",
|
"mssql": "^7.2.1",
|
||||||
"mysql": "^2.18.1",
|
"mysql": "^2.18.1",
|
||||||
"next-auth": "workspace:*",
|
"next-auth": "workspace:*",
|
||||||
"pg": "^8.7.3",
|
"pg": "^8.7.1",
|
||||||
"sqlite3": "^5.0.8",
|
"sqlite3": "^5.0.2",
|
||||||
"typeorm": "0.3.7",
|
"typeorm": "^0.2.37",
|
||||||
"typeorm-naming-strategies": "^4.1.0",
|
"typeorm-naming-strategies": "^2.0.0"
|
||||||
"typescript": "^4.7.4"
|
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"mssql": "^6.2.1 || 7",
|
"mssql": "^6.2.1 || 7",
|
||||||
"mysql": "^2.18.1",
|
"mysql": "^2.18.1",
|
||||||
"next-auth": "^4",
|
"next-auth": "workspace:*",
|
||||||
"pg": "^8.2.1",
|
"pg": "^8.2.1",
|
||||||
"sqlite3": "^5.0.2",
|
"sqlite3": "^5.0.2",
|
||||||
"typeorm": "0.3.7"
|
"typeorm": "^0.2.31"
|
||||||
},
|
},
|
||||||
"peerDependenciesMeta": {
|
"peerDependenciesMeta": {
|
||||||
"mysql": {
|
"mysql": {
|
||||||
|
|||||||
@@ -1,8 +1,13 @@
|
|||||||
import type { Adapter, AdapterSession, AdapterUser } from "next-auth/adapters"
|
import { Adapter, AdapterSession, AdapterUser } from "next-auth/adapters"
|
||||||
import { DataSourceOptions, DataSource, EntityManager } from "typeorm"
|
import {
|
||||||
import type { Account } from "next-auth"
|
Connection,
|
||||||
|
ConnectionOptions,
|
||||||
|
EntityManager,
|
||||||
|
getConnectionManager,
|
||||||
|
} from "typeorm"
|
||||||
|
import { Account } from "next-auth"
|
||||||
import * as defaultEntities from "./entities"
|
import * as defaultEntities from "./entities"
|
||||||
import { parseDataSourceConfig, updateConnectionEntities } from "./utils"
|
import { parseConnectionConfig, updateConnectionEntities } from "./utils"
|
||||||
|
|
||||||
export const entities = defaultEntities
|
export const entities = defaultEntities
|
||||||
|
|
||||||
@@ -12,40 +17,43 @@ export interface TypeORMLegacyAdapterOptions {
|
|||||||
entities?: Entities
|
entities?: Entities
|
||||||
}
|
}
|
||||||
|
|
||||||
let _dataSource: DataSource | undefined
|
let _connection: Connection
|
||||||
|
|
||||||
export async function getManager(options: {
|
export async function getManager(options: {
|
||||||
dataSource: string | DataSourceOptions
|
connection: string | ConnectionOptions
|
||||||
entities: Entities
|
entities: Entities
|
||||||
}): Promise<EntityManager> {
|
}): Promise<EntityManager> {
|
||||||
const { dataSource, entities } = options
|
const { connection, entities } = options
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
...parseDataSourceConfig(dataSource),
|
...parseConnectionConfig(connection),
|
||||||
entities: Object.values(entities),
|
entities: Object.values(entities),
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_dataSource) _dataSource = new DataSource(config)
|
const connectionManager = getConnectionManager()
|
||||||
|
|
||||||
const manager = _dataSource?.manager
|
if (connectionManager.has(config.name ?? "default")) {
|
||||||
|
_connection = connectionManager.get(config.name ?? "default")
|
||||||
|
|
||||||
if (!manager.connection.isInitialized) {
|
if (_connection.isConnected) return _connection.manager
|
||||||
await manager.connection.initialize()
|
|
||||||
}
|
|
||||||
|
|
||||||
if (process.env.NODE_ENV !== "production") {
|
if (process.env.NODE_ENV !== "production") {
|
||||||
await updateConnectionEntities(_dataSource, config.entities)
|
await updateConnectionEntities(_connection, config.entities)
|
||||||
}
|
}
|
||||||
return manager
|
} else {
|
||||||
|
_connection = await connectionManager.create(config).connect()
|
||||||
|
}
|
||||||
|
|
||||||
|
return _connection.manager
|
||||||
}
|
}
|
||||||
|
|
||||||
export function TypeORMLegacyAdapter(
|
export function TypeORMLegacyAdapter(
|
||||||
dataSource: string | DataSourceOptions,
|
connection: string | ConnectionOptions,
|
||||||
options?: TypeORMLegacyAdapterOptions
|
options?: TypeORMLegacyAdapterOptions
|
||||||
): Adapter {
|
): Adapter {
|
||||||
const entities = options?.entities
|
const entities = options?.entities
|
||||||
const c = {
|
const c = {
|
||||||
dataSource,
|
connection,
|
||||||
entities: {
|
entities: {
|
||||||
UserEntity: entities?.UserEntity ?? defaultEntities.UserEntity,
|
UserEntity: entities?.UserEntity ?? defaultEntities.UserEntity,
|
||||||
SessionEntity: entities?.SessionEntity ?? defaultEntities.SessionEntity,
|
SessionEntity: entities?.SessionEntity ?? defaultEntities.SessionEntity,
|
||||||
@@ -74,14 +82,14 @@ export function TypeORMLegacyAdapter(
|
|||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
async getUser(id) {
|
async getUser(id) {
|
||||||
const m = await getManager(c)
|
const m = await getManager(c)
|
||||||
const user = await m.findOne("UserEntity", { where: { id } })
|
const user = await m.findOne("UserEntity", { id })
|
||||||
if (!user) return null
|
if (!user) return null
|
||||||
return { ...user }
|
return { ...user }
|
||||||
},
|
},
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
async getUserByEmail(email) {
|
async getUserByEmail(email) {
|
||||||
const m = await getManager(c)
|
const m = await getManager(c)
|
||||||
const user = await m.findOne("UserEntity", { where: { email } })
|
const user = await m.findOne("UserEntity", { email })
|
||||||
if (!user) return null
|
if (!user) return null
|
||||||
return { ...user }
|
return { ...user }
|
||||||
},
|
},
|
||||||
@@ -89,7 +97,8 @@ export function TypeORMLegacyAdapter(
|
|||||||
const m = await getManager(c)
|
const m = await getManager(c)
|
||||||
const account = await m.findOne<Account & { user: AdapterUser }>(
|
const account = await m.findOne<Account & { user: AdapterUser }>(
|
||||||
"AccountEntity",
|
"AccountEntity",
|
||||||
{ where: provider_providerAccountId, relations: ["user"] }
|
provider_providerAccountId,
|
||||||
|
{ relations: ["user"] }
|
||||||
)
|
)
|
||||||
if (!account) return null
|
if (!account) return null
|
||||||
return account.user ?? null
|
return account.user ?? null
|
||||||
@@ -127,7 +136,7 @@ export function TypeORMLegacyAdapter(
|
|||||||
const m = await getManager(c)
|
const m = await getManager(c)
|
||||||
const sessionAndUser = await m.findOne<
|
const sessionAndUser = await m.findOne<
|
||||||
AdapterSession & { user: AdapterUser }
|
AdapterSession & { user: AdapterUser }
|
||||||
>("SessionEntity", { where: { sessionToken }, relations: ["user"] })
|
>("SessionEntity", { sessionToken }, { relations: ["user"] })
|
||||||
|
|
||||||
if (!sessionAndUser) return null
|
if (!sessionAndUser) return null
|
||||||
const { user, ...session } = sessionAndUser
|
const { user, ...session } = sessionAndUser
|
||||||
@@ -153,9 +162,10 @@ export function TypeORMLegacyAdapter(
|
|||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
async useVerificationToken(identifier_token) {
|
async useVerificationToken(identifier_token) {
|
||||||
const m = await getManager(c)
|
const m = await getManager(c)
|
||||||
const verificationToken = await m.findOne("VerificationTokenEntity", {
|
const verificationToken = await m.findOne(
|
||||||
where: identifier_token,
|
"VerificationTokenEntity",
|
||||||
})
|
identifier_token
|
||||||
|
)
|
||||||
if (!verificationToken) {
|
if (!verificationToken) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import type { DataSource, DataSourceOptions } from "typeorm"
|
import { Connection, ConnectionOptions } from "typeorm"
|
||||||
import * as defaultEntities from "./entities"
|
import * as defaultEntities from "./entities"
|
||||||
|
|
||||||
/** Ensure configOrString is normalized to an object. */
|
/** Ensure configOrString is normalized to an object. */
|
||||||
export function parseDataSourceConfig(
|
export function parseConnectionConfig(
|
||||||
configOrString: string | DataSourceOptions
|
configOrString: string | ConnectionOptions
|
||||||
): DataSourceOptions {
|
): ConnectionOptions {
|
||||||
if (typeof configOrString !== "string") {
|
if (typeof configOrString !== "string") {
|
||||||
return {
|
return {
|
||||||
...configOrString,
|
...configOrString,
|
||||||
@@ -89,22 +89,22 @@ function entitiesChanged(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function updateConnectionEntities(
|
export async function updateConnectionEntities(
|
||||||
dataSource: DataSource,
|
connection: Connection,
|
||||||
entities: any[]
|
entities: any[]
|
||||||
) {
|
) {
|
||||||
if (!entitiesChanged(dataSource.entityMetadatas, entities)) return
|
if (!entitiesChanged(connection.options.entities, entities)) return
|
||||||
|
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
dataSource.entityMetadatas = entities
|
connection.options.entities = entities
|
||||||
|
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
dataSource.buildMetadatas()
|
connection.buildMetadatas()
|
||||||
|
|
||||||
if (dataSource.options.synchronize !== false) {
|
if (connection.options.synchronize !== false) {
|
||||||
console.warn(
|
console.warn(
|
||||||
"[next-auth][warn][adapter_typeorm_updating_entities]",
|
"[next-auth][warn][adapter_typeorm_updating_entities]",
|
||||||
"\nhttps://next-auth.js.org/warnings#adapter_typeorm_updating_entities"
|
"\nhttps://next-auth.js.org/warnings#adapter_typeorm_updating_entities"
|
||||||
)
|
)
|
||||||
await dataSource.synchronize()
|
await connection.synchronize()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,49 +1,46 @@
|
|||||||
import { DataSource } from "typeorm"
|
import { ConnectionManager, ConnectionOptions } from "typeorm"
|
||||||
import type { DataSourceOptions } from "typeorm"
|
import { TestOptions } from "@next-auth/adapter-test"
|
||||||
import type { TestOptions } from "@next-auth/adapter-test"
|
|
||||||
import * as defaultEntities from "../src/entities"
|
import * as defaultEntities from "../src/entities"
|
||||||
import { parseDataSourceConfig } from "../src/utils"
|
import { parseConnectionConfig } from "../src/utils"
|
||||||
|
|
||||||
export { defaultEntities }
|
export { defaultEntities }
|
||||||
|
|
||||||
console.warn = jest.fn()
|
|
||||||
|
|
||||||
/** Set up Test Database */
|
/** Set up Test Database */
|
||||||
export function db(
|
export function db(
|
||||||
config: string | DataSourceOptions,
|
config: string | ConnectionOptions,
|
||||||
entities: typeof defaultEntities = defaultEntities
|
entities: typeof defaultEntities = defaultEntities
|
||||||
): TestOptions["db"] {
|
): TestOptions["db"] {
|
||||||
const connection = new DataSource({
|
const connection = new ConnectionManager().create({
|
||||||
...parseDataSourceConfig(config),
|
...parseConnectionConfig(config),
|
||||||
entities: Object.values(entities),
|
entities: Object.values(entities),
|
||||||
}).manager.connection
|
})
|
||||||
|
|
||||||
const m = connection.manager
|
const m = connection.manager
|
||||||
return {
|
return {
|
||||||
connect: async () => await connection.initialize(),
|
connect: async () => await connection.connect(),
|
||||||
disconnect: async () => await connection.destroy(),
|
disconnect: async () => await connection.close(),
|
||||||
async user(id) {
|
async user(id) {
|
||||||
const user = await m.findOne(entities.UserEntity, { where: { id } })
|
const user = await m.findOne(entities.UserEntity, id)
|
||||||
return user ?? null
|
return user ?? null
|
||||||
},
|
},
|
||||||
async account(provider_providerAccountId) {
|
async account(provider_providerAccountId) {
|
||||||
const account = await m.findOne(entities.AccountEntity, {
|
const account = await m.findOne(
|
||||||
where: provider_providerAccountId,
|
entities.AccountEntity,
|
||||||
})
|
provider_providerAccountId
|
||||||
|
)
|
||||||
return account ?? null
|
return account ?? null
|
||||||
},
|
},
|
||||||
async session(sessionToken) {
|
async session(sessionToken) {
|
||||||
const session = await m.findOne(entities.SessionEntity, {
|
const session = await m.findOne(entities.SessionEntity, { sessionToken })
|
||||||
where: { sessionToken },
|
|
||||||
})
|
|
||||||
return session ?? null
|
return session ?? null
|
||||||
},
|
},
|
||||||
async verificationToken(token_identifier) {
|
async verificationToken(token_identifier) {
|
||||||
const verificationToken = await m.findOne(
|
const verificationToken = await m.findOne(
|
||||||
entities.VerificationTokenEntity,
|
entities.VerificationTokenEntity,
|
||||||
{ where: token_identifier }
|
token_identifier
|
||||||
)
|
)
|
||||||
if (!verificationToken) return null
|
if (!verificationToken) return null
|
||||||
|
// @ts-expect-error
|
||||||
const { id: _, ...rest } = verificationToken
|
const { id: _, ...rest } = verificationToken
|
||||||
return rest
|
return rest
|
||||||
},
|
},
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user