0

I'm having trouble persisting authenticated sessions to the UserStore on page refresh with fluxible. In my case, I update the user store on login and everything is fine as they click around the page - but if they refresh the page - it loses the userstore state - I see that the hydrate/rehydrate isn't triggerin on page refresh - is that supposed to be triggered on the user store in that case?

How should I initialize/inject the server side req.session to the userstore in this scenario so that it loads the userstore with the correct logged in user state on a browser refresh?

class UserStore extends BaseStore {

    constructor(dispatcher) {
        super(dispatcher);
        this.reset();
        this.userInfo = {
            emailPreferences: {}
        };
        this.registrationState = {
            isLoading: false
        };
        this.resetPasswordState = {};
        this.userAddressInfo = {
            isLoading: false,
            addNew: false
        };

    }


    reset() {
        // this.loginState = {};
        console.log('resetting user');
        console.log(this.loginState);
        this.loginState = {
            loginStatus: false,
            isLoading: false,
            errorMessage: ''
        };

        this.currentUser = null;
        this.registrationStatus = REQUEST_STATUS.NONE;
        this.registrationErrorMessage = null;
        this.resetPasswordState = {};
        this.loginState.loginStatus = false;

        this.profile = null;
        this.getProfileStatus = REQUEST_STATUS.NONE;
        this.getProfileErrorMessage = null;
    }
}

And in the client.js

const dehydratedState = window.App; // Sent from the server

window.React = ReactDOM; // For chrome dev tool support

// expose debug object to browser, so that it can be enabled/disabled from browser:
// https://github.com/visionmedia/debug#browser-support
window.fluxibleDebug = debug;

debugClient('rehydrating app');

// pass in the dehydrated server state from server.js
app.rehydrate(dehydratedState, (err, context) => {
    if (err) {
        throw err;
    }
    window.context = context;
    const mountNode = document.getElementById('app');

    debugClient('React Rendering');
    ReactDOM.render(
        createElementWithContext(context),
        mountNode,
        () => debugClient('React Rendered')
    );
});

Should I trigger a rehydration of userstore/state in the client.js somehow?

MonkeyBonkey
  • 46,433
  • 78
  • 254
  • 460
  • Add your object to the localStorage (`window.localStorage`) and check if the object in your storage is set or not. (`window.localStorage.getItem('your_object_key')`and `window.localStorage.setItem('your_object_key', yourObject)`) – Philip Aug 11 '16 at 13:55
  • I don't store it in local storage expcitly - I ended up getting it to work by dispatching the setuser action on the server-side code so that it sets it to the store – MonkeyBonkey Aug 14 '16 at 00:18

0 Answers0