rt2zz / redux-persist

persist and rehydrate a redux store
MIT License
12.94k stars 866 forks source link

How to set expiry for multiple reducers in v5 #508

Open chen-fang-1992 opened 6 years ago

chen-fang-1992 commented 6 years ago

I tried use redux-persist-transform-expire, while I received a error: redux-persist: invalid option passed to persistStore: "transforms". You may be incorrectly passing persistConfig into persistStore, whereas it should be passed into persistReducer.

How can I set expiry for my reducer either in 30 minutes or on browser closing?

`const configureStore = () => {
    let middleware = [ thunk ]
    if (process.env.NODE_ENV !== 'production')
        middleware.push(createLogger())

    let expireTransform = createExpirationTransform({
        expireKey: 'persistExpiresAt',
        defaultState: {
            custom: 'values'
        }
    })

    let config = {
        key: 'root',
        storage
    }

    let configureReducer = persistReducer(config, reducer)

    let store = createStore(
        configureReducer,
        applyMiddleware(...middleware)
    )

    persistStore(store, {
        transforms: [expireTransform]
    })

    return store
}`
rt2zz commented 6 years ago

as the error says, you need transforms on the persistReducer config, not the persistStore config. The redux-persist-transform-expire are probably not updated for redux-persist v5 yet.

chen-fang-1992 commented 6 years ago

I've tried to put transforms into persistReducer config, while it still doesn't work. It seems redux-persist-transform-expire is only compatible with v4.

My situation is that I need to persist users login state in my web app, otherwise it will be lost when refresh page. However, if I use redux-persist, the state will be persist locally like forever. If users forget to click logout, the session will expire at server anyway while the client-side won't expire.

Any kind advice is appreciated. Thanks.

chen-fang-1992 commented 6 years ago

I've tried sessionStorage instead of localStorage. Here is another problem, if I close browser tab, the session will be ended by redux-persist while the session at server is still alive.

rt2zz commented 6 years ago

options:

  1. update redux-persist-transform-expire to work with v5 (not sure why it would break)
  2. upon rehydration check if the access token is still valid and act accordingly. This can be done either
    • in PersistGate onBeforeLift
    • in persist config migrate method
chenyu1990 commented 5 years ago

as the error says, you need transforms on the persistReducer config, not the persistStore config. The redux-persist-transform-expire are probably not updated for redux-persist v5 yet.

but it's ur example, i also encountered the same problem.