Skip to content

Commit 0ecb457

Browse files
authored
Merge pull request #45 from jerelmiller/fix-state-sync
Ensure state is synced when running invalidate
2 parents 576ecb7 + 74f85d0 commit 0ecb457

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

packages/redux-simple-auth/src/middleware.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ export default (config = {}) => {
8484
}
8585

8686
return next => action => {
87+
const sync = () => {
88+
const { session: prevSession } = getState()
89+
const result = next(action)
90+
const { session } = getState()
91+
92+
if (session.data !== prevSession.data) {
93+
const { authenticator, data } = session
94+
95+
storage.persist({ authenticated: { ...data, authenticator } })
96+
}
97+
98+
return result
99+
}
100+
87101
switch (action.type) {
88102
case AUTHENTICATE: {
89103
const authenticator = findAuthenticator(action.meta.authenticator)
@@ -132,7 +146,7 @@ export default (config = {}) => {
132146
if (!state.session) {
133147
throw new Error(
134148
'No session data to invalidate. Be sure you authenticate the ' +
135-
'session before you try to invalidate it'
149+
'session before you try to invalidate it'
136150
)
137151
}
138152

@@ -143,7 +157,7 @@ export default (config = {}) => {
143157
return Promise.reject(
144158
new Error(
145159
'No authenticated session. Be sure you authenticate the session ' +
146-
'before you try to invalidate it'
160+
'before you try to invalidate it'
147161
)
148162
)
149163
}
@@ -157,25 +171,13 @@ export default (config = {}) => {
157171
'config.'
158172
)
159173
}
160-
161-
return authenticator.invalidate(getSessionData(state)).then(
162-
() => next(action),
163-
// TODO: what happens in this block:
164-
() => dispatch(invalidateSessionFailed())
165-
)
174+
175+
return authenticator
176+
.invalidate(getSessionData(state))
177+
.then(sync, () => dispatch(invalidateSessionFailed()))
166178
}
167179
default: {
168-
const { session: prevSession } = getState()
169-
const result = next(action)
170-
const { session } = getState()
171-
172-
if (session.data !== prevSession.data) {
173-
const { authenticator, data } = session
174-
175-
storage.persist({ authenticated: { ...data, authenticator } })
176-
}
177-
178-
return result
180+
return sync()
179181
}
180182
}
181183
}

0 commit comments

Comments
 (0)