Supabase Oauth与Remix在重定向时出现错误,”您正试图在POP导航上使用阻止器”。

huangapple go评论83阅读模式
英文:

Supabase Oauth with Remix errors on redirect "You are trying to use a blocker on a POP navigation"

问题

使用Supabase Auth通过第三方身份验证提供程序登录后,我没有看到我应该看到的内容,即仅对经过身份验证的用户可见的数据。此外,我在控制台中收到以下警告:

您正在尝试在由@remix-run/router创建的位置之外使用阻止器进行POP导航。这将在生产中静默失败。如果您通过window.history.pushState/window.location.hash而不是使用路由导航API导航到路由器之外,可能会发生这种情况。如果您正在使用createHashRouter并且用户手动更改了URL,也可能会发生这种情况。

现在这很令人困惑。

这是我调用Supabase Auth的地方:

// login.tsx
import { useOutletContext } from '@remix-run/react';

import type { SupabaseOutletContext } from '~/root';

export function Login() {
	const { supabase } = useOutletContext<SupabaseOutletContext>();

	async function handleLogin() {
		return (await supabase).auth.signInWithOAuth({
			provider: 'github',
		});
	}

	const handleLogout = async () => {
		const { error } = await supabase.auth.signOut();
		if (error) {
			console.log(error);
		}
	};

	return (
		<div>
			<h1>Login</h1>
			<button onClick={handleLogin}>Login</button>
			<button onClick={handleLogout}>Logout</button>
		</div>
	);
}
// root.tsx
// ...
import { createClient } from '@supabase/supabase-js';

import type { SupabaseClient } from '@supabase/supabase-js';
// ...
type TypedSupabaseClient = SupabaseClient<Database>;
export type SupabaseOutletContext = {
	supabase: TypedSupabaseClient;
};

// ...

export const loader = ({ }: LoaderArgs) => {
	const env = {
		SUPABASE_URL: process.env.SUPABASE_URL!,
		SUPABASE_ANON_KEY: process.env.SUPABASE_ANON_KEY!,
	};

	return json({ env });
};

export default function App() {
	const { env } = useLoaderData<typeof loader>();

	const [supabase] = useState(() =>
		createClient<Database>(env.SUPABASE_URL!, env.SUPABASE_ANON_KEY!)
	);

	return (
		{/* ... */}
				<Outlet context={{ supabase }} />
		{/* ... */}
	);
}

最后,index.tsx

import type { LoaderArgs } from '@remix-run/node';

import { useLoaderData } from '@remix-run/react';
import { Login } from 'components/login';
import supabase from 'utils/supabase.server';

export const loader = async ({ }: LoaderArgs) => {
    // 使用RLS
	const { data } = await supabase.from('messages').select('*');

	return { messages: data ?? [] };
};

export default function Index() {
	const { messages } = useLoaderData<typeof loader>();
	return (
		<>
			<Login />
			<pre>{JSON.stringify({ messages }, null, 2)}</pre>
		</>
	);
}

为什么我会收到这个警告?为什么我没有通过身份验证?它原本是一个简单的应用程序,用于检查Supabase。如何修复警告?

软件包版本:

  • "@remix-run/node": "1.12.0",
  • "@remix-run/react": "1.12.0",
  • "react": "18.2.0",
  • "react-dom": "18.2.0",
  • "@supabase/supabase-js": "^2.7.1"

编辑1:
我在公共存储库中共享了代码https://github.com/jessaleks/supabase-remix-debug-repo

英文:

After I sign in with a 3rd party auth provider using Supabase Auth, I am not seeing what I should be seeing, that is data visible to authenticated users only. Moreover, I am getting this warning in my console

> You are trying to use a blocker on a POP navigation to a location that
> was not created by @remix-run/router. This will fail silently in
> production. This can happen if you are navigating outside the router
> via window.history.pushState/window.location.hash instead of using
> router navigation APIs. This can also happen if you are using
> createHashRouter and the user manually changes the URL.

Now this is puzzling.

Here's where I call supabase auth:

// login.tsx
import { useOutletContext } from &#39;@remix-run/react&#39;;

