Skip to content

Commit

Permalink
chore: improve types. (#255)
Browse files Browse the repository at this point in the history
* chore: improve types.

* chore: types and changeset.
  • Loading branch information
thorwebdev authored Sep 20, 2022
1 parent a5f0d20 commit fe5c4a6
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 17 deletions.
7 changes: 7 additions & 0 deletions .changeset/sixty-kangaroos-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@supabase/auth-helpers-nextjs': patch
'@supabase/auth-helpers-react': patch
'@supabase/auth-helpers-shared': patch
---

chore: improve types.
11 changes: 8 additions & 3 deletions packages/nextjs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ import {
} from '@supabase/auth-helpers-shared';

// Types
export type { User } from '@supabase/supabase-js';
export type { Session, User, SupabaseClient } from '@supabase/supabase-js';

// Methods
export * from './middleware';
export { default as withPageAuth } from './utils/withPageAuth';
export { default as withApiAuth } from './utils/withApiAuth';
export { default as logger } from './utils/log';

export function createBrowserSupabaseClient({
export function createBrowserSupabaseClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database
>({
cookieOptions
}: {
cookieOptions?: CookieOptions;
Expand All @@ -26,7 +31,7 @@ export function createBrowserSupabaseClient({
);
}

return _createBrowserSupabaseClient({
return _createBrowserSupabaseClient<Database, SchemaName>({
supabaseUrl: process.env.NEXT_PUBLIC_SUPABASE_URL,
supabaseKey: process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
cookieOptions
Expand Down
6 changes: 3 additions & 3 deletions packages/nextjs/src/middleware/withMiddlewareAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
CookieOptions,
createServerSupabaseClient
} from '@supabase/auth-helpers-shared';
import { User } from '@supabase/supabase-js';
import { SupabaseClient, User } from '@supabase/supabase-js';

class NoPermissionError extends Error {
constructor(message: string) {
Expand All @@ -26,7 +26,7 @@ export interface withMiddlewareAuthOptions {
redirectTo?: string;
cookieOptions?: CookieOptions;
authGuard?: {
isPermitted: (user: User) => Promise<boolean>;
isPermitted: (user: User, supabase: SupabaseClient) => Promise<boolean>;
redirectTo: string;
};
}
Expand Down Expand Up @@ -77,7 +77,7 @@ export const withMiddlewareAuth: withMiddlewareAuth =
throw new Error('No auth session, redirecting');
} else if (
options.authGuard &&
!(await options.authGuard.isPermitted(session.user))
!(await options.authGuard.isPermitted(session.user, supabase))
) {
throw new NoPermissionError('User is not permitted, redirecting');
}
Expand Down
3 changes: 3 additions & 0 deletions packages/react/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
// Types
export type { Session, User, SupabaseClient } from '@supabase/supabase-js';

// Methods & Components
export * from './components/SessionContext';
17 changes: 11 additions & 6 deletions packages/shared/src/supabase-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@ import { parse, serialize } from 'cookie';
import { CookieOptions } from './types';
import { isBrowser } from './utils/helpers';

export function createBrowserSupabaseClient({
export function createBrowserSupabaseClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database
>({
supabaseUrl,
supabaseKey,
cookieOptions: {
name = 'supabase-auth-token',
domain,
path = '/',
sameSite = 'lax',
secure = false
secure,
maxAge = 1000 * 60 * 60 * 24 * 365
} = {}
}: {
supabaseUrl: string;
supabaseKey: string;
cookieOptions?: CookieOptions;
}) {
return createClient(supabaseUrl, supabaseKey, {
return createClient<Database, SchemaName>(supabaseUrl, supabaseKey, {
auth: {
storageKey: name,
storage: {
Expand All @@ -44,12 +50,11 @@ export function createBrowserSupabaseClient({
document.cookie = serialize(key, value, {
domain,
path,
maxAge: 1000 * 60 * 60 * 24 * 365,
maxAge,
// Allow supabase-js on the client to read the cookie as well
httpOnly: false,
sameSite,
// TODO: set this secure based on the browser location
secure
secure: secure ?? document.location?.protocol === 'https:'
});
},
removeItem(key: string) {
Expand Down
14 changes: 10 additions & 4 deletions packages/shared/src/supabase-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import { CookieOptions } from './types';
import { filterCookies, isSecureEnvironment } from './utils/cookies';
import { ensureArray } from './utils/helpers';

export function createServerSupabaseClient({
export function createServerSupabaseClient<
Database = any,
SchemaName extends string & keyof Database = 'public' extends keyof Database
? 'public'
: string & keyof Database
>({
supabaseUrl,
supabaseKey,
getRequestHeader,
Expand All @@ -15,7 +20,8 @@ export function createServerSupabaseClient({
domain,
path = '/',
sameSite = 'lax',
secure
secure,
maxAge = 1000 * 60 * 60 * 24 * 365
} = {}
}: {
supabaseUrl: string;
Expand Down Expand Up @@ -51,7 +57,7 @@ export function createServerSupabaseClient({
});
}

return createClient(supabaseUrl, supabaseKey, {
return createClient<Database, SchemaName>(supabaseUrl, supabaseKey, {
auth: {
detectSessionInUrl: false,
autoRefreshToken: false,
Expand Down Expand Up @@ -81,7 +87,7 @@ export function createServerSupabaseClient({
const newSessionStr = serialize(key, value, {
domain,
path,
maxAge: 1000 * 60 * 60 * 24 * 365,
maxAge,
// Allow supabase-js on the client to read the cookie as well
httpOnly: false,
sameSite,
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import type { CookieSerializeOptions } from 'cookie';

export type CookieOptions = { name?: string } & Pick<
CookieSerializeOptions,
'domain' | 'secure' | 'path' | 'sameSite'
'domain' | 'secure' | 'path' | 'sameSite' | 'maxAge'
>;

0 comments on commit fe5c4a6

Please sign in to comment.