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

Couldn't load authHeaders in client-side rendering #47

Closed
rayhsieh opened this issue Apr 19, 2016 · 2 comments
Closed

Couldn't load authHeaders in client-side rendering #47

rayhsieh opened this issue Apr 19, 2016 · 2 comments

Comments

@rayhsieh
Copy link

rayhsieh commented Apr 19, 2016

I'm looking into a way to keep user session alive so that user doesn't have to login again next time visiting the site. My site is using client-side rendering. After tracing the code, I figured out that there might be a chance to load session from localStorage to accomplish the goal. I'm not sure if this a good way and would like to get some feedback here.

In action/configure.js

      if (authRedirectPath) {
        dispatch(push({pathname: authRedirectPath}));
      }

      if (authRedirectHeaders && authRedirectHeaders.uid && authRedirectHeaders["access-token"]) {
        settings.initialCredentials = extend({}, settings.initialCredentials, authRedirectHeaders);
      }

      /*  Maybe check localStorage before destroying session */

      // if tokens were invalidated by server, make sure to clear browser
      // credentials
      if (!settings.initialCredentials) {
        destroySession();
      }
@rayhsieh rayhsieh changed the title Couldn't load authHeaders in the client-side rendering Couldn't load authHeaders in client-side rendering Apr 19, 2016
@nyjy85
Copy link

nyjy85 commented Apr 26, 2016

That's what I did. Don't know if it's the 'right' way to do it, but it functions well.

I added this piece of code in configure.js right where you added the /* */ comment:

if (localStorage.authHeaders) {
  settings.initialCredentials = {};
  settings.initialCredentials.headers = JSON.parse(localStorage.authHeaders)
  settings.initialCredentials.currentEndpointKey = JSON.parse(localStorage.currentConfigName)
}

Then inside client-settings.js in the applyConfig function, I do a fetch for getTokenValidationPath, which then sets the user data under state.auth.user:

if (getCurrentSettings().initialCredentials) {
    // skip initial headers check (i.e. check was already done server-side)
    // *****
    let {user, headers, config} = getCurrentSettings().initialCredentials;
    persistData(C.SAVED_CREDS_KEY, headers);
    return fetch(getTokenValidationPath('default')) 
      .then(parseResponse)
      .then(({data}) => Promise.resolve(data))
      .catch(({errors}) => Promise.reject({errors}));
    // *****

Just to note, I had to change the default settings of storage from 'cookies' to 'localStorage' in client-settings.js in order for this whole thing to work:

 proxyIf:            function() { return false; },
  proxyUrl:           "/proxy",
  forceHardRedirect:  false,
  storage:            "localStorage", // was originally set to 'cookies'
  cookieExpiry:       14,
  cookiePath:         "/",
  initialCredentials: null,

It's a bit hacky, and there are some things i hard coded (like the 'default' argument being passed into getTokenValidationPath ), but it works like a charm so far.

@rayhsieh
Copy link
Author

rayhsieh commented May 3, 2016

Thanks @nyjy85 ! The solution works like a charm. I also tested the pull request #50 by @jlonardi, and it works perfectly as well. @lynndylanhurley, you may consider to merge #50.

@rayhsieh rayhsieh closed this as completed May 3, 2016
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