supabase 身份验证注销自动重定向到注销页面

问题描述 投票:0回答:1

如何更改配置操作系统supabase auth注销以避免自动重定向到注销页面?

代码:

supabase.auth.signOut();

上面的代码结果自动重定向到我的 nextjs 应用程序中的注销。我想重定向到我的主页!

我已经阅读了文档,我已经阅读了在线论坛。

authentication next.js supabase
1个回答
0
投票

您是否尝试过将服务器操作与中间件重定向结合使用?

注销服务器操作:

export async function signout() {
  const supabase = createClient();
  try {
    const { error } = await supabase.auth.signOut();
    if (error) {
      throw new Error(error.message);
    }
  } catch (error) {
    throw error;
  }
  revalidatePath("/", "layout");
}

在你的中间件中

try {
  const {
    data: {
      user
    },
  } = await supabase.auth.getUser();

  // If user is visiting any admin routes, redirect them to the home page if they're not signed in
  if (!user &&
    request.nextUrl.pathname.startsWith("/admin") &&
    request.nextUrl.pathname !== "/admin/login" &&
    request.nextUrl.pathname !== "/admin/sign-up"
  ) {
    console.log("User is not signed in");
    return NextResponse.redirect(`${request.nextUrl.origin}/admin/login`);
  }

  // If user is signed in, hide the login page
  if (user && request.nextUrl.pathname === "/admin/login") {
    return NextResponse.redirect(`${request.nextUrl.origin}/admin`);
  }
  // If none of the above conditions are met, continue to the requested route
  return NextResponse.next();
} catch (error) {
  console.error("Error in middleware:", error);
  // Handle the error as needed
}
return response;
}

希望这有帮助。我从我的一个应用程序中获取它,因此它适合我的情况

© www.soinside.com 2019 - 2024. All rights reserved.