简体   繁体   English

注销后 Next-auth 将我重定向回仪表板

[英]Next-auth redirects me back to dashboard after logout

I have a credential based auth flow in NextJS and a dashboard page I have also created custom AccessDenied component in case it not logged used would end up on the dashboard route, hence I did not set redirect in the getServerSideProps where I fetch the data.我在 NextJS 中有一个基于凭据的身份验证流程和一个仪表板页面我还创建了自定义 AccessDenied 组件,以防它没有记录使用最终会出现在仪表板路由上,因此我没有在获取数据的 getServerSideProps 中设置重定向。 so my checkout handler looks like so所以我的结帐处理程序看起来像这样

    onClick={() => {
            signOut();
            router.push("/");
          }}

but after I end up on the index page after a short while i am redirecte to the dashboard page, I don'tt understand why但是在我被重定向到仪表板页面后不久我就结束了索引页面,我不明白为什么

This is my dashboard page这是我的仪表板页面

const DashboardPage = ({
  sessionData,
}: InferGetServerSidePropsType<typeof getServerSideProps>) => {
  const [renderedInfo, setRenderedInfo] = useState<RenderedInfo>();
  const matches = useMediaQuery("(max-width: 768px)");

  useEffect(() => {
    if (!matches) setRenderedInfo("UserDetails");
  }, [matches]);

  if (!sessionData) {
    return <AccessDenied />;
  }

  const { address, userEmail, avatar } = sessionData;
  const menuPanelVisible = !matches || (matches && !renderedInfo);
  const userDisplayName = address?.length ? address[0] : userEmail;
  return (
    <div className="flex-1 px-4 flex w-full justify-center">
      {menuPanelVisible && (
        <MenuPanel>
          <UserAvatar avatar={avatar} displayName={userDisplayName} />
          <DashboardNavigation setRenderedInfo={setRenderedInfo} />
        </MenuPanel>
      )}
      <InformationPanel
        renderedInfo={renderedInfo}
        setRenderedInfo={setRenderedInfo}
        address={address}
      />
    </div>
  );
};

export default DashboardPage;

interface GetServerSidePropsType {
  sessionData?: {
    address: string[] | undefined;
    avatar:
      | {
          url: string;
          width?: number | null | undefined;
          height?: number | null | undefined;
        }
      | null
      | undefined;
    userEmail: string;
  } | null;
}

export const getServerSideProps: GetServerSideProps<
  GetServerSidePropsType
> = async (context) => {
  const session = await unstable_getServerSession(
    context.req,
    context.res,
    authOptions
  );

  console.log({ email: session?.user.email });

  if (!session?.user.email) {
    return {
      props: {
        session: null,
      },
    };
  }

  const { data } = await personAuthApolloClient.query<
    GetPersonDetailsByEmailQuery,
    GetPersonDetailsByEmailQueryVariables
  >({
    query: GetPersonDetailsByEmailDocument,
    variables: {
      email: session.user.email,
    },
  });

  const address = data.person?.address;
  const avatar = data.person?.avatar;
  const sessionData = { address, avatar, userEmail: session.user.email };

  return {
    props: { sessionData },
  };
};

What do I need to do to stay on the index page after redirect on logout?注销重定向后我需要做什么才能留在索引页面上?

Thanks谢谢

This is because next auth didn't make signout correctly and it still thinks that your session is valid, if U want correctly make signOut from a session U would need to provide callback Url in your signOut function like signOut({ callbackUrl: your-provider-id }), where callbackUrl must be id of your current provider, U can take this provider from a session object这是因为下一个身份验证没有正确注销并且它仍然认为你的 session 是有效的,如果你想从 session 正确注销你需要在你的注销 function 中提供回调 Url 就像 signOut({ callbackUrl: your-provider-id }),其中 callbackUrl 必须是您当前提供者的 ID,您可以从 session object 获取此提供者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM