main repo

This commit is contained in:
Basilosaurusrex
2025-11-24 18:09:40 +01:00
parent b636ee5e70
commit f027651f9b
34146 changed files with 4436636 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
> @supabase/auth-helpers-nextjs@0.10.0 build /home/runner/work/auth-helpers/auth-helpers/packages/nextjs
> tsup
CLI Building entry: src/index.ts
CLI Using tsconfig: tsconfig.json
CLI tsup v6.7.0
CLI Using tsup config: /home/runner/work/auth-helpers/auth-helpers/packages/nextjs/tsup.config.ts
CLI Target: node14
CLI Cleaning output folder
CJS Build start
CJS dist/index.js 12.61 KB
CJS dist/index.js.map 29.03 KB
CJS ⚡️ Build success in 27ms
DTS Build start
DTS ⚡️ Build success in 3560ms
DTS dist/index.d.ts 5.95 KB

424
node_modules/@supabase/auth-helpers-nextjs/CHANGELOG.md generated vendored Normal file
View File

@@ -0,0 +1,424 @@
# @supabase/auth-helpers-nextjs
## 0.10.0
### Minor Changes
- 9e7ff76: upgrade supabase-js version to v2.42.0
### Patch Changes
- Updated dependencies [9e7ff76]
- @supabase/auth-helpers-shared@0.7.0
## 0.9.0
### Minor Changes
- 18327fc: add isServer property to server-side storage adaptors
## 0.8.7
### Patch Changes
- f7e5c2d: Revert cookie name to storage key change
- Updated dependencies [f7e5c2d]
- @supabase/auth-helpers-shared@0.6.3
## 0.8.6
### Patch Changes
- 5893215: Update storage key name with cookie name
- Updated dependencies [5893215]
- @supabase/auth-helpers-shared@0.6.2
## 0.8.5
### Patch Changes
- 7abfe9b: Fix header and cookie trying to read undefined response body
## 0.8.4
### Patch Changes
- Updated dependencies [841fce0]
- @supabase/auth-helpers-shared@0.6.1
## 0.8.3
### Patch Changes
- Updated dependencies [9fa8f2b]
- @supabase/auth-helpers-shared@0.6.0
## 0.8.2
### Patch Changes
- c8a121d: include cookie options for deleteCookie
## 0.8.1
### Patch Changes
- dfff00a: Remove the appending of a cookie to the response
## 0.8.0
### Minor Changes
- c48bb87: Remove max-age option from cookieOptions
### Patch Changes
- Updated dependencies [c48bb87]
- @supabase/auth-helpers-shared@0.5.0
## 0.7.4
### Patch Changes
- 8c77550: Fix type for next.js cookies and headers functions
## 0.7.3
### Patch Changes
- 1619e93: Add cookie options to set cookie for route handler
## 0.7.2
### Patch Changes
- d33aa1b: Upgrade patch version of Next.js to fix TS def for `cookies().set()`
- Updated dependencies [97e5541]
- @supabase/auth-helpers-shared@0.4.1
## 0.7.1
### Patch Changes
- b20cc32: Allow user to opt out of singleton pattern for Client Components
## 0.7.0
### Minor Changes
- 65bf8d4: Add cookie storage adapter per library following a shared storage adapter
- 65bf8d4: Add full server side support to auth helpers through PKCE
### Patch Changes
- 165ba70: Fix "Failed to parse cookie string" bug in middleware storage
- a128b9e: Fix typedefs and add deprecated functions for App Router createClient functions
- d46c2ed: fix cookie saving in middleware/routeHandler
- 067402a: Set cookie header to make new session from middleware available to server component
- 66b13b9: simplifying defaults for storage adapter
- 008a08c: Implement singleton pattern for createClientComponentClient to simplify implementation
- d90866f: [BREAKING CHANGES]: Renamed createBrowserSupabaseClient to createPagesBrowserClient, createServerSupabaseClient to createPagesServerClient and createMiddlewareSupabaseClient to createMiddlewareClient
- 1086021: Upgrade Next.js and remove custom type for WritableRequestCookies
- Updated dependencies [65bf8d4]
- Updated dependencies [353be76]
- Updated dependencies [56db807]
- Updated dependencies [56db807]
- Updated dependencies [66b13b9]
- Updated dependencies [65bf8d4]
- @supabase/auth-helpers-shared@0.4.0
## 0.7.0-next.8
### Patch Changes
- a128b9e: Fix typedefs and add deprecated functions for App Router createClient functions
## 0.7.0-next.7
### Patch Changes
- 067402a: Set cookie header to make new session from middleware available to server component
- 008a08c: Implement singleton pattern for createClientComponentClient to simplify implementation
- 1086021: Upgrade Next.js and remove custom type for WritableRequestCookies
## 0.7.0-next.6
### Patch Changes
- d90866f: [BREAKING CHANGES]: Renamed createBrowserSupabaseClient to createPagesBrowserClient, createServerSupabaseClient to createPagesServerClient and createMiddlewareSupabaseClient to createMiddlewareClient
## 0.7.0-next.5
### Patch Changes
- d46c2ed: fix cookie saving in middleware/routeHandler
## 0.7.0-next.4
### Patch Changes
- 165ba70: Fix "Failed to parse cookie string" bug in middleware storage
## 0.7.0-next.3
### Patch Changes
- Updated dependencies [56db807]
- Updated dependencies [56db807]
- @supabase/auth-helpers-shared@0.4.0-next.3
## 0.7.0-next.2
### Patch Changes
- Updated dependencies [353be76]
- @supabase/auth-helpers-shared@0.4.0-next.2
## 0.7.0-next.1
### Patch Changes
- 66b13b9: simplifying defaults for storage adapter
- Updated dependencies [66b13b9]
- @supabase/auth-helpers-shared@0.4.0-next.1
## 0.7.0-next.0
### Minor Changes
- 65bf8d4: Add cookie storage adapter per library following a shared storage adapter
- 65bf8d4: Add full server side support to auth helpers through PKCE
### Patch Changes
- Updated dependencies [65bf8d4]
- Updated dependencies [65bf8d4]
- @supabase/auth-helpers-shared@0.4.0-next.0
## 0.6.0
### Minor Changes
- 50d0a16: Remove deprecated methods from the nextjs package
## 0.5.9
### Patch Changes
- Updated dependencies [1ea258e]
- @supabase/auth-helpers-shared@0.3.3
## 0.5.8
### Patch Changes
- Updated dependencies [185e9cf]
- @supabase/auth-helpers-shared@0.3.2
## 0.5.7
### Patch Changes
- Updated dependencies [f86073d]
- @supabase/auth-helpers-shared@0.3.1
## 0.5.6
### Patch Changes
- 5ab18fe: Export function to create Supabase Client in Next.js Route Handler functions
## 0.5.5
### Patch Changes
- Updated dependencies [33c8a81]
- @supabase/auth-helpers-shared@0.3.0
## 0.5.4
### Patch Changes
- 849e447: allow overwriting client url and key
## 0.5.3
### Patch Changes
- d3366e4: Allow passing client options
- Updated dependencies [d3366e4]
- @supabase/auth-helpers-shared@0.2.4
## 0.5.2
### Patch Changes
- 2be3f10: feat: add helper for Next.js Server Components.
## 0.5.1
### Patch Changes
- Updated dependencies [bee77c7]
- @supabase/auth-helpers-shared@0.2.3
## 0.5.0
### Minor Changes
- d8f7446: chore: deprecate withApiAuth, withPageAuth, and withMiddlewareAuth.
## 0.4.5
### Patch Changes
- d6c43ef: fix: "host" request header is not available. #358
## 0.4.4
### Patch Changes
- 5cdc84c: Fix a bug with withPageAuth when a session is null and authRequired is set to false
## 0.4.3
### Patch Changes
- Updated dependencies [999e57e]
- @supabase/auth-helpers-shared@0.2.2
## 0.4.2
### Patch Changes
- 2fda843: add missing supabase-js peerDependency
- 2fda843: update supabase-js
- Updated dependencies [2fda843]
- Updated dependencies [2fda843]
- @supabase/auth-helpers-shared@0.2.1
## 0.4.1
### Patch Changes
- 5140f5a: fix: withPageAuth return user. [#314](https://github.com/supabase/auth-helpers/issues/314)
## 0.4.0
### Minor Changes
- fd30e33: Update to work with supabase-js v2 RC
### Patch Changes
- 20fa944: add sveltekit supabase v2 support
- fe5c4a6: chore: improve types.
- 2fdb094: chore: types and middleware improvements.
- af28db1: chore: export middleware at root.
- Updated dependencies [20fa944]
- Updated dependencies [fd30e33]
- Updated dependencies [fe5c4a6]
- Updated dependencies [2fdb094]
- @supabase/auth-helpers-shared@0.2.0
## 0.4.0-next.4
### Patch Changes
- 20fa944: add sveltekit supabase v2 support
- Updated dependencies [20fa944]
- @supabase/auth-helpers-shared@0.2.0-next.3
## 0.4.0-next.3
### Patch Changes
- 2fdb094: chore: types and middleware improvements.
- Updated dependencies [2fdb094]
- @supabase/auth-helpers-shared@0.2.0-next.2
## 0.4.0-next.2
### Patch Changes
- fe5c4a6: chore: improve types.
- Updated dependencies [fe5c4a6]
- @supabase/auth-helpers-shared@0.2.0-next.1
## 0.4.0-next.1
### Patch Changes
- af28db1: chore: export middleware at root.
## 0.4.0-next.0
### Minor Changes
- 1b33e44: Update to work with supabase-js v2 RC
### Patch Changes
- Updated dependencies [1b33e44]
- @supabase/auth-helpers-shared@0.2.0-next.0
## 0.2.7
### Patch Changes
- 0ab05cf: Update format of x-client-info header
## 0.2.6
### Patch Changes
- 50669a6: Add x-client-info header to show package name and version
## 0.2.5
### Patch Changes
- 8e0b747: Change error handling to not show flow disruption errors
## 0.2.4
### Patch Changes
- 56228e3: Add getProviderToken helper method
- Updated dependencies [56228e3]
- @supabase/auth-helpers-shared@0.1.3
## 0.2.3
### Patch Changes
- 69fefcb: Change logger to be a wrapper for console
## 0.2.2
### Patch Changes
- 38ccf1c: Logger can be imported in your own project for setting log levels
- Updated dependencies [38ccf1c]
- @supabase/auth-helpers-shared@0.1.2
## 0.2.1
### Patch Changes
- 9dda264: Add better error handling and error codes
- Updated dependencies [9dda264]
- @supabase/auth-helpers-shared@0.1.1
## 0.2.0
### Minor Changes
- f399820: Using shared package as a dependency
Update sveltekit package with latest code to update tokens
## 0.1.0
### Minor Changes
- a3c2991: Initial release of new library version

21
node_modules/@supabase/auth-helpers-nextjs/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022 Supabase
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,236 @@
## Migrating to `0.5.X`
To make these helpers more flexible as well as more maintainable and easier to upgrade for new versions of Next.js, we're stripping them down to the most useful part which is managing the cookies and giving you an authenticated supabase-js client in any environment (client, server, middleware/edge).
Therefore we're marking the `withApiAuth`, `withPageAuth`, and `withMiddlewareAuth` higher order functions as deprectaed and they will be removed in the next **minor** release (v0.6.X).
Please follow the steps below to update your API routes, pages, and middleware handlers. Thanks!
### `withApiAuth` deprecated!
Use `createServerSupabaseClient` within your `NextApiHandler`:
#### Before
```tsx
// pages/api/protected-route.ts
import { withApiAuth } from '@supabase/auth-helpers-nextjs';
export default withApiAuth(async function ProtectedRoute(req, res, supabase) {
// Run queries with RLS on the server
const { data } = await supabase.from('test').select('*');
res.json(data);
});
```
#### After
```tsx
// pages/api/protected-route.ts
import { NextApiHandler } from 'next';
import { createServerSupabaseClient } from '@supabase/auth-helpers-nextjs';
const ProtectedRoute: NextApiHandler = async (req, res) => {
// Create authenticated Supabase Client
const supabase = createServerSupabaseClient({ req, res });
// Check if we have a session
const {
data: { session }
} = await supabase.auth.getSession();
if (!session)
return res.status(401).json({
error: 'not_authenticated',
description: 'The user does not have an active session or is not authenticated'
});
// Run queries with RLS on the server
const { data } = await supabase.from('test').select('*');
res.json(data);
};
export default ProtectedRoute;
```
### `withPageAuth` deprecated!
Use `createServerSupabaseClient` within `getServerSideProps`:
#### Before
```tsx
// pages/profile.js
import { withPageAuth, User } from '@supabase/auth-helpers-nextjs';
export default function Profile({ user }: { user: User }) {
return <pre>{JSON.stringify(user, null, 2)}</pre>;
}
export const getServerSideProps = withPageAuth({ redirectTo: '/' });
```
#### After
```tsx
// pages/profile.js
import { createServerSupabaseClient, User } from '@supabase/auth-helpers-nextjs';
import { GetServerSidePropsContext } from 'next';
export default function Profile({ user }: { user: User }) {
return <pre>{JSON.stringify(user, null, 2)}</pre>;
}
export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
// Create authenticated Supabase Client
const supabase = createServerSupabaseClient(ctx);
// Check if we have a session
const {
data: { session }
} = await supabase.auth.getSession();
if (!session)
return {
redirect: {
destination: '/',
permanent: false
}
};
return {
props: {
initialSession: session,
user: session.user
}
};
};
```
### `withMiddlewareAuth` deprecated!
#### Before
```tsx
import { withMiddlewareAuth } from '@supabase/auth-helpers-nextjs';
export const middleware = withMiddlewareAuth({
redirectTo: '/',
authGuard: {
isPermitted: async (user) => {
return user.email?.endsWith('@gmail.com') ?? false;
},
redirectTo: '/insufficient-permissions'
}
});
export const config = {
matcher: '/middleware-protected'
};
```
#### After
```tsx
import { createMiddlewareSupabaseClient } from '@supabase/auth-helpers-nextjs';
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export async function middleware(req: NextRequest) {
// We need to create a response and hand it to the supabase client to be able to modify the response headers.
const res = NextResponse.next();
// Create authenticated Supabase Client.
const supabase = createMiddlewareSupabaseClient({ req, res });
// Check if we have a session
const {
data: { session }
} = await supabase.auth.getSession();
// Check auth condition
if (session?.user.email?.endsWith('@gmail.com')) {
// Authentication successful, forward request to protected route.
return res;
}
// Auth condition not met, redirect to home page.
const redirectUrl = req.nextUrl.clone();
redirectUrl.pathname = '/';
redirectUrl.searchParams.set(`redirectedFrom`, req.nextUrl.pathname);
return NextResponse.redirect(redirectUrl);
}
export const config = {
matcher: '/middleware-protected'
};
```
## Migrating to `0.4.X` and supabase-js v2
- With the update to `supabase-js` v2 the `auth` API routes are no longer required, therefore you can go ahead and delete your `auth` directory under the `/pages/api/` directory. Please refer to the [v2 migration guide](https://supabase.com/docs/reference/javascript/upgrade-guide) for the full set of changes within supabase-js.
- The `/api/auth/logout` API route has been removed, please use the `signout` method instead:
```jsx
<button
onClick={async () => {
await supabaseClient.auth.signOut();
router.push('/');
}}
>
Logout
</button>
```
- The `supabaseClient` and `supabaseServerClient` have been removed in favor of the `createBrowserSupabaseClient` and `createServerSupabaseClient` methods. This is so that you can provide the CLI generated types to the client, e.g.
```tsx
// client-side
import type { Database } from 'types_db';
const [supabaseClient] = useState(() => createBrowserSupabaseClient<Database>());
// server-side API route
import type { NextApiRequest, NextApiResponse } from 'next';
import type { Database } from 'types_db';
export default async (req: NextApiRequest, res: NextApiResponse) => {
const supabaseServerClient = createServerSupabaseClient<Database>({
req,
res
});
const {
data: { user }
} = await supabaseServerClient.auth.getUser();
res.status(200).json({ name: user?.name ?? '' });
};
```
- The `UserProvider` has been replaced by the `SessionContextProvider`. Make sure to wrap your `pages/_app.js` componenent with the `SessionContextProvider`. Then, throughout your application you can use the `useSessionContext` hook to get the `session` and the `useSupabaseClient` hook to get an authenticated `supabaseClient`.
- The `useUser` hook now returns the `user` object or `null`.
- Usage with TypeScript: You can pass types that were [generated with the Supabase CLI](https://supabase.com/docs/reference/javascript/next/typescript-support#generating-types) to the Supabase Client to get enhanced type safety and auto completion:
```tsx
// Creating a new supabase client object:
import { Database } from '../db_types';
const [supabaseClient] = useState(() => createBrowserSupabaseClient<Database>());
```
```tsx
// Retrieving a supabase client object from the SessionContext:
import { useSupabaseClient } from '@supabase/auth-helpers-react';
import { Database } from '../db_types';
const supabaseClient = useSupabaseClient<Database>();
```
## Migrating from @supabase/supabase-auth-helpers to @supabase/auth-helpers
This is a step by step guide on migrating away from the `@supabase/supabase-auth-helpers` to the newly released `@supabase/auth-helpers`.
1. Install `@supabase/supabase-js`, `@supabase/auth-helpers-nextjs` and `@supabase/auth-helpers-react` libraries from npm.
2. Replace all imports of `@supabase/supabase-auth-helpers/nextjs` in your project with `@supabase/auth-helpers-nextjs`.
3. Replace all imports of `@supabase/supabase-auth-helpers/react` in your project with `@supabase/auth-helpers-react`.
4. Replace all instances of `withAuthRequired` in any of your NextJS pages with `withPageAuth`.
5. Replace all instances of `withAuthRequired` in any of your NextJS API endpoints with `withApiAuth`.
6. Uninstall `@supabase/supabase-auth-helpers`.

6
node_modules/@supabase/auth-helpers-nextjs/README.md generated vendored Normal file
View File

@@ -0,0 +1,6 @@
# @supabase/auth-helpers-nextjs (BETA)
This submodule provides convenience helpers for implementing user authentication in Next.js applications.
- [Documentation](https://supabase.com/docs/guides/auth/auth-helpers/nextjs)
- [Example](https://github.com/supabase/supabase/tree/master/examples/auth/nextjs)

View File

@@ -0,0 +1,94 @@
import * as _supabase_supabase_js from '@supabase/supabase-js';
import { SupabaseClient } from '@supabase/supabase-js';
export { Session, SupabaseClient, User } from '@supabase/supabase-js';
import { SupabaseClientOptionsWithoutAuth, CookieOptionsWithName } from '@supabase/auth-helpers-shared';
import { GenericSchema } from '@supabase/supabase-js/dist/module/lib/types';
import { GetServerSidePropsContext, NextApiRequest, NextApiResponse } from 'next';
import { NextRequest, NextResponse } from 'next/server';
import { cookies } from 'next/headers';
declare function createClientComponentClient<Database = any, SchemaName extends string & keyof Database = 'public' extends keyof Database ? 'public' : string & keyof Database, Schema extends GenericSchema = Database[SchemaName] extends GenericSchema ? Database[SchemaName] : any>({ supabaseUrl, supabaseKey, options, cookieOptions, isSingleton }?: {
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
isSingleton?: boolean;
}): SupabaseClient<Database, SchemaName, Schema>;
declare const createPagesBrowserClient: typeof createClientComponentClient;
declare function createPagesServerClient<Database = any, SchemaName extends string & keyof Database = 'public' extends keyof Database ? 'public' : string & keyof Database, Schema extends GenericSchema = Database[SchemaName] extends GenericSchema ? Database[SchemaName] : any>(context: GetServerSidePropsContext | {
req: NextApiRequest;
res: NextApiResponse;
}, { supabaseUrl, supabaseKey, options, cookieOptions }?: {
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
}): SupabaseClient<Database, SchemaName, Schema>;
declare function createMiddlewareClient<Database = any, SchemaName extends string & keyof Database = 'public' extends keyof Database ? 'public' : string & keyof Database, Schema extends GenericSchema = Database[SchemaName] extends GenericSchema ? Database[SchemaName] : any>(context: {
req: NextRequest;
res: NextResponse;
}, { supabaseUrl, supabaseKey, options, cookieOptions }?: {
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
}): SupabaseClient<Database, SchemaName, Schema>;
declare function createServerComponentClient<Database = any, SchemaName extends string & keyof Database = 'public' extends keyof Database ? 'public' : string & keyof Database, Schema extends GenericSchema = Database[SchemaName] extends GenericSchema ? Database[SchemaName] : any>(context: {
cookies: () => ReturnType<typeof cookies>;
}, { supabaseUrl, supabaseKey, options, cookieOptions }?: {
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
}): SupabaseClient<Database, SchemaName, Schema>;
declare function createRouteHandlerClient<Database = any, SchemaName extends string & keyof Database = 'public' extends keyof Database ? 'public' : string & keyof Database, Schema extends GenericSchema = Database[SchemaName] extends GenericSchema ? Database[SchemaName] : any>(context: {
cookies: () => ReturnType<typeof cookies>;
}, { supabaseUrl, supabaseKey, options, cookieOptions }?: {
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
}): SupabaseClient<Database, SchemaName, Schema>;
declare const createServerActionClient: typeof createRouteHandlerClient;
/**
* @deprecated utilize the `createPagesBrowserClient` function instead
*/
declare function createBrowserSupabaseClient<Database = any, SchemaName extends string & keyof Database = 'public' extends keyof Database ? 'public' : string & keyof Database, Schema extends GenericSchema = Database[SchemaName] extends GenericSchema ? Database[SchemaName] : any>({ supabaseUrl, supabaseKey, options, cookieOptions }?: {
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
}): _supabase_supabase_js.SupabaseClient<Database, SchemaName, Schema>;
/**
* @deprecated utilize the `createPagesServerClient` function instead
*/
declare function createServerSupabaseClient<Database = any, SchemaName extends string & keyof Database = 'public' extends keyof Database ? 'public' : string & keyof Database, Schema extends GenericSchema = Database[SchemaName] extends GenericSchema ? Database[SchemaName] : any>(context: GetServerSidePropsContext | {
req: NextApiRequest;
res: NextApiResponse;
}, { supabaseUrl, supabaseKey, options, cookieOptions }?: {
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
}): _supabase_supabase_js.SupabaseClient<Database, SchemaName, Schema>;
/**
* @deprecated utilize the `createMiddlewareClient` function instead
*/
declare function createMiddlewareSupabaseClient<Database = any, SchemaName extends string & keyof Database = 'public' extends keyof Database ? 'public' : string & keyof Database, Schema extends GenericSchema = Database[SchemaName] extends GenericSchema ? Database[SchemaName] : any>(context: {
req: NextRequest;
res: NextResponse;
}, { supabaseUrl, supabaseKey, options, cookieOptions }?: {
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
}): _supabase_supabase_js.SupabaseClient<Database, SchemaName, Schema>;
export { createBrowserSupabaseClient, createClientComponentClient, createMiddlewareClient, createMiddlewareSupabaseClient, createPagesBrowserClient, createPagesServerClient, createRouteHandlerClient, createServerActionClient, createServerComponentClient, createServerSupabaseClient };

View File

@@ -0,0 +1,373 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
createBrowserSupabaseClient: () => createBrowserSupabaseClient,
createClientComponentClient: () => createClientComponentClient,
createMiddlewareClient: () => createMiddlewareClient,
createMiddlewareSupabaseClient: () => createMiddlewareSupabaseClient,
createPagesBrowserClient: () => createPagesBrowserClient,
createPagesServerClient: () => createPagesServerClient,
createRouteHandlerClient: () => createRouteHandlerClient,
createServerActionClient: () => createServerActionClient,
createServerComponentClient: () => createServerComponentClient,
createServerSupabaseClient: () => createServerSupabaseClient
});
module.exports = __toCommonJS(src_exports);
// src/clientComponentClient.ts
var import_auth_helpers_shared = require("@supabase/auth-helpers-shared");
var supabase;
function createClientComponentClient({
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
options,
cookieOptions,
isSingleton = true
} = {}) {
if (!supabaseUrl || !supabaseKey) {
throw new Error(
"either NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY env variables or supabaseUrl and supabaseKey are required!"
);
}
const createNewClient = () => {
var _a;
return (0, import_auth_helpers_shared.createSupabaseClient)(supabaseUrl, supabaseKey, {
...options,
global: {
...options == null ? void 0 : options.global,
headers: {
...(_a = options == null ? void 0 : options.global) == null ? void 0 : _a.headers,
"X-Client-Info": `${"@supabase/auth-helpers-nextjs"}@${"0.10.0"}`
}
},
auth: {
storage: new import_auth_helpers_shared.BrowserCookieAuthStorageAdapter(cookieOptions)
}
});
};
if (isSingleton) {
const _supabase = supabase ?? createNewClient();
if (typeof window === "undefined")
return _supabase;
if (!supabase)
supabase = _supabase;
return supabase;
}
return createNewClient();
}
// src/pagesBrowserClient.ts
var createPagesBrowserClient = createClientComponentClient;
// src/pagesServerClient.ts
var import_auth_helpers_shared2 = require("@supabase/auth-helpers-shared");
var import_set_cookie_parser = require("set-cookie-parser");
var NextServerAuthStorageAdapter = class extends import_auth_helpers_shared2.CookieAuthStorageAdapter {
constructor(context, cookieOptions) {
super(cookieOptions);
this.context = context;
}
getCookie(name) {
var _a, _b, _c;
const setCookie = (0, import_set_cookie_parser.splitCookiesString)(
((_b = (_a = this.context.res) == null ? void 0 : _a.getHeader("set-cookie")) == null ? void 0 : _b.toString()) ?? ""
).map((c) => (0, import_auth_helpers_shared2.parseCookies)(c)[name]).find((c) => !!c);
const value = setCookie ?? ((_c = this.context.req) == null ? void 0 : _c.cookies[name]);
return value;
}
setCookie(name, value) {
this._setCookie(name, value);
}
deleteCookie(name) {
this._setCookie(name, "", {
maxAge: 0
});
}
_setCookie(name, value, options) {
var _a;
const setCookies = (0, import_set_cookie_parser.splitCookiesString)(
((_a = this.context.res.getHeader("set-cookie")) == null ? void 0 : _a.toString()) ?? ""
).filter((c) => !(name in (0, import_auth_helpers_shared2.parseCookies)(c)));
const cookieStr = (0, import_auth_helpers_shared2.serializeCookie)(name, value, {
...this.cookieOptions,
...options,
// Allow supabase-js on the client to read the cookie as well
httpOnly: false
});
this.context.res.setHeader("set-cookie", [...setCookies, cookieStr]);
}
};
function createPagesServerClient(context, {
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
options,
cookieOptions
} = {}) {
var _a;
if (!supabaseUrl || !supabaseKey) {
throw new Error(
"either NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY env variables or supabaseUrl and supabaseKey are required!"
);
}
return (0, import_auth_helpers_shared2.createSupabaseClient)(supabaseUrl, supabaseKey, {
...options,
global: {
...options == null ? void 0 : options.global,
headers: {
...(_a = options == null ? void 0 : options.global) == null ? void 0 : _a.headers,
"X-Client-Info": `${"@supabase/auth-helpers-nextjs"}@${"0.10.0"}`
}
},
auth: {
storage: new NextServerAuthStorageAdapter(context, cookieOptions)
}
});
}
// src/middlewareClient.ts
var import_auth_helpers_shared3 = require("@supabase/auth-helpers-shared");
var import_set_cookie_parser2 = require("set-cookie-parser");
var NextMiddlewareAuthStorageAdapter = class extends import_auth_helpers_shared3.CookieAuthStorageAdapter {
constructor(context, cookieOptions) {
super(cookieOptions);
this.context = context;
}
getCookie(name) {
var _a;
const setCookie = (0, import_set_cookie_parser2.splitCookiesString)(
((_a = this.context.res.headers.get("set-cookie")) == null ? void 0 : _a.toString()) ?? ""
).map((c) => (0, import_auth_helpers_shared3.parseCookies)(c)[name]).find((c) => !!c);
if (setCookie) {
return setCookie;
}
const cookies = (0, import_auth_helpers_shared3.parseCookies)(this.context.req.headers.get("cookie") ?? "");
return cookies[name];
}
setCookie(name, value) {
this._setCookie(name, value);
}
deleteCookie(name) {
this._setCookie(name, "", {
maxAge: 0
});
}
_setCookie(name, value, options) {
const newSessionStr = (0, import_auth_helpers_shared3.serializeCookie)(name, value, {
...this.cookieOptions,
...options,
// Allow supabase-js on the client to read the cookie as well
httpOnly: false
});
if (this.context.res.headers) {
this.context.res.headers.append("set-cookie", newSessionStr);
}
}
};
function createMiddlewareClient(context, {
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
options,
cookieOptions
} = {}) {
var _a;
if (!supabaseUrl || !supabaseKey) {
throw new Error(
"either NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY env variables or supabaseUrl and supabaseKey are required!"
);
}
return (0, import_auth_helpers_shared3.createSupabaseClient)(supabaseUrl, supabaseKey, {
...options,
global: {
...options == null ? void 0 : options.global,
headers: {
...(_a = options == null ? void 0 : options.global) == null ? void 0 : _a.headers,
"X-Client-Info": `${"@supabase/auth-helpers-nextjs"}@${"0.10.0"}`
}
},
auth: {
storage: new NextMiddlewareAuthStorageAdapter(context, cookieOptions)
}
});
}
// src/serverComponentClient.ts
var import_auth_helpers_shared4 = require("@supabase/auth-helpers-shared");
var NextServerComponentAuthStorageAdapter = class extends import_auth_helpers_shared4.CookieAuthStorageAdapter {
constructor(context, cookieOptions) {
super(cookieOptions);
this.context = context;
this.isServer = true;
}
getCookie(name) {
var _a;
const nextCookies = this.context.cookies();
return (_a = nextCookies.get(name)) == null ? void 0 : _a.value;
}
setCookie(name, value) {
}
deleteCookie(name) {
}
};
function createServerComponentClient(context, {
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
options,
cookieOptions
} = {}) {
var _a;
if (!supabaseUrl || !supabaseKey) {
throw new Error(
"either NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY env variables or supabaseUrl and supabaseKey are required!"
);
}
return (0, import_auth_helpers_shared4.createSupabaseClient)(supabaseUrl, supabaseKey, {
...options,
global: {
...options == null ? void 0 : options.global,
headers: {
...(_a = options == null ? void 0 : options.global) == null ? void 0 : _a.headers,
"X-Client-Info": `${"@supabase/auth-helpers-nextjs"}@${"0.10.0"}`
}
},
auth: {
storage: new NextServerComponentAuthStorageAdapter(context, cookieOptions)
}
});
}
// src/routeHandlerClient.ts
var import_auth_helpers_shared5 = require("@supabase/auth-helpers-shared");
var NextRouteHandlerAuthStorageAdapter = class extends import_auth_helpers_shared5.CookieAuthStorageAdapter {
constructor(context, cookieOptions) {
super(cookieOptions);
this.context = context;
}
getCookie(name) {
var _a;
const nextCookies = this.context.cookies();
return (_a = nextCookies.get(name)) == null ? void 0 : _a.value;
}
setCookie(name, value) {
const nextCookies = this.context.cookies();
nextCookies.set(name, value, this.cookieOptions);
}
deleteCookie(name) {
const nextCookies = this.context.cookies();
nextCookies.set(name, "", {
...this.cookieOptions,
maxAge: 0
});
}
};
function createRouteHandlerClient(context, {
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
options,
cookieOptions
} = {}) {
var _a;
if (!supabaseUrl || !supabaseKey) {
throw new Error(
"either NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY env variables or supabaseUrl and supabaseKey are required!"
);
}
return (0, import_auth_helpers_shared5.createSupabaseClient)(supabaseUrl, supabaseKey, {
...options,
global: {
...options == null ? void 0 : options.global,
headers: {
...(_a = options == null ? void 0 : options.global) == null ? void 0 : _a.headers,
"X-Client-Info": `${"@supabase/auth-helpers-nextjs"}@${"0.10.0"}`
}
},
auth: {
storage: new NextRouteHandlerAuthStorageAdapter(context, cookieOptions)
}
});
}
// src/serverActionClient.ts
var createServerActionClient = createRouteHandlerClient;
// src/deprecated.ts
function createBrowserSupabaseClient({
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
options,
cookieOptions
} = {}) {
console.warn(
"Please utilize the `createPagesBrowserClient` function instead of the deprecated `createBrowserSupabaseClient` function. Learn more: https://supabase.com/docs/guides/auth/auth-helpers/nextjs-pages"
);
return createPagesBrowserClient({
supabaseUrl,
supabaseKey,
options,
cookieOptions
});
}
function createServerSupabaseClient(context, {
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
options,
cookieOptions
} = {}) {
console.warn(
"Please utilize the `createPagesServerClient` function instead of the deprecated `createServerSupabaseClient` function. Learn more: https://supabase.com/docs/guides/auth/auth-helpers/nextjs-pages"
);
return createPagesServerClient(context, {
supabaseUrl,
supabaseKey,
options,
cookieOptions
});
}
function createMiddlewareSupabaseClient(context, {
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
options,
cookieOptions
} = {}) {
console.warn(
"Please utilize the `createMiddlewareClient` function instead of the deprecated `createMiddlewareSupabaseClient` function. Learn more: https://supabase.com/docs/guides/auth/auth-helpers/nextjs#middleware"
);
return createMiddlewareClient(context, {
supabaseUrl,
supabaseKey,
options,
cookieOptions
});
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createBrowserSupabaseClient,
createClientComponentClient,
createMiddlewareClient,
createMiddlewareSupabaseClient,
createPagesBrowserClient,
createPagesServerClient,
createRouteHandlerClient,
createServerActionClient,
createServerComponentClient,
createServerSupabaseClient
});
//# sourceMappingURL=index.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,49 @@
{
"name": "@supabase/auth-helpers-nextjs",
"version": "0.10.0",
"description": "A collection of framework specific Auth utilities for working with Supabase.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/supabase/auth-helpers.git"
},
"keywords": [
"Supabase",
"Auth",
"Nextjs"
],
"author": "Supabase",
"license": "MIT",
"bugs": {
"url": "https://github.com/supabase/auth-helpers/issues"
},
"homepage": "https://github.com/supabase/auth-helpers/tree/main/packages/nextjs#readme",
"devDependencies": {
"@supabase/supabase-js": "2.42.0",
"@types/set-cookie-parser": "^2.4.3",
"next": "^13.5.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rimraf": "^4.4.1",
"tslib": "^2.6.2",
"tsup": "^6.7.0",
"config": "0.1.0",
"tsconfig": "0.1.1"
},
"dependencies": {
"set-cookie-parser": "^2.6.0",
"@supabase/auth-helpers-shared": "0.7.0"
},
"peerDependencies": {
"@supabase/supabase-js": "^2.39.8"
},
"scripts": {
"lint": "tsc",
"build": "tsup",
"clean:all": "rimraf dist node_modules"
}
}

View File

@@ -0,0 +1,3 @@
// these variables are defined by tsup
declare const PACKAGE_NAME: string;
declare const PACKAGE_VERSION: string;

View File

@@ -0,0 +1,71 @@
import {
BrowserCookieAuthStorageAdapter,
CookieOptionsWithName,
SupabaseClientOptionsWithoutAuth,
createSupabaseClient
} from '@supabase/auth-helpers-shared';
import type { SupabaseClient } from '@supabase/supabase-js';
import type { GenericSchema } from '@supabase/supabase-js/dist/module/lib/types';
// can't type this properly as `Database`, `SchemaName` and `Schema` are only available within `createClientComponentClient` function
let supabase: any;
export function createClientComponentClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any
>({
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
options,
cookieOptions,
isSingleton = true
}: {
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
isSingleton?: boolean;
} = {}): SupabaseClient<Database, SchemaName, Schema> {
if (!supabaseUrl || !supabaseKey) {
throw new Error(
'either NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY env variables or supabaseUrl and supabaseKey are required!'
);
}
const createNewClient = () =>
createSupabaseClient<Database, SchemaName, Schema>(supabaseUrl, supabaseKey, {
...options,
global: {
...options?.global,
headers: {
...options?.global?.headers,
'X-Client-Info': `${PACKAGE_NAME}@${PACKAGE_VERSION}`
}
},
auth: {
storage: new BrowserCookieAuthStorageAdapter(cookieOptions)
}
});
if (isSingleton) {
// The `Singleton` pattern is the default to simplify the instantiation
// of a Supabase client across Client Components.
const _supabase = supabase ?? createNewClient();
// For SSG and SSR always create a new Supabase client
if (typeof window === 'undefined') return _supabase;
// Create the Supabase client once in the client
if (!supabase) supabase = _supabase;
return supabase;
}
// This allows for multiple Supabase clients, which may be required when using
// multiple schemas. The user will be responsible for ensuring a single
// instance of Supabase is used across Client Components, for each schema.
return createNewClient();
}

View File

@@ -0,0 +1,239 @@
import {
SupabaseClientOptionsWithoutAuth,
CookieOptionsWithName
} from '@supabase/auth-helpers-shared';
import { NextResponse } from 'next/server';
import { createPagesBrowserClient } from './pagesBrowserClient';
import { createPagesServerClient } from './pagesServerClient';
import { createMiddlewareClient } from './middlewareClient';
import { createClientComponentClient } from './clientComponentClient';
import { createServerComponentClient } from './serverComponentClient';
import { createRouteHandlerClient } from './routeHandlerClient';
import { headers, cookies } from 'next/headers';
import type { GetServerSidePropsContext, NextApiRequest, NextApiResponse } from 'next';
import type { NextRequest } from 'next/server';
import type { GenericSchema } from '@supabase/supabase-js/dist/module/lib/types';
/**
* @deprecated utilize the `createPagesBrowserClient` function instead
*/
export function createBrowserSupabaseClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any
>({
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
options,
cookieOptions
}: {
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
} = {}) {
console.warn(
'Please utilize the `createPagesBrowserClient` function instead of the deprecated `createBrowserSupabaseClient` function. Learn more: https://supabase.com/docs/guides/auth/auth-helpers/nextjs-pages'
);
return createPagesBrowserClient<Database, SchemaName, Schema>({
supabaseUrl,
supabaseKey,
options,
cookieOptions
});
}
/**
* @deprecated utilize the `createPagesServerClient` function instead
*/
export function createServerSupabaseClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any
>(
context: GetServerSidePropsContext | { req: NextApiRequest; res: NextApiResponse },
{
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
options,
cookieOptions
}: {
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
} = {}
) {
console.warn(
'Please utilize the `createPagesServerClient` function instead of the deprecated `createServerSupabaseClient` function. Learn more: https://supabase.com/docs/guides/auth/auth-helpers/nextjs-pages'
);
return createPagesServerClient<Database, SchemaName, Schema>(context, {
supabaseUrl,
supabaseKey,
options,
cookieOptions
});
}
/**
* @deprecated utilize the `createMiddlewareClient` function instead
*/
export function createMiddlewareSupabaseClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any
>(
context: { req: NextRequest; res: NextResponse },
{
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
options,
cookieOptions
}: {
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
} = {}
) {
console.warn(
'Please utilize the `createMiddlewareClient` function instead of the deprecated `createMiddlewareSupabaseClient` function. Learn more: https://supabase.com/docs/guides/auth/auth-helpers/nextjs#middleware'
);
return createMiddlewareClient<Database, SchemaName, Schema>(context, {
supabaseUrl,
supabaseKey,
options,
cookieOptions
});
}
/**
* @deprecated utilize the `createClientComponentClient` function instead
*/
export function createClientComponentSupabaseClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any
>({
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
options,
cookieOptions
}: {
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
} = {}) {
console.warn(
'Please utilize the `createClientComponentClient` function instead of the deprecated `createClientComponentSupabaseClient` function. Learn more: https://supabase.com/docs/guides/auth/auth-helpers/nextjs#client-component'
);
return createClientComponentClient<Database, SchemaName, Schema>({
supabaseUrl,
supabaseKey,
options,
cookieOptions
});
}
/**
* @deprecated utilize the `createServerComponentClient` function instead
*/
export function createServerComponentSupabaseClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any
>(
context: {
headers: () => ReturnType<typeof headers>;
cookies: () => ReturnType<typeof cookies>;
},
{
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
options,
cookieOptions
}: {
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
} = {}
) {
console.warn(
'Please utilize the `createServerComponentClient` function instead of the deprecated `createServerComponentSupabaseClient` function. Additionally, this function no longer requires the `headers` function as a parameter. Learn more: https://supabase.com/docs/guides/auth/auth-helpers/nextjs#server-component'
);
return createServerComponentClient<Database, SchemaName, Schema>(
{ cookies: context.cookies },
{
supabaseUrl,
supabaseKey,
options,
cookieOptions
}
);
}
/**
* @deprecated utilize the `createRouteHandlerClient` function instead
*/
export function createRouteHandlerSupabaseClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any
>(
context: { headers: () => ReturnType<typeof headers>; cookies: () => ReturnType<typeof cookies> },
{
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
options,
cookieOptions
}: {
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
} = {}
) {
console.warn(
'Please utilize the `createRouteHandlerClient` function instead of the deprecated `createRouteHandlerSupabaseClient` function. Additionally, this function no longer requires the `headers` function as a parameter. Learn more: https://supabase.com/docs/guides/auth/auth-helpers/nextjs#route-handler'
);
return createRouteHandlerClient<Database, SchemaName, Schema>(
{ cookies: context.cookies },
{
supabaseUrl,
supabaseKey,
options,
cookieOptions
}
);
}

View File

@@ -0,0 +1,17 @@
// Types
export type { Session, User, SupabaseClient } from '@supabase/supabase-js';
export { createPagesBrowserClient } from './pagesBrowserClient';
export { createPagesServerClient } from './pagesServerClient';
export { createMiddlewareClient } from './middlewareClient';
export { createClientComponentClient } from './clientComponentClient';
export { createServerComponentClient } from './serverComponentClient';
export { createRouteHandlerClient } from './routeHandlerClient';
export { createServerActionClient } from './serverActionClient';
// Deprecated Functions
export {
createBrowserSupabaseClient,
createServerSupabaseClient,
createMiddlewareSupabaseClient
} from './deprecated';

View File

@@ -0,0 +1,104 @@
import {
CookieAuthStorageAdapter,
CookieOptions,
CookieOptionsWithName,
createSupabaseClient,
DefaultCookieOptions,
parseCookies,
serializeCookie,
SupabaseClientOptionsWithoutAuth
} from '@supabase/auth-helpers-shared';
import { NextResponse } from 'next/server';
import { splitCookiesString } from 'set-cookie-parser';
import type { NextRequest } from 'next/server';
import type { GenericSchema } from '@supabase/supabase-js/dist/module/lib/types';
import type { SupabaseClient } from '@supabase/supabase-js';
class NextMiddlewareAuthStorageAdapter extends CookieAuthStorageAdapter {
constructor(
private readonly context: { req: NextRequest; res: NextResponse },
cookieOptions?: CookieOptions
) {
super(cookieOptions);
}
protected getCookie(name: string): string | null | undefined {
const setCookie = splitCookiesString(
this.context.res.headers.get('set-cookie')?.toString() ?? ''
)
.map((c) => parseCookies(c)[name])
.find((c) => !!c);
if (setCookie) {
return setCookie;
}
const cookies = parseCookies(this.context.req.headers.get('cookie') ?? '');
return cookies[name];
}
protected setCookie(name: string, value: string): void {
this._setCookie(name, value);
}
protected deleteCookie(name: string): void {
this._setCookie(name, '', {
maxAge: 0
});
}
private _setCookie(name: string, value: string, options?: DefaultCookieOptions) {
const newSessionStr = serializeCookie(name, value, {
...this.cookieOptions,
...options,
// Allow supabase-js on the client to read the cookie as well
httpOnly: false
});
if (this.context.res.headers) {
this.context.res.headers.append('set-cookie', newSessionStr);
}
}
}
export function createMiddlewareClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any
>(
context: { req: NextRequest; res: NextResponse },
{
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
options,
cookieOptions
}: {
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
} = {}
): SupabaseClient<Database, SchemaName, Schema> {
if (!supabaseUrl || !supabaseKey) {
throw new Error(
'either NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY env variables or supabaseUrl and supabaseKey are required!'
);
}
return createSupabaseClient<Database, SchemaName, Schema>(supabaseUrl, supabaseKey, {
...options,
global: {
...options?.global,
headers: {
...options?.global?.headers,
'X-Client-Info': `${PACKAGE_NAME}@${PACKAGE_VERSION}`
}
},
auth: {
storage: new NextMiddlewareAuthStorageAdapter(context, cookieOptions)
}
});
}

View File

@@ -0,0 +1,3 @@
import { createClientComponentClient } from './clientComponentClient';
export const createPagesBrowserClient = createClientComponentClient;

View File

@@ -0,0 +1,103 @@
import {
CookieAuthStorageAdapter,
CookieOptions,
CookieOptionsWithName,
createSupabaseClient,
DefaultCookieOptions,
parseCookies,
serializeCookie,
SupabaseClientOptionsWithoutAuth
} from '@supabase/auth-helpers-shared';
import { GetServerSidePropsContext, NextApiRequest, NextApiResponse } from 'next';
import { splitCookiesString } from 'set-cookie-parser';
import type { GenericSchema } from '@supabase/supabase-js/dist/module/lib/types';
import type { SupabaseClient } from '@supabase/supabase-js';
class NextServerAuthStorageAdapter extends CookieAuthStorageAdapter {
constructor(
private readonly context:
| GetServerSidePropsContext
| { req: NextApiRequest; res: NextApiResponse },
cookieOptions?: CookieOptions
) {
super(cookieOptions);
}
protected getCookie(name: string): string | null | undefined {
const setCookie = splitCookiesString(
this.context.res?.getHeader('set-cookie')?.toString() ?? ''
)
.map((c) => parseCookies(c)[name])
.find((c) => !!c);
const value = setCookie ?? this.context.req?.cookies[name];
return value;
}
protected setCookie(name: string, value: string): void {
this._setCookie(name, value);
}
protected deleteCookie(name: string): void {
this._setCookie(name, '', {
maxAge: 0
});
}
private _setCookie(name: string, value: string, options?: DefaultCookieOptions) {
const setCookies = splitCookiesString(
this.context.res.getHeader('set-cookie')?.toString() ?? ''
).filter((c) => !(name in parseCookies(c)));
const cookieStr = serializeCookie(name, value, {
...this.cookieOptions,
...options,
// Allow supabase-js on the client to read the cookie as well
httpOnly: false
});
this.context.res.setHeader('set-cookie', [...setCookies, cookieStr]);
}
}
export function createPagesServerClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any
>(
context: GetServerSidePropsContext | { req: NextApiRequest; res: NextApiResponse },
{
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
options,
cookieOptions
}: {
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
} = {}
): SupabaseClient<Database, SchemaName, Schema> {
if (!supabaseUrl || !supabaseKey) {
throw new Error(
'either NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY env variables or supabaseUrl and supabaseKey are required!'
);
}
return createSupabaseClient<Database, SchemaName, Schema>(supabaseUrl, supabaseKey, {
...options,
global: {
...options?.global,
headers: {
...options?.global?.headers,
'X-Client-Info': `${PACKAGE_NAME}@${PACKAGE_VERSION}`
}
},
auth: {
storage: new NextServerAuthStorageAdapter(context, cookieOptions)
}
});
}

View File

@@ -0,0 +1,83 @@
import {
CookieAuthStorageAdapter,
CookieOptions,
CookieOptionsWithName,
SupabaseClientOptionsWithoutAuth,
createSupabaseClient
} from '@supabase/auth-helpers-shared';
import { cookies } from 'next/headers';
import type { GenericSchema } from '@supabase/supabase-js/dist/module/lib/types';
import type { SupabaseClient } from '@supabase/supabase-js';
class NextRouteHandlerAuthStorageAdapter extends CookieAuthStorageAdapter {
constructor(
private readonly context: {
cookies: () => ReturnType<typeof cookies>;
},
cookieOptions?: CookieOptions
) {
super(cookieOptions);
}
protected getCookie(name: string): string | null | undefined {
const nextCookies = this.context.cookies();
return nextCookies.get(name)?.value;
}
protected setCookie(name: string, value: string): void {
const nextCookies = this.context.cookies();
nextCookies.set(name, value, this.cookieOptions);
}
protected deleteCookie(name: string): void {
const nextCookies = this.context.cookies();
nextCookies.set(name, '', {
...this.cookieOptions,
maxAge: 0
});
}
}
export function createRouteHandlerClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any
>(
context: {
cookies: () => ReturnType<typeof cookies>;
},
{
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
options,
cookieOptions
}: {
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
} = {}
): SupabaseClient<Database, SchemaName, Schema> {
if (!supabaseUrl || !supabaseKey) {
throw new Error(
'either NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY env variables or supabaseUrl and supabaseKey are required!'
);
}
return createSupabaseClient<Database, SchemaName, Schema>(supabaseUrl, supabaseKey, {
...options,
global: {
...options?.global,
headers: {
...options?.global?.headers,
'X-Client-Info': `${PACKAGE_NAME}@${PACKAGE_VERSION}`
}
},
auth: {
storage: new NextRouteHandlerAuthStorageAdapter(context, cookieOptions)
}
});
}

View File

@@ -0,0 +1,3 @@
import { createRouteHandlerClient } from './routeHandlerClient';
export const createServerActionClient = createRouteHandlerClient;

View File

@@ -0,0 +1,82 @@
import {
CookieAuthStorageAdapter,
CookieOptions,
CookieOptionsWithName,
SupabaseClientOptionsWithoutAuth,
createSupabaseClient
} from '@supabase/auth-helpers-shared';
import { cookies } from 'next/headers';
import type { SupabaseClient } from '@supabase/supabase-js';
import type { GenericSchema } from '@supabase/supabase-js/dist/module/lib/types';
class NextServerComponentAuthStorageAdapter extends CookieAuthStorageAdapter {
readonly isServer = true;
constructor(
private readonly context: {
cookies: () => ReturnType<typeof cookies>;
},
cookieOptions?: CookieOptions
) {
super(cookieOptions);
}
protected getCookie(name: string): string | null | undefined {
const nextCookies = this.context.cookies();
return nextCookies.get(name)?.value;
}
protected setCookie(name: string, value: string): void {
// Server Components cannot set cookies. Must use Middleware, Server Action or Route Handler
// https://github.com/vercel/next.js/discussions/41745#discussioncomment-5198848
}
protected deleteCookie(name: string): void {
// Server Components cannot set cookies. Must use Middleware, Server Action or Route Handler
// https://github.com/vercel/next.js/discussions/41745#discussioncomment-5198848
}
}
export function createServerComponentClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database,
Schema extends GenericSchema = Database[SchemaName] extends GenericSchema
? Database[SchemaName]
: any
>(
context: {
cookies: () => ReturnType<typeof cookies>;
},
{
supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
options,
cookieOptions
}: {
supabaseUrl?: string;
supabaseKey?: string;
options?: SupabaseClientOptionsWithoutAuth<SchemaName>;
cookieOptions?: CookieOptionsWithName;
} = {}
): SupabaseClient<Database, SchemaName, Schema> {
if (!supabaseUrl || !supabaseKey) {
throw new Error(
'either NEXT_PUBLIC_SUPABASE_URL and NEXT_PUBLIC_SUPABASE_ANON_KEY env variables or supabaseUrl and supabaseKey are required!'
);
}
return createSupabaseClient<Database, SchemaName, Schema>(supabaseUrl, supabaseKey, {
...options,
global: {
...options?.global,
headers: {
...options?.global?.headers,
'X-Client-Info': `${PACKAGE_NAME}@${PACKAGE_VERSION}`
}
},
auth: {
storage: new NextServerComponentAuthStorageAdapter(context, cookieOptions)
}
});
}

View File

@@ -0,0 +1,9 @@
{
"extends": "tsconfig/base.json",
"compilerOptions": {
"incremental": false,
"importHelpers": true
},
"include": ["src"],
"exclude": ["node_modules"]
}

View File

@@ -0,0 +1,20 @@
import type { Options } from 'tsup';
import pkg from './package.json';
export const tsup: Options = {
dts: true,
entryPoints: ['src/index.ts'],
external: ['next', 'react', /^@supabase\//],
format: ['cjs'],
// inject: ['src/react-shim.js'],
// ! .cjs/.mjs doesn't work with Angular's webpack4 config by default!
legacyOutput: false,
sourcemap: true,
splitting: false,
bundle: true,
clean: true,
define: {
PACKAGE_NAME: JSON.stringify(pkg.name),
PACKAGE_VERSION: JSON.stringify(pkg.version)
}
};