【发布时间】:2021-11-24 19:39:00
【问题描述】:
我在 localstorage 中设置令牌并使用 (axios) 将其发送到每个请求,但问题是当我使用 getServerSideProps 时令牌未发送,因为无法在服务器端访问 localStorage .
我认为我应该使用 Cookie,我尝试了 js-cookies,但它在服务器上也不起作用。
是否有任何解决方案可以在服务器端以 getServerSideProps 和 getStaticProps 的形式发送令牌?
【问题讨论】:
-
您使用哪个库进行身份验证?
-
实际上,我不使用任何我只是将令牌存储在 redux-store 和 localstorage 中,并与每个客户端请求一起发送。
-
您无法从
getServerSideProps访问localStorage,因为它在服务器上运行。尝试使用 cookie 来存储令牌,或者将身份验证移至客户端。
标签: axios next.js server-side-rendering