昨天我遇到了一个问题,我无法在路径中运行 getServerSideProps() 。有关更多信息 getServerSideProps() 未被调用 nextJS 13 查看此主题。
正如我提到的,我想知道验证会话密钥仍在服务器端的最佳实践。我意识到这样我就不会将 cookie 发送到 express 服务器,因为 nextJS 服务器端不拥有一个:
import { redirect } from 'next/navigation';
import Content from "@/components/content";
import LoginForm from "@/components/loginForm";
import Title from "@/components/title";
async function isLoggedIn() {
try {
const response = await api.get("/users/session-check", {
withCredentials: true,
});
if (response.status === 200) return true;
} catch (err) {
console.log(err.message);
}
return false;
}
function Page() {
const isLogged = await isLoggedIn();
if (isLogged) redirect('/');
return (
<Content>
<div className="ml-2 my-2">
{"NextJS is ok."}
<Title text="Login" />
</div>
<LoginForm />
</Content>
);
}
export default async Page;
昨天我尝试调用一个
getServerSideProps(context)
函数,我可以在其中从 context 解析 cookie。不幸的是,我发现这是 nextJS 中运行服务器端代码的旧方法。
我要实现的关键功能是将用户重定向到根页面
/
如果他已登录。如果没有,请将他留在登录屏幕上。