Skip to content

Commit

Permalink
docs: Add Supabase Authentication example (#673 by @wscourge)
Browse files Browse the repository at this point in the history
Based on [this
answer](#422 (comment))
with a little improvements.
  • Loading branch information
wscourge authored Nov 28, 2023
1 parent a2d8cdd commit 97769a7
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion docs/pages/docs/routing/middleware.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Since the middleware is aware of all your domains, the domain will automatically

<details>
<summary>How is the best matching domain for a given locale detected?</summary>

The bestmatching domain is detected based on these priorities:

1. Stay on the current domain if the locale is supported here
Expand Down Expand Up @@ -431,6 +431,34 @@ export const config = {
};
```

### Example: Integrating with Supabase Authentication

[`@supabase/auth-helpers-nextjs`](https://supabase.com/docs/guides/auth/auth-helpers/nextjs?language=ts#managing-session-with-middleware) exports `createMiddlewareClient` that accepts a `req` and `res` object. We can pass the incoming `req` and the `res` created with `createIntlMiddleware` to it.

```tsx filename="middleware.ts"
import {createMiddlewareClient} from '@supabase/auth-helpers-nextjs';
import createIntlMiddleware from 'next-intl/middleware';
import type {NextRequest} from 'next/server';
import type {Database} from '@/lib/database.types';

export default async function middleware(req: NextRequest) {
const handleI18nRouting = createIntlMiddleware({
locales: ['en', 'de'],
defaultLocale: 'en'
});
const res = handleI18nRouting(req);

const supabase = createMiddlewareClient<Database>({ req, res })
await supabase.auth.getSession()

return res;
}

export const config = {
matcher: ['/', '/(de|en)/:path*']
};
```

### Example: Integrating with Auth.js (aka NextAuth.js) [#example-auth-js]

The Next.js middleware of [Auth.js](https://authjs.dev/) requires an integration with their control flow to be compatible with other middlewares. The [success callback](https://next-auth.js.org/configuration/nextjs#wrap-middleware) can be used to run the `next-intl` middleware on authorized pages. However, public pages need to be treated separately.
Expand Down

2 comments on commit 97769a7

@vercel
Copy link

@vercel vercel bot commented on 97769a7 Nov 28, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on 97769a7 Nov 28, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

next-intl-docs – ./docs

next-intl-docs-git-main-next-intl.vercel.app
next-intl-docs-next-intl.vercel.app
next-intl-docs.vercel.app

Please sign in to comment.