Skip to content
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

docs: Update Supabase auth example to use @supabase/ssr #856

Merged
merged 3 commits into from
Feb 13, 2024

Conversation

flixlix
Copy link
Contributor

@flixlix flixlix commented Feb 11, 2024

created an example for usage with the new supabase ssr package. The middleware I created is working in development as well as in production. The matcher in middleware.ts Was adapted based on multiple metadata files that needed to be ignored.

Suggestions on changes are welcome.

image

see https://supabase.com/docs/guides/auth/auth-helpers/nextjs?language=ts for the recommendation from supabase.

Copy link

vercel bot commented Feb 11, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
next-intl-example-app-router ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 12, 2024 2:59pm

Copy link

vercel bot commented Feb 11, 2024

@flixlix is attempting to deploy a commit to the next-intl Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Owner

@amannn amannn left a comment

Choose a reason for hiding this comment

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

Thank you so much for proposing an update here! 🙌

docs/pages/docs/routing/middleware.mdx Outdated Show resolved Hide resolved
docs/pages/docs/routing/middleware.mdx Outdated Show resolved Hide resolved
docs/pages/docs/routing/middleware.mdx Outdated Show resolved Hide resolved
docs/pages/docs/routing/middleware.mdx Outdated Show resolved Hide resolved
import {createServerClient, type CookieOptions} from '@supabase/ssr';
import {type NextRequest, NextResponse} from 'next/server';

export const createClient = (request: NextRequest) => {
Copy link
Owner

Choose a reason for hiding this comment

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

Based on the Supabase guide, I think we should accept a response here, where we can can assign cookies on directly (or if we inline this function, just use the response from the closure).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Don't we generate our own response, which we use in the createIntlMiddleware function?

From the Supabase docs, I don't see the response argument
image

docs/pages/docs/routing/middleware.mdx Outdated Show resolved Hide resolved
docs/pages/docs/routing/middleware.mdx Outdated Show resolved Hide resolved
docs/pages/docs/routing/middleware.mdx Outdated Show resolved Hide resolved
docs/pages/docs/routing/middleware.mdx Outdated Show resolved Hide resolved
- Rely more on the guide from Supabase
- Don't recreate the `next-intl` middleware per request
- Don't wrap the Supabase client creation in a function and use `request` from the closure (as shown in their guide)
@amannn
Copy link
Owner

amannn commented Feb 12, 2024

Hey, I took the liberty to adapt the section a bit in a780a55. I tried to make it a bit more compact by relying more on the guide from Supabase (e.g. in regard to setting up env params).

Let me know if this looks good to you!

Copy link
Contributor Author

@flixlix flixlix left a comment

Choose a reason for hiding this comment

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

Otherwise lgtm


```tsx filename="middleware.ts"
import {type NextRequest} from 'next/server';
import {createServerClient, type CookieOptions} from '@supabase/ssr';
import createIntlMiddleware from 'next-intl/middleware';

const handleI18nRouting = createIntlMiddleware({
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why put this function outside the middleware, if is not used anywhere else? Just curious

Copy link
Owner

Choose a reason for hiding this comment

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

I'd say it's really fine in either place. Since the creation of the middleware doesn't depend on any request-based information, I figured we can move it outside. But inside is absolutely ok too!

Copy link
Owner

@amannn amannn left a comment

Choose a reason for hiding this comment

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

Thanks again for your help here! 🙌


```tsx filename="middleware.ts"
import {type NextRequest} from 'next/server';
import {createServerClient, type CookieOptions} from '@supabase/ssr';
import createIntlMiddleware from 'next-intl/middleware';

const handleI18nRouting = createIntlMiddleware({
Copy link
Owner

Choose a reason for hiding this comment

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

I'd say it's really fine in either place. Since the creation of the middleware doesn't depend on any request-based information, I figured we can move it outside. But inside is absolutely ok too!

@amannn amannn changed the title docs: 📝 use new @supabase/ssr instead of the deprecated @supasebase/auth-helpers-nextjs package docs: Update Supabase auth example to use @supabase/ssr Feb 13, 2024
@amannn amannn merged commit b973586 into amannn:main Feb 13, 2024
4 of 5 checks passed
@flixlix
Copy link
Contributor Author

flixlix commented Feb 13, 2024

Thank you for merging this PR! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants