无法在服务器端注销用户(Next.js和Supabase)



当使用Supabase作为验证提供者时,我如何在服务器端注销用户?

我认为最简单和最明显的解决方案就是这样做:

export const getServerSideProps: GetServerSideProps = withPageAuth({
redirectTo: '/auth/sign-in',
authRequired: true,
async getServerSideProps(ctx) {
const session = await supabaseServerClient(ctx).auth.session();
await supabaseServerClient(ctx).auth.api.signOut(session!.access_token); // There is a valid access token
return {
redirect: {
permanent: false,
destination: '/auth/sign-in',
}
}
}
});

我相信这也是文档所说的,但是当我这样做时,来自signOut的响应是:

{ error: null }

但是它什么也不做。重定向发生后,客户端的onAuthStateChange会触发TOKEN_REFRESH。

一定有我不明白的地方,但我不知道那是什么。

这将不起作用,因为您正在使用的Supabase验证助手将令牌存储在此域的cookie中。您还需要删除cookie才能使其工作。或者,您应该使用注销端点,它会为您处理所有这些。

import { supabaseClient, withPageAuth } from "@supabase/auth-helpers-nextjs";
import {
setCookies,
NextRequestAdapter,
NextResponseAdapter,
} from "@supabase/auth-helpers-shared";
export const getServerSideProps = withPageAuth({
redirectTo: "/",
async getServerSideProps(ctx) {
const session = supabaseClient.auth.session();
await supabaseClient.auth.api.signOut(session.access_token);
setCookies(
new NextRequestAdapter(ctx.req),
new NextResponseAdapter(ctx.res),
["access-token", "refresh-token", "provider-token"].map((key) => ({
name: `sb-${key}`,
value: "",
maxAge: -1,
}))
);
return {
redirect: {
destination: "/",
permanent: false,
},
};
},
});

最新更新