Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Improved the auth/callback doc to solve the url bug (#18734)

vinhegde86f03f854aaa
diff --git a/apps/docs/pages/guides/auth/server-side/oauth-with-pkce-flow-for-ssr.mdx b/apps/docs/pages/guides/auth/server-side/oauth-with-pkce-flow-for-ssr.mdx
index 307e9f7de25..71a2a5502ad 100644
--- a/apps/docs/pages/guides/auth/server-side/oauth-with-pkce-flow-for-ssr.mdx
+++ b/apps/docs/pages/guides/auth/server-side/oauth-with-pkce-flow-for-ssr.mdx
@@ -28,18 +28,18 @@ In order to use OAuth we will need to setup a endpoint for the `code` exchange,
Create a new file at `app/auth/callback/route.ts` and populate with the following:
```ts app/auth/callback/route.ts
-import { createServerClient, type CookieOptions } from '@supabase/ssr'
import { cookies } from 'next/headers'
import { NextResponse } from 'next/server'
+import { type CookieOptions, createServerClient } from '@supabase/ssr'
export async function GET(request: Request) {
- const { searchParams } = new URL(request.url)
+ const { searchParams, origin } = new URL(request.url)
const code = searchParams.get('code')
+ // if "next" is in param, use it as the redirect URL
const next = searchParams.get('next') ?? '/'
if (code) {
const cookieStore = cookies()
-
const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
@@ -57,15 +57,14 @@ export async function GET(request: Request) {
},
}
)
-
const { error } = await supabase.auth.exchangeCodeForSession(code)
if (!error) {
- return NextResponse.redirect(next)
+ return NextResponse.redirect(`${origin}${next}`)
}
}
// return the user to an error page with instructions
- return NextResponse.redirect('/auth/auth-code-error')
+ return NextResponse.redirect(`${origin}/auth/auth-code-error`)
}
```