例如,假设我有一个名为internal.tsx 的页面,其中包含:
export const getServerSideProps: GetServerSideProps = async (ctx) => {
const session = await getSession(ctx);
if (!session) {
// TODO: Add a toast notification explaining the redirect. Ideally, the desired destination should be remembered and should be redirected to after login.
return {
redirect: {
destination: '/',
permanent: false,
},
};
}
const props = ...
return { props };
};
如果访问者在 /internal 浏览此页面,他们将被弹回我的登录页面,而无需任何解释。
相反,我希望我的登录页面在屏幕顶部显示一条 toast 通知,解释他们尝试访问的页面需要登录,并且登录后将返回该页面。
我计划使用像这样的图书馆https://github.com/fkhadra/react-toastify https://github.com/fkhadra/react-toastify在登录页面上,但无法弄清楚如何/在哪里从会话中读取访问者刚刚被重定向的信息。
理想情况下每个页面(例如/internal
)启动重定向可以指定其自己的自定义消息(例如保存到会话中的“flash 变量”),以便登录页面显示在 toast 中。
根据上面的评论尝试在这里回答。
与其使用重定向,不如使用context
并设置一个新标题并执行context.res.redirect
现在包含新的标头flashVariable
(借用 Laravel)来自我之前的评论 - 内置redirect
似乎没有能力设置这些值。
i.e
export const getServerSideProps: GetServerSideProps = async (ctx) => {
const session = await getSession(ctx);
if (!session) {
ctx.res.setHeader("yourFlashVariable", "yourFlashValue");
ctx.res.redirect('/')
}
const props = ...
return { props };
};
在你的/
路线,您可以查找 FlashVariable 并调用 toast 库。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)