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

Use Cookies instead of LocalStorage for JWT token store. #301

Open
2 tasks
chpapa opened this issue Aug 14, 2017 · 8 comments
Open
2 tasks

Use Cookies instead of LocalStorage for JWT token store. #301

chpapa opened this issue Aug 14, 2017 · 8 comments

Comments

@chpapa
Copy link

chpapa commented Aug 14, 2017

Reported by Clients, to make isomorphic JS app, it needs to be in Cookies. (Plus briefly search online, it seems Cookies is the recommend way?)

Either we make cookies default (if it make sense) or have an options to use Cookies?

  • Skygear SDK Date/Version: v1.1
  • JS runtime:
  • Skygear Server Date/Version: v1.1
  • Is this a regression?
  • Attached logs, screenshots
@malyon
Copy link

malyon commented Oct 3, 2017

I wouldn't mind taking a look into this.

@chpapa
Copy link
Author

chpapa commented Oct 3, 2017

@malyon cool, let me or @rickmak know if you run into any trouble or need some help

@cheungpat
Copy link
Contributor

reference: https://github.com/zeit/next.js/

@cheungpat
Copy link
Contributor

So we are considering this use case for server side rendering which requires cookie to pass user session state:

  • The client fetches a page from a nodejs process. The HTTP request contains a previously nodejs-supplied cookie.
  • The nodejs process use the cookie to obtain client user session state, the nodejs process then calls skygear api to obtain skydb data.
  • The nodejs process use the skydb data to render the page (aka server-side rendering), the nodejs generates/forwards user session state in the form of a cookie.

It has yet to know:

  • whether the cookie is generated from the skygear server or from the nodejs process
  • whether the nodejs process is part of skygear handler plugin

@IniZio
Copy link
Contributor

IniZio commented Jan 17, 2018

  • In SSR the server needs to grab data from skygear db to prerender the page, so the token generated from skygear is needed. Currrently it is stored in session storage or cookie if session is unsupported.

The login process is still done in client, so the cookie is actually produced in client and then sent to server. This should be done by developers who are using skygear, since different SSR frameworks have different flags to distinguish server-side and client-side.

Problem is that skygear does not allow passing in token as parameter from server due to its current auth implementation.

  • the nodejs process is the SSR framework, so I do not think it is related to skygear handler

@rickmak
Copy link
Member

rickmak commented Jan 18, 2018

whether the cookie is generated from the skygear server or from the nodejs process

The cookie should be set by skygear-server.

Problem is that skygear does not allow passing in token as parameter from server due to its current auth implementation.

We plan to make skygear set the cookie properly, all user request will have the skygear auth token in cookie. Including request to skygear-server or node-SSR server. In the way, it will solved @IniZio problem, right?

whether the nodejs process is part of skygear handler plugin

The framework should affect more than the runtime. Let me providing some info on how react works.

One approach used by https://github.com/nfl/react-helmet . The developer will required to call renderStatic after ReactDOMServer.renderToString.
related discussion: nfl/react-helmet#216
This approach is not thread-safe and not working with streamed SSR. But it requires no change in the client side code.

For supporting SSR stream, we may refer how https://github.com/tizmagik/react-head do. But this is probably another approach and may require users to change how they currently use skygear within react component.

We may want to take a look on vue and angular too.

@chpapa
Copy link
Author

chpapa commented Jan 18, 2018

@rickmak @cheungpat @carmenlau I was told firebase use both cookies and localstorage a hybrid approach. Maybe worth checkout to see how it was done too.

@louischan-oursky
Copy link
Contributor

Skygear server should support rotating session. For example, if the skygear server is configured with AUTH_COOKIE_MAX_AGE=86400, then every response (including non-auth related) must have the HTTP header Set-Cookie: a=b; Max-Age=86400 set.

Alternatively if the skygear server is configured with AUTH_COOKIE_EXPIRES=, then the header will be Set-Cookie: a=b;, meaning that the cookie is a session cookie.

The default value of AUTH_COOKIE_EXPIRES should be the same as the token store's.

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

No branches or pull requests

8 participants