import type { SupabaseOutletContext } from &#39;~/root&#39;;

export function Login() {
	const { supabase } = useOutletContext&lt;SupabaseOutletContext&gt;();

	async function handleLogin() {
		return (await supabase).auth.signInWithOAuth({
			provider: &#39;github&#39;,
		});
	}

	const handleLogout = async () =&gt; {
		const { error } = await supabase.auth.signOut();
		if (error) {
			console.log(error);
		}
	};

	return (
		&lt;div&gt;
			&lt;h1&gt;Login&lt;/h1&gt;
			&lt;button onClick={handleLogin}&gt;Login&lt;/button&gt;
			&lt;button onClick={handleLogout}&gt;Logout&lt;/button&gt;
		&lt;/div&gt;
	);
}
// root.tsx
// ...
import { createClient } from &#39;@supabase/supabase-js&#39;;

import type { SupabaseClient } from &#39;@supabase/supabase-js&#39;;
// ...
type TypedSupabaseClient = SupabaseClient&lt;Database&gt;;
export type SupabaseOutletContext = {
	supabase: TypedSupabaseClient;
};

// ...

export const loader = ({}: LoaderArgs) =&gt; {
	const env = {
		SUPABASE_URL: process.env.SUPABASE_URL!,
		SUPABASE_ANON_KEY: process.env.SUPABASE_ANON_KEY!,
	};

	return json({ env });
};

export default function App() {
	const { env } = useLoaderData&lt;typeof loader&gt;();

	const [supabase] = useState(() =&gt;
		createClient&lt;Database&gt;(env.SUPABASE_URL!, env.SUPABASE_ANON_KEY!)
	);

	return (
		{/* ... */}
				&lt;Outlet context={{ supabase }} /&gt;
		{/* ... */}	);
}

Finally, index.tsx

import type { LoaderArgs } from &#39;@remix-run/node&#39;;

import { useLoaderData } from &#39;@remix-run/react&#39;;
import { Login } from &#39;components/login&#39;;
import supabase from &#39;utils/supabase.server&#39;;

export const loader = async ({}: LoaderArgs) =&gt; {
    // uses RLS
	const { data } = await supabase.from(&#39;messages&#39;).select(&#39;*&#39;);

	return { messages: data ?? [] };
};

export default function Index() {
	const { messages } = useLoaderData&lt;typeof loader&gt;();
	return (
		&lt;&gt;
			&lt;Login /&gt;
			&lt;pre&gt;{JSON.stringify({ messages }, null, 2)}&lt;/pre&gt;
		&lt;/&gt;
	);
}

Supabase Oauth与Remix在重定向时出现错误,”您正试图在POP导航上使用阻止器”。

Why on Earth am I getting this warning? Why am I not authenticated? It was meant to be this simple app to check out supabase. What to do to fix the warning?

Versions of packages:

    &quot;@remix-run/node&quot;: &quot;1.12.0&quot;,
    &quot;@remix-run/react&quot;: &quot;1.12.0&quot;,
    &quot;react&quot;: &quot;18.2.0&quot;,
    &quot;react-dom&quot;: &quot;18.2.0&quot;,
    &quot;@supabase/supabase-js&quot;: &quot;^2.7.1&quot;

Edit 1:
I shared the code in a public repo https://github.com/jessaleks/supabase-remix-debug-repo

答案1

得分: 2

这是在本地运行时出现的警告,因为Supabase在成功完成与GitHub的OAuth过程后重定向。Remix(或React Router)只是记录一个警告,告诉你在没有使用它们的路由器的情况下进行了重定向,因为你正在编写的应用程序代码应该使用它们的路由器进行重定向等操作。在Supabase在身份验证过程中执行此操作时,可以安全地忽略 👍

英文:

This is a warning that appears when running locally, as Supabase redirects after receiving a token from the successful OAuth process with GitHub. Remix (or React Router) are just logging a warning to tell you that you redirected without using their router, as the application code you are writing should use their router for redirects and things. In the case of Supabase doing this during the auth process, it is safe to ignore 👍

huangapple
  • 本文由 发表于 2023年2月6日 07:13:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定