From 06a7149b66433ffe630b18f5f7b937e2a9cd0ed5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Wed, 14 Jun 2023 14:09:29 +0200 Subject: [PATCH] feat: introduce `@auth/supabase-adapter` (#7807) Database adapters are not dependent on Next.js features, so it makes sense to republish them under the `@auth/*` scope. This PR is part of a series to convert adapters, using `@auth/core` for types. If you are coming from the previous adapter, change your `package.json`: ```diff - "@next-auth/supabase-adapter": "0.0.0", + "@auth/supabase-adapter": "0.0.0", ``` And run `npm install`, `yarn install` or `pnpm install` respectively. **Note:** This packages is published as ESM-only --- .github/ISSUE_TEMPLATE/3_bug_adapter.yml | 2 +- .github/issue-labeler.yml | 2 +- apps/dev/nextjs-v4/package.json | 2 +- .../pages/api/auth-old/[...nextauth].ts | 2 +- apps/dev/nextjs/package.json | 2 +- .../nextjs/pages/api/auth/[...nextauth].ts | 2 +- packages/adapter-supabase/README.md | 10 +- packages/adapter-supabase/package.json | 29 +++- .../adapter-supabase/src/database.types.ts | 139 ---------------- packages/adapter-supabase/src/index.ts | 149 ++++++++++++++++-- packages/adapter-supabase/tests/index.test.ts | 4 +- pnpm-lock.yaml | 13 +- turbo.json | 4 +- 13 files changed, 182 insertions(+), 178 deletions(-) delete mode 100644 packages/adapter-supabase/src/database.types.ts diff --git a/.github/ISSUE_TEMPLATE/3_bug_adapter.yml b/.github/ISSUE_TEMPLATE/3_bug_adapter.yml index f7046da1..c26da536 100644 --- a/.github/ISSUE_TEMPLATE/3_bug_adapter.yml +++ b/.github/ISSUE_TEMPLATE/3_bug_adapter.yml @@ -31,7 +31,7 @@ body: - "@auth/pouchdb-adapter" - "@auth/prisma-adapter" - "@auth/sequelize-adapter" - - "@next-auth/supabase-adapter" + - "@auth/supabase-adapter" - "@auth/typeorm-adapter" - "@auth/upstash-redis-adapter" - "@auth/xata-adapter" diff --git a/.github/issue-labeler.yml b/.github/issue-labeler.yml index a72a2f3c..eec31eeb 100644 --- a/.github/issue-labeler.yml +++ b/.github/issue-labeler.yml @@ -31,7 +31,7 @@ sequelize: - "@auth/sequelize-adapter" supabase: - - "@next-auth/supabase-adapter" + - "@auth/supabase-adapter" typeorm: - "@auth/typeorm-adapter" diff --git a/apps/dev/nextjs-v4/package.json b/apps/dev/nextjs-v4/package.json index 9623a3a1..e7171002 100644 --- a/apps/dev/nextjs-v4/package.json +++ b/apps/dev/nextjs-v4/package.json @@ -16,7 +16,7 @@ "dependencies": { "@auth/fauna-adapter": "workspace:*", "@auth/prisma-adapter": "workspace:*", - "@next-auth/supabase-adapter": "workspace:*", + "@auth/supabase-adapter": "workspace:*", "@auth/typeorm-adapter": "workspace:*", "@prisma/client": "^3", "@supabase/supabase-js": "^2.0.5", diff --git a/apps/dev/nextjs-v4/pages/api/auth-old/[...nextauth].ts b/apps/dev/nextjs-v4/pages/api/auth-old/[...nextauth].ts index 84db8539..5d2ed2bc 100644 --- a/apps/dev/nextjs-v4/pages/api/auth-old/[...nextauth].ts +++ b/apps/dev/nextjs-v4/pages/api/auth-old/[...nextauth].ts @@ -60,7 +60,7 @@ import WorkOS from "next-auth/providers/workos" // }) // // Supabase -// import { SupabaseAdapter } from "@next-auth/supabase-adapter" +// import { SupabaseAdapter } from "@auth/supabase-adapter" // const adapter = SupabaseAdapter({ // url: process.env.NEXT_PUBLIC_SUPABASE_URL, // secret: process.env.SUPABASE_SERVICE_ROLE_KEY, diff --git a/apps/dev/nextjs/package.json b/apps/dev/nextjs/package.json index a32819ea..9716028e 100644 --- a/apps/dev/nextjs/package.json +++ b/apps/dev/nextjs/package.json @@ -17,7 +17,7 @@ "@auth/core": "workspace:*", "@auth/fauna-adapter": "workspace:*", "@auth/prisma-adapter": "workspace:*", - "@next-auth/supabase-adapter": "workspace:*", + "@auth/supabase-adapter": "workspace:*", "@auth/typeorm-adapter": "workspace:*", "@prisma/client": "^3", "@supabase/supabase-js": "^2.0.5", diff --git a/apps/dev/nextjs/pages/api/auth/[...nextauth].ts b/apps/dev/nextjs/pages/api/auth/[...nextauth].ts index ab820f18..5c7c3ec6 100644 --- a/apps/dev/nextjs/pages/api/auth/[...nextauth].ts +++ b/apps/dev/nextjs/pages/api/auth/[...nextauth].ts @@ -64,7 +64,7 @@ import WorkOS from "@auth/core/providers/workos" // }) // // Supabase -// import { SupabaseAdapter } from "@next-auth/supabase-adapter" +// import { SupabaseAdapter } from "@auth/supabase-adapter" // const adapter = SupabaseAdapter({ // url: process.env.NEXT_PUBLIC_SUPABASE_URL, // secret: process.env.SUPABASE_SERVICE_ROLE_KEY, diff --git a/packages/adapter-supabase/README.md b/packages/adapter-supabase/README.md index 821aff07..d898f973 100644 --- a/packages/adapter-supabase/README.md +++ b/packages/adapter-supabase/README.md @@ -8,14 +8,14 @@

Supabase Adapter - NextAuth.js / Auth.js

- + TypeScript - - npm + + npm - - Downloads + + Downloads Github Stars diff --git a/packages/adapter-supabase/package.json b/packages/adapter-supabase/package.json index e9b6fc55..5bd9e804 100644 --- a/packages/adapter-supabase/package.json +++ b/packages/adapter-supabase/package.json @@ -1,14 +1,26 @@ { - "name": "@next-auth/supabase-adapter", - "version": "0.2.1", - "description": "Supabase adapter for next-auth.", + "name": "@auth/supabase-adapter", + "version": "0.0.0", + "description": "Supabase adapter for Auth.js", "homepage": "https://authjs.dev", "repository": "https://github.com/nextauthjs/next-auth", "bugs": { "url": "https://github.com/nextauthjs/next-auth/issues" }, "author": "Martin Sonnberger ", - "main": "dist/index.js", + "type": "module", + "types": "./index.d.ts", + "files": [ + "*.js", + "*.d.ts*", + "src" + ], + "exports": { + ".": { + "types": "./index.d.ts", + "import": "./index.js" + } + }, "keywords": [ "next-auth", "next.js", @@ -23,16 +35,17 @@ "build": "tsc", "test": "./tests/test.sh" }, + "dependencies": { + "@auth/core": "workspace:*" + }, "peerDependencies": { - "@supabase/supabase-js": "^2.0.5", - "next-auth": "^4.18.7" + "@supabase/supabase-js": "^2.0.5" }, "devDependencies": { "@next-auth/adapter-test": "workspace:^0.0.0", "@next-auth/tsconfig": "workspace:^0.0.0", "@supabase/supabase-js": "^2.0.5", - "jest": "^27.4.3", - "next-auth": "workspace:*" + "jest": "^27.4.3" }, "jest": { "preset": "@next-auth/adapter-test/jest" diff --git a/packages/adapter-supabase/src/database.types.ts b/packages/adapter-supabase/src/database.types.ts deleted file mode 100644 index 6d048325..00000000 --- a/packages/adapter-supabase/src/database.types.ts +++ /dev/null @@ -1,139 +0,0 @@ -export type Json = - | string - | number - | boolean - | null - | { [key: string]: Json } - | Json[] - -export interface Database { - next_auth: { - Tables: { - accounts: { - Row: { - id: string - type: string | null - provider: string | null - providerAccountId: string | null - refresh_token: string | null - access_token: string | null - expires_at: number | null - token_type: string | null - scope: string | null - id_token: string | null - session_state: string | null - oauth_token_secret: string | null - oauth_token: string | null - userId: string | null - } - Insert: { - id?: string - type?: string | null - provider?: string | null - providerAccountId?: string | null - refresh_token?: string | null - access_token?: string | null - expires_at?: number | null - token_type?: string | null - scope?: string | null - id_token?: string | null - session_state?: string | null - oauth_token_secret?: string | null - oauth_token?: string | null - userId?: string | null - } - Update: { - id?: string - type?: string | null - provider?: string | null - providerAccountId?: string | null - refresh_token?: string | null - access_token?: string | null - expires_at?: number | null - token_type?: string | null - scope?: string | null - id_token?: string | null - session_state?: string | null - oauth_token_secret?: string | null - oauth_token?: string | null - userId?: string | null - } - } - sessions: { - Row: { - expires: string | null - sessionToken: string | null - userId: string | null - id: string - } - Insert: { - expires?: string | null - sessionToken?: string | null - userId?: string | null - id?: string - } - Update: { - expires?: string | null - sessionToken?: string | null - userId?: string | null - id?: string - } - } - users: { - Row: { - name: string | null - email: string | null - emailVerified: string | null - image: string | null - id: string - } - Insert: { - name?: string | null - email?: string | null - emailVerified?: string | null - image?: string | null - id?: string - } - Update: { - name?: string | null - email?: string | null - emailVerified?: string | null - image?: string | null - id?: string - } - } - verification_tokens: { - Row: { - id: number - identifier: string | null - token: string | null - expires: string | null - } - Insert: { - id?: number - identifier?: string | null - token?: string | null - expires?: string | null - } - Update: { - id?: number - identifier?: string | null - token?: string | null - expires?: string | null - } - } - } - Views: { - [_ in never]: never - } - Functions: { - uid: { - Args: Record - Returns: string - } - } - Enums: { - [_ in never]: never - } - } -} diff --git a/packages/adapter-supabase/src/index.ts b/packages/adapter-supabase/src/index.ts index b27d1739..1dcadddc 100644 --- a/packages/adapter-supabase/src/index.ts +++ b/packages/adapter-supabase/src/index.ts @@ -9,19 +9,18 @@ * ## Installation * * ```bash npm2yarn2pnpm - * npm install @supabase/supabase-js next-auth @next-auth/supabase-adapter + * npm install @supabase/supabase-js @auth/supabase-adapter * ``` * - * @module @next-auth/supabase-adapter + * @module @auth/supabase-adapter */ import { createClient } from "@supabase/supabase-js" -import { Database } from "./database.types" import { Adapter, AdapterSession, AdapterUser, VerificationToken, -} from "next-auth/adapters" +} from "@auth/core/adapters" function isDate(date: any) { return ( @@ -61,7 +60,7 @@ export interface SupabaseAdapterOptions { * :::note * This adapter is developed by the community and not officially maintained or supported by Supabase. It uses the Supabase Database to store user and session data in a separate `next_auth` schema. It is a standalone Auth server that does not interface with Supabase Auth and therefore provides a different feature set. * - * If you’re looking for an officially maintained Auth server with additional features like [built-in email server](https://supabase.com/docs/guides/auth/auth-email#configure-email-settings?utm_source=authjs-docs&medium=referral&campaign=authjs), [phone auth](https://supabase.com/docs/guides/auth/auth-twilio?utm_source=authjs-docs&medium=referral&campaign=authjs), and [Multi Factor Authentication (MFA / 2FA)](https://supabase.com/contact/mfa?utm_source=authjs-docs&medium=referral&campaign=authjs), please use [Supabase Auth](https://supabase.com/auth) with the [Auth Helpers for Next.js](https://supabase.com/docs/guides/auth/auth-helpers/nextjs?utm_source=authjs-docs&medium=referral&campaign=authjs). + * If you're looking for an officially maintained Auth server with additional features like [built-in email server](https://supabase.com/docs/guides/auth/auth-email#configure-email-settings?utm_source=authjs-docs&medium=referral&campaign=authjs), [phone auth](https://supabase.com/docs/guides/auth/auth-twilio?utm_source=authjs-docs&medium=referral&campaign=authjs), and [Multi Factor Authentication (MFA / 2FA)](https://supabase.com/contact/mfa?utm_source=authjs-docs&medium=referral&campaign=authjs), please use [Supabase Auth](https://supabase.com/auth) with the [Auth Helpers for Next.js](https://supabase.com/docs/guides/auth/auth-helpers/nextjs?utm_source=authjs-docs&medium=referral&campaign=authjs). * ::: * * ## Setup @@ -72,7 +71,7 @@ export interface SupabaseAdapterOptions { * * ```js title="pages/api/auth/[...nextauth].js" * import NextAuth from "next-auth" - * import { SupabaseAdapter } from "@next-auth/supabase-adapter" + * import { SupabaseAdapter } from "@auth/supabase-adapter" * * // For more information on each option (and a full list of options) go to * // https://authjs.dev/reference/configuration/auth-config @@ -224,7 +223,7 @@ export interface SupabaseAdapterOptions { * * ```js title="pages/api/auth/[...nextauth].js" * import NextAuth from "next-auth" - * import { SupabaseAdapter } from "@next-auth/supabase-adapter" + * import { SupabaseAdapter } from "@auth/supabase-adapter" * import jwt from "jsonwebtoken" * * // For more information on each option (and a full list of options) go to @@ -350,9 +349,7 @@ export function SupabaseAdapter(options: SupabaseAdapterOptions): Adapter { const { url, secret } = options const supabase = createClient(url, secret, { db: { schema: "next_auth" }, - global: { - headers: { "X-Client-Info": "@next-auth/supabase-adapter@0.1.0" }, - }, + global: { headers: { "X-Client-Info": "@auth/supabase-adapter" } }, }) return { async createUser(user) { @@ -524,3 +521,135 @@ export function SupabaseAdapter(options: SupabaseAdapterOptions): Adapter { }, } } + +interface Database { + next_auth: { + Tables: { + accounts: { + Row: { + id: string + type: string | null + provider: string | null + providerAccountId: string | null + refresh_token: string | null + access_token: string | null + expires_at: number | null + token_type: string | null + scope: string | null + id_token: string | null + session_state: string | null + oauth_token_secret: string | null + oauth_token: string | null + userId: string | null + } + Insert: { + id?: string + type?: string | null + provider?: string | null + providerAccountId?: string | null + refresh_token?: string | null + access_token?: string | null + expires_at?: number | null + token_type?: string | null + scope?: string | null + id_token?: string | null + session_state?: string | null + oauth_token_secret?: string | null + oauth_token?: string | null + userId?: string | null + } + Update: { + id?: string + type?: string | null + provider?: string | null + providerAccountId?: string | null + refresh_token?: string | null + access_token?: string | null + expires_at?: number | null + token_type?: string | null + scope?: string | null + id_token?: string | null + session_state?: string | null + oauth_token_secret?: string | null + oauth_token?: string | null + userId?: string | null + } + } + sessions: { + Row: { + expires: string | null + sessionToken: string | null + userId: string | null + id: string + } + Insert: { + expires?: string | null + sessionToken?: string | null + userId?: string | null + id?: string + } + Update: { + expires?: string | null + sessionToken?: string | null + userId?: string | null + id?: string + } + } + users: { + Row: { + name: string | null + email: string | null + emailVerified: string | null + image: string | null + id: string + } + Insert: { + name?: string | null + email?: string | null + emailVerified?: string | null + image?: string | null + id?: string + } + Update: { + name?: string | null + email?: string | null + emailVerified?: string | null + image?: string | null + id?: string + } + } + verification_tokens: { + Row: { + id: number + identifier: string | null + token: string | null + expires: string | null + } + Insert: { + id?: number + identifier?: string | null + token?: string | null + expires?: string | null + } + Update: { + id?: number + identifier?: string | null + token?: string | null + expires?: string | null + } + } + } + Views: { + [_ in never]: never + } + Functions: { + uid: { + Args: Record + Returns: string + } + } + Enums: { + [_ in never]: never + } + } +} diff --git a/packages/adapter-supabase/tests/index.test.ts b/packages/adapter-supabase/tests/index.test.ts index 16a8fea4..6e82dc1a 100644 --- a/packages/adapter-supabase/tests/index.test.ts +++ b/packages/adapter-supabase/tests/index.test.ts @@ -5,8 +5,8 @@ import type { AdapterSession, AdapterUser, VerificationToken, -} from "next-auth/adapters" -import type { Account } from "next-auth" +} from "@auth/core/adapters" +import type { Account } from "@auth/core/types" const url = process.env.SUPABASE_URL ?? "http://localhost:54321" const secret = diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 87746d88..6b31fccd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -56,8 +56,8 @@ importers: '@auth/core': workspace:* '@auth/fauna-adapter': workspace:* '@auth/prisma-adapter': workspace:* + '@auth/supabase-adapter': workspace:* '@auth/typeorm-adapter': workspace:* - '@next-auth/supabase-adapter': workspace:* '@playwright/test': 1.29.2 '@prisma/client': ^3 '@supabase/supabase-js': ^2.0.5 @@ -80,8 +80,8 @@ importers: '@auth/core': link:../../../packages/core '@auth/fauna-adapter': link:../../../packages/adapter-fauna '@auth/prisma-adapter': link:../../../packages/adapter-prisma + '@auth/supabase-adapter': link:../../../packages/adapter-supabase '@auth/typeorm-adapter': link:../../../packages/adapter-typeorm - '@next-auth/supabase-adapter': link:../../../packages/adapter-supabase '@prisma/client': 3.15.2_prisma@3.15.2 '@supabase/supabase-js': 2.0.5 faunadb: 4.6.0 @@ -106,8 +106,8 @@ importers: specifiers: '@auth/fauna-adapter': workspace:* '@auth/prisma-adapter': workspace:* + '@auth/supabase-adapter': workspace:* '@auth/typeorm-adapter': workspace:* - '@next-auth/supabase-adapter': workspace:* '@prisma/client': ^3 '@supabase/supabase-js': ^2.0.5 '@types/jsonwebtoken': ^8.5.5 @@ -127,8 +127,8 @@ importers: dependencies: '@auth/fauna-adapter': link:../../../packages/adapter-fauna '@auth/prisma-adapter': link:../../../packages/adapter-prisma + '@auth/supabase-adapter': link:../../../packages/adapter-supabase '@auth/typeorm-adapter': link:../../../packages/adapter-typeorm - '@next-auth/supabase-adapter': link:../../../packages/adapter-supabase '@prisma/client': 3.15.2_prisma@3.15.2 '@supabase/supabase-js': 2.0.5 faunadb: 4.6.0 @@ -433,17 +433,18 @@ importers: packages/adapter-supabase: specifiers: + '@auth/core': workspace:* '@next-auth/adapter-test': workspace:^0.0.0 '@next-auth/tsconfig': workspace:^0.0.0 '@supabase/supabase-js': ^2.0.5 jest: ^27.4.3 - next-auth: workspace:* + dependencies: + '@auth/core': link:../core devDependencies: '@next-auth/adapter-test': link:../adapter-test '@next-auth/tsconfig': link:../tsconfig '@supabase/supabase-js': 2.0.5 jest: 27.5.1 - next-auth: link:../next-auth packages/adapter-test: specifiers: diff --git a/turbo.json b/turbo.json index fe489cb8..aa532987 100644 --- a/turbo.json +++ b/turbo.json @@ -64,7 +64,7 @@ "@auth/neo4j-adapter#build", "@auth/pouchdb-adapter#build", "@auth/sequelize-adapter#build", - "@next-auth/supabase-adapter#build", + "@auth/supabase-adapter#build", "@auth/typeorm-adapter#build", "@auth/upstash-redis-adapter#build", "@auth/xata-adapter#build", @@ -87,7 +87,7 @@ "@auth/neo4j-adapter#build", "@auth/pouchdb-adapter#build", "@auth/sequelize-adapter#build", - "@next-auth/supabase-adapter#build", + "@auth/supabase-adapter#build", "@auth/typeorm-adapter#build", "@auth/upstash-redis-adapter#build", "@auth/xata-adapter#build",