Skip to content

Commit 5436d08

Browse files
darkriftbernabe9
authored andcommitted
Allow validateSession to return a promise instead of an immediate value (#36)
* Allow promise like object/function to be provided as validateSession function Extracted code that would become duplicated since the promise addition * Handle promised validateSession return value the same as if it was a function that would return an immediate value. * Fixed lint issues * Added tests for validateSession function * Fixed readme to specify that the validateSession can be a function that returns a promise. * Added code example for a validateSession that returns a promise.
1 parent 3239a19 commit 5436d08

File tree

5 files changed

+1532
-1428
lines changed

5 files changed

+1532
-1428
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ Options:
6767
- refreshOnCheckAuth(**default**: false): Refresh Redux store in the `checkAuth` function
6868
- redirectPath(**default**: `"login"`): Path used when a session is rejected or doesn't exist
6969
- driver: Force to use a particular driver, could be: 'INDEXEDDB', 'WEBSQL', 'LOCALSTORAGE' or 'COOKIES'
70-
- validateSession: Function to validate the saved session, it should return a BOOLEAN. If it returns `false` the session will be destroyed
71-
70+
- validateSession: Function to validate the saved session. It can either be a function to return an immediate boolean value or a function that returns a promise. In the case it returns an immadiate value and `false` is returned the session will be destroyed. In the case of a promise, if either `false` is returned or an exception is thrown, the session will be destroyed.
7271
Example:
7372
```javascript
7473
const validateSession = (session) => {
@@ -82,6 +81,18 @@ sessionService.initSessionService(store, options)
8281
.catch(() => console.log('Redux React Session is ready and there is no session in your storage'));
8382
```
8483

84+
```javascript
85+
const validateSession = (session) => {
86+
// check if your session is still valid with a server check, through axios for instance
87+
return api.invokeRemoteSessionValidationThroughAxios(session).then(response => response.isSessionValid);
88+
}
89+
const options = { refreshOnCheckAuth: true, redirectPath: '/home', driver: 'COOKIES', validateSession };
90+
91+
sessionService.initSessionService(store, options)
92+
.then(() => console.log('Redux React Session is ready and a session was refreshed from your storage'))
93+
.catch(() => console.log('Redux React Session is ready and there is no session in your storage'));
94+
```
95+
8596
### refreshFromLocalStorage
8697
Force to refresh the Redux Store from the local storage.
8798

0 commit comments

Comments
 (0)