supabase/supabase

Subtle bug in Remix guide

Open

#30.900 aberto em 4 de dez. de 2024

Ver no GitHub
 (10 comments) (1 reaction) (0 assignees)TypeScript (12.387 forks)batch import
documentationexternal-issuehelp wanted

Métricas do repositório

Stars
 (102.348 stars)
Métricas de merge de PR
 (Mesclagem média 3d 9h) (534 fundiu PRs em 30d)

Description

Improve documentation

Link

https://supabase.com/docs/guides/auth/social-login/auth-github?queryGroups=environment&environment=server&queryGroups=framework&framework=remix

Describe the problem

This code is wrong:

const { data, error } = await supabase.auth.signInWithOAuth({
  provider,
  options: {
    redirectTo: 'http://example.com/auth/callback',
  },
})

if (data.url) {
  redirect(data.url) // use the redirect API for your server framework
}

It causes a bug where the cookie is not set and the auth does not work

Describe the improvement

const headers = new Headers();

 const supabase = createServerClient(process.env.SUPABASE_URL!, process.env.SUPABASE_API_KEY!, { 
	cookies: {
		getAll() {
			return parseCookieHeader(request.headers.get("Cookie") ?? "")
		},
		setAll(cookiesToSet) {
			for (const { name, value, options } of cookiesToSet) {
				headers.append("Set-Cookie", serializeCookieHeader(name, value, options))
			}
		},
	},
})
const { data, error } = await supabase.auth.signInWithOAuth({
  provider,
  options: {
    redirectTo: 'http://example.com/auth/callback',
  },
})

if (data.url) { 
  redirect(data.url, { headers }) // use the redirect API for your server framework
}

Additional context

The previous docs cause a subtle and hard to catch bug that cost my employee 2 days of work, and me 2 hours to figure out. It doesn't append the cookie headers to the redirect and doesn't set the cookies in the browser making the subsequent request to the callback url NOT have the verifier code or whatever it's called and making supabase throw an error

Guia do colaborador