Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is is possible to use localStorage before rendering in browser? #7

Open
nickzym opened this issue Apr 19, 2018 · 5 comments
Open

Is is possible to use localStorage before rendering in browser? #7

nickzym opened this issue Apr 19, 2018 · 5 comments

Comments

@nickzym
Copy link

nickzym commented Apr 19, 2018

I use token to authenticate users. It just like the following code

if(localStorage.jwtToken) {
      setAuthorizationToken(localStorage.jwtToken);
      try {
          store.dispatch(setCurrentUser(jwtDecode(localStorage.jwtToken)));
      } catch (err) {
          store.dispatch(setCurrentUser({}));
      }
  }

But localStorage is not available in node environment where inside renderToString function.

Any possible solution for this?

@yangfan0095
Copy link
Owner

you can add your token on url query .and then node can get token .

@nickzym
Copy link
Author

nickzym commented Apr 22, 2018

我上面写的代码的意思是,在我渲染首页的时候,可以通过localStorage获取登录过的token。但是服务端渲染的话,无法在node renderToString的时候,得到locaStorage存的东西啊。

@yangfan0095
Copy link
Owner

你可以把token 放到cookie 里面,ssr服务的根域名和 你认证权限的根域名要保持一直。 通过设置cookie 的domain属性 去控制根域名下子域名之间的cookie共享 具体参见https://developer.mozilla.org/zh-CN/docs/Web/API/Document/cookie 。 有服务端数据共享的需求用locaStorage 就不能满足了。😕

@nickzym
Copy link
Author

nickzym commented Apr 22, 2018

感谢您的建议,我现在就是用cookie没用localstorage。

@yangfan0095
Copy link
Owner

🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants