Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

docs(examples): add integrations-supabase #2058

Closed
wants to merge 3 commits into from
Closed

Conversation

atinux
Copy link
Member

@atinux atinux commented Nov 21, 2021

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)

πŸ“š Description

This example showcases how to use Supabase with Nuxt.

Features

  • useSupabase() composable
  • User sessions with SSR support
  • Handle auth changes (and removes the session)

@atinux atinux added the example label Nov 21, 2021
@netlify
Copy link

netlify bot commented Nov 21, 2021

βœ”οΈ Deploy Preview for nuxt3-docs canceled.

πŸ”¨ Explore the source changes: ac77c69

πŸ” Inspect the deploy log: https://app.netlify.com/sites/nuxt3-docs/deploys/61a4fa13e9ff33000865eb4f

@atinux atinux changed the title docs(example): Add Supabase integration example docs(examples): Add Supabase integration example Nov 21, 2021
export default defineNuxtConfig({
publicRuntimeConfig: {
supabase: {
url: process.env.SUPABASE_URL,
Copy link
Member

@pi0 pi0 Nov 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Key shall be same as env name to allow overriding un runtime (and top level)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure to understand, what feature does it bring?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overriding env variables at runtime. Current config basically adds env on build-time to bundle. Check this:

// Allow override from process.env and deserialize

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the purpose of changing these keys at runtime? Do you have an example?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is runtime config :) Changing config without need to rebuild

// Supabase needs to read from req.cookies
req.cookies = useCookies(req)
// Check authenticated user during SSR
user.value = (await auth.api.getUserByCookie(req)).user
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic can be handle on demand in composable without using a global plugin (unless global guard is the purpose)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well actually having it by default offers a lot of features directly (feature flags per user, redirects, etc).

But if Nuxt exposes a smarter way to cachify() the user to speed up the process that would be neat.

I need to time the time execution of the plugin.

Something that Nuxt itself should expose (through hooks) so we can display them in the devtools (performance tabs)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite sure to understand what you mean but yes we can cache promise within a composable/context :)

req.cookies = useCookies(req)
res.status = () => ({ json: () => ({}) })

supabase.auth.api.setAuthCookie(req, res)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this can also be handled in app namespace (ssr only) instead of adding an additional api route

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well actually when doing the login it needs to also call our server to store the cookie as secured. How to would want to handle it without calling back the server?

Or we need to have a saveSession() exposed from Nuxt to easily create HTTP only cookies for secured sessions experience from day 1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure we probaly need useSession. My main point is that it can be in a composable and yet running in server-only to set secure cookie. This reduces boilerplate and allows logic working with rest of ssr context.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to try the demo to understand, when using Supabase, you basically no need to create API routes.

So I don't see how you want to implement this without an API call to works.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same server logic just in composable :) Will make refactor

@pi0 pi0 marked this pull request as draft November 21, 2021 12:29
@pi0 pi0 changed the title docs(examples): Add Supabase integration example docs(examples): add integrations-supabase Nov 21, 2021

## Development

Start the development server on http://localhost:3000
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Start the development server on http://localhost:3000
Start the development server on http://localhost:3000:

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels weird to me to add the : at the end

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think @danielroe ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This convention can also potentially make formatting issues. If we do decide to add :, should use explicit markdown syntax for generated link or add a space after 3000


- `useSupabase()` composable
- User sessions with SSR support
- Handle auth changes (and removes the session)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest either of the following:

  • Handles auth changes (and removes the session)
  • Handling auth changes (and removing the session)

npx nuxi init nuxt3-app -t nuxt/framework/examples/integrations/supabase
```

Make sure to install the dependencies and setup the `.env`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Make sure to install the dependencies and setup the `.env`.
Make sure to install the dependencies and setup the `.env`:

@sduduzog
Copy link

This is so awesome. I've been trying to experiment with ways to try add SSR support to https://github.com/supabase-community/nuxt-supabase with no luck but I'll definitely take learnings here and work on a PR there.

@pi0 pi0 assigned atinux and pi0 Nov 22, 2021
@eMeRiKa13
Copy link

Thanks a lot for this code. It's very helpful.

Could you add an example of how to query Supabase in a page, (other to the authenticated user) please?

supabase.from("my_table").select('*').eq(...)

I'm trying to use the useSupabase() composable to do that but I can't make it work.

@pi0
Copy link
Member

pi0 commented Feb 3, 2022

Finally moving this to a Nuxt module. nuxt-modules/supabase#8

@pi0 pi0 closed this Feb 3, 2022
@pi0 pi0 deleted the example/supabase branch February 3, 2022 11:23
@danielroe danielroe added the 3.x label Jan 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants