The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
docs(ui-library): fix oauth example code to use exchangeCodeForSession (#36311)
Andrew Smith51d22de62e95
diff --git a/apps/ui-library/public/r/social-auth-tanstack.json b/apps/ui-library/public/r/social-auth-tanstack.jsonindex 5304e77f91a..ccc42a24aae 100644--- a/apps/ui-library/public/r/social-auth-tanstack.json+++ b/apps/ui-library/public/r/social-auth-tanstack.json@@ -43,7 +43,7 @@},{"path": "registry/default/blocks/social-auth-tanstack/routes/auth/oauth.ts",- "content": "import { createClient } from '@/registry/default/clients/tanstack/lib/supabase/server'\nimport { type EmailOtpType } from '@supabase/supabase-js'\nimport { createFileRoute, redirect } from '@tanstack/react-router'\nimport { createServerFn } from '@tanstack/react-start'\nimport { getWebRequest } from '@tanstack/react-start/server'\n\nconst confirmFn = createServerFn({ method: 'GET' })\n .validator((searchParams: unknown) => {\n if (\n searchParams &&\n typeof searchParams === 'object' &&\n 'token_hash' in searchParams &&\n 'type' in searchParams &&\n 'next' in searchParams\n ) {\n return searchParams\n }\n throw new Error('Invalid search params')\n })\n .handler(async (ctx) => {\n const request = getWebRequest()\n\n if (!request) {\n throw redirect({ to: `/auth/error`, search: { error: 'No request' } })\n }\n\n const searchParams = ctx.data\n const token_hash = searchParams['token_hash'] as string\n const type = searchParams['type'] as EmailOtpType | null\n const _next = (searchParams['next'] ?? '/') as string\n const next = _next?.startsWith('/') ? _next : '/'\n\n if (token_hash && type) {\n const supabase = createClient()\n\n const { error } = await supabase.auth.verifyOtp({\n type,\n token_hash,\n })\n console.log(error?.message)\n if (!error) {\n // redirect user to specified redirect URL or root of app\n throw redirect({ href: next })\n } else {\n // redirect the user to an error page with some instructions\n throw redirect({\n to: `/auth/error`,\n search: { error: error?.message },\n })\n }\n }\n\n // redirect the user to an error page with some instructions\n throw redirect({\n to: `/auth/error`,\n search: { error: 'No token hash or type' },\n })\n })\n\nexport const Route = createFileRoute('/auth/confirm')({\n preload: false,\n loader: (opts) => confirmFn({ data: opts.location.search }),\n})\n",+ "content": "import { createClient } from '@/registry/default/clients/tanstack/lib/supabase/server'\nimport { createFileRoute, redirect } from '@tanstack/react-router'\nimport { createServerFn } from '@tanstack/react-start'\nimport { getWebRequest } from '@tanstack/react-start/server'\n\nconst confirmFn = createServerFn({ method: 'GET' })\n .validator((searchParams: unknown) => {\n if (\n searchParams &&\n typeof searchParams === 'object' &&\n 'code' in searchParams &&\n 'next' in searchParams\n ) {\n return searchParams\n }\n throw new Error('Invalid search params')\n })\n .handler(async (ctx) => {\n const request = getWebRequest()\n\n if (!request) {\n throw redirect({ to: `/auth/error`, search: { error: 'No request' } })\n }\n\n const searchParams = ctx.data\n const code = searchParams['code'] as string\n const _next = (searchParams['next'] ?? '/') as string\n const next = _next?.startsWith('/') ? _next : '/'\n\n if (code) {\n const supabase = createClient()\n\n const { error } = await supabase.auth.exchangeCodeForSession(code)\n if (!error) {\n // redirect user to specified redirect URL or root of app\n throw redirect({ href: next })\n } else {\n // redirect the user to an error page with some instructions\n throw redirect({\n to: `/auth/error`,\n search: { error: error?.message },\n })\n }\n }\n\n // redirect the user to an error page with some instructions\n throw redirect({\n to: `/auth/error`,\n search: { error: 'No code found' },\n })\n })\n\nexport const Route = createFileRoute('/auth/confirm')({\n preload: false,\n loader: (opts) => confirmFn({ data: opts.location.search }),\n})\n","type": "registry:file","target": "routes/auth/oauth.ts"},diff --git a/apps/ui-library/registry/default/blocks/social-auth-tanstack/routes/auth/oauth.ts b/apps/ui-library/registry/default/blocks/social-auth-tanstack/routes/auth/oauth.tsindex 2996a594a65..529a6e539ab 100644--- a/apps/ui-library/registry/default/blocks/social-auth-tanstack/routes/auth/oauth.ts+++ b/apps/ui-library/registry/default/blocks/social-auth-tanstack/routes/auth/oauth.ts@@ -1,5 +1,4 @@import { createClient } from '@/registry/default/clients/tanstack/lib/supabase/server'-import { type EmailOtpType } from '@supabase/supabase-js'import { createFileRoute, redirect } from '@tanstack/react-router'import { createServerFn } from '@tanstack/react-start'import { getWebRequest } from '@tanstack/react-start/server'@@ -9,8 +8,7 @@ const confirmFn = createServerFn({ method: 'GET' })if (searchParams &&typeof searchParams === 'object' &&- 'token_hash' in searchParams &&- 'type' in searchParams &&+ 'code' in searchParams &&'next' in searchParams) {return searchParams@@ -25,19 +23,14 @@ const confirmFn = createServerFn({ method: 'GET' })}const searchParams = ctx.data- const token_hash = searchParams['token_hash'] as string- const type = searchParams['type'] as EmailOtpType | null+ const code = searchParams['code'] as stringconst _next = (searchParams['next'] ?? '/') as stringconst next = _next?.startsWith('/') ? _next : '/'- if (token_hash && type) {+ if (code) {const supabase = createClient()- const { error } = await supabase.auth.verifyOtp({- type,- token_hash,- })- console.log(error?.message)+ const { error } = await supabase.auth.exchangeCodeForSession(code)if (!error) {// redirect user to specified redirect URL or root of appthrow redirect({ href: next })@@ -53,7 +46,7 @@ const confirmFn = createServerFn({ method: 'GET' })// redirect the user to an error page with some instructionsthrow redirect({to: `/auth/error`,- search: { error: 'No token hash or type' },+ search: { error: 'No code found' },})})