-
-
Notifications
You must be signed in to change notification settings - Fork 38
doc: migrate supabase auth guide to v3 #518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,6 @@ position: 7 | |
| label: Recipes | ||
| collapsible: true | ||
| collapsed: true | ||
| link: | ||
| type: generated-index | ||
| title: Recipes | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
versioned_docs/version-3.x/recipe/auth-integration/supabase.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| --- | ||
| description: Integrating with Supabase Auth. | ||
| sidebar_position: 4 | ||
| sidebar_label: Supabase Auth | ||
| --- | ||
|
|
||
| # Integrating With Supabase Auth | ||
|
|
||
| [Supabase](https://supabase.com) is a comprehensive Backend-as-a-Service that offers database, authentication, and other services. | ||
|
|
||
| To get access policies to work, ZenStack needs to be connected to the authentication system to get the user's identity. If you use Supabase as your authentication provider, this document will guide you through integrating ZenStack with it. | ||
|
|
||
| ## Syncing Supabase Auth users to your database | ||
|
|
||
| :::info | ||
| This section is only relevant if you're also using Supabase's Database service as the underlying Postgres database of ZenStack. | ||
| ::: | ||
|
|
||
| Supabase Auth stores user data in a separate Postgres schema called "auth". Since that schema is managed by Supabase, it's good idea NOT to directly import it into ZModel and use it in your application. Instead, if you want to have a synchronized copy of the data, refer to [Supabase's user management documentation](https://supabase.com/docs/guides/auth/managing-user-data) for how to set up database triggers and keep your user table in sync with Supabase Auth. | ||
|
|
||
| ## Creating a user-bound ORM client | ||
|
|
||
| Supabase provides the `@supabase/ssr` package to help with server-side authentication. Please refer to [its documentation](https://supabase.com/docs/guides/auth/server-side) for full details. The following example shows how to do it in a Next.js application. | ||
|
|
||
| ```ts | ||
| import { createServerClient } from '@supabase/ssr' | ||
| import { cookies } from 'next/headers' | ||
| import { db } from '@/lib/db' // your ZenStackClient instance | ||
|
|
||
| // create a Supabase SSR client | ||
| async function createSupabaseSSRClient() { | ||
| const cookieStore = await cookies() | ||
| return createServerClient( | ||
| process.env.NEXT_PUBLIC_SUPABASE_URL!, | ||
| process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!, | ||
| { | ||
| cookies: { | ||
| getAll() { | ||
| return cookieStore.getAll() | ||
| }, | ||
| setAll(cookiesToSet) { | ||
| try { | ||
| cookiesToSet.forEach(({ name, value }) => cookieStore.set(name, value)) | ||
| } catch { | ||
| // The `setAll` method was called from a Server Component. | ||
| // This can be ignored if you have middleware refreshing | ||
| // user sessions. | ||
| } | ||
| }, | ||
| }, | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| // create a user-bound ORM client | ||
| async function getUserDb() { | ||
| const supabase = await createSupabaseSSRClient(); | ||
| const { data: { user } } = await supabase.auth.getUser() | ||
|
|
||
| // you can selectively include fields into the context object | ||
| // depending on what your access policies need | ||
| const contextUser = user ? { id: user.id } : undefined; | ||
| return db.$setAuth(contextUser); | ||
| } | ||
| ``` | ||
|
|
||
| :::warning | ||
| It may be tempting to call the `supabase.auth.getSession()` API to get the current user. However, the data returned is not validated by Supabase's service, so it must not be trusted. | ||
| ::: | ||
|
|
||
| You can then use this user-bound ORM client for CRUD operations governed by the access policies you defined in ZModel. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.