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

Question: What's the recommended way to store cookie? #36

Closed
jhwang09 opened this issue Feb 6, 2015 · 2 comments
Closed

Question: What's the recommended way to store cookie? #36

jhwang09 opened this issue Feb 6, 2015 · 2 comments

Comments

@jhwang09
Copy link

jhwang09 commented Feb 6, 2015

Since fetchr is a RESTful API, which should be stateless, so does handling cookie at the client side makes sense?

Also since it's a isomorphic JS app, reading cookie at the server side to dictate the user state also make sense right?

Just want to get these two questions out of my head before I move forward with this approach.

Thanks!
Jerome

@gpbl
Copy link
Contributor

gpbl commented Feb 7, 2015

@jhwang09 fetchr just helps for isomorphic data fetching – it's not strictly related to a RESTful API.

If you need to read a cookie from a service, you can use the req argument:

var myService = {
  name: 'myService',
  read: function (req, resource, params, config, callback) {
   if (req.cookies && req.cookies.myCookie) // get the cookie from the request
   // then make a superagent call or whatever
};

this is possible only if you create a context on the server passing the req object to it:

// express server
var server = express();
server.use(cookieParser()); // make sure it read cookies and attach them `req.cookies`
server.use(function(req, res, next) {
  var context = app.createContext({
    req: req, // pass the request to the context
    xhrContext: { _csrf: req.csrfToken() }
  });
 // go on with server-side rendering
);

I didn't see a specific example using it (or I missed it), but it should work.

As for writing a cookie, you need to create it from the browser. For example, a Component.jsx may execute an action onButtonClick which creates a cookie.

@redonkulus
Copy link
Contributor

This is accurate. Thanks @gpbl for answering the question.

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

3 participants