Skip to content

Commit

Permalink
Add handleAuth function to export all hooks as an array
Browse files Browse the repository at this point in the history
  • Loading branch information
silentworks committed Jun 21, 2022
1 parent 588d329 commit 1c95004
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-beers-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@supabase/auth-helpers-sveltekit': patch
---

Add new handleAuth function to export all hooks as an array to destructure
7 changes: 2 additions & 5 deletions examples/sveltekit/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { handleUser, handleCallback } from '@supabase/auth-helpers-sveltekit';
import { handleAuth } from '@supabase/auth-helpers-sveltekit';
import type { GetSession, Handle } from '@sveltejs/kit';
import { sequence } from '@sveltejs/kit/hooks';

export const handle: Handle = sequence(
handleCallback({
cookieOptions: { lifetime: 1 * 365 * 24 * 60 * 60 }
}),
handleUser({
...handleAuth({
cookieOptions: { lifetime: 1 * 365 * 24 * 60 * 60 }
})
);
Expand Down
6 changes: 3 additions & 3 deletions packages/sveltekit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ export async function post() {
}
```

We need to add the `handleCallback()` and `handleUser()` hooks to your `hooks.ts` file.
We need to add the `handleAuth()` hook to your `hooks.ts` file.

```ts
// src/hooks.ts
import { handleUser, handleCallback } from '@supabase/auth-helpers-sveltekit';
import { handleAuth } from '@supabase/auth-helpers-sveltekit';
import type { GetSession, Handle } from '@sveltejs/kit';
import { sequence } from '@sveltejs/kit/hooks';

export const handle: Handle = sequence(handleCallback(), handleUser());
export const handle: Handle = sequence(...handleAuth(), handleUser());

export const getSession: GetSession = async (event) => {
const { user, accessToken, error } = event.locals;
Expand Down
22 changes: 22 additions & 0 deletions packages/sveltekit/src/handlers/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { COOKIE_OPTIONS, TOKEN_REFRESH_MARGIN, type CookieOptions } from "@supabase/auth-helpers-shared";
import { handleCallback } from "./callback";
import { handleLogout } from "./logout";
import { handleUser } from "./user";

export interface HandleAuthOptions {
cookieOptions?: CookieOptions;
logout?: { returnTo?: string };
tokenRefreshMargin?: number;
}

export const handleAuth = (options: HandleAuthOptions = {}) => {
const { logout } = options;
const cookieOptions = { ...COOKIE_OPTIONS, ...options.cookieOptions };
const tokenRefreshMargin =
options.tokenRefreshMargin ?? TOKEN_REFRESH_MARGIN;
return [
handleCallback({ cookieOptions }),
handleUser({ cookieOptions, tokenRefreshMargin }),
handleLogout({ cookieOptions, ...logout })
];
}
1 change: 1 addition & 0 deletions packages/sveltekit/src/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { handleCallback } from './callback';
export { handleUser } from './user';
export { handleLogout } from './logout';
export { handleAuth } from './auth';
2 changes: 1 addition & 1 deletion packages/sveltekit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export type { User, SupabaseClient } from '@supabase/supabase-js';

// Methods
export { handleCallback, handleUser, handleLogout } from './handlers/index';
export { handleCallback, handleUser, handleLogout, handleAuth } from './handlers/index';
export { default as getUserAndSaveTokens, getUser } from './utils/getUser';
export { default as withApiAuth } from './utils/withApiAuth';
export { default as withPageAuth } from './utils/withPageAuth';
Expand Down

0 comments on commit 1c95004

Please sign in to comment.