@@ -25,8 +25,8 @@ yarn add redux-simple-auth
2525
2626### Setup
2727
28- This library ships with middleware and a reducer. You will need to do the
29- following:
28+ This library ships with middleware, a reducer, and an optional store enhancer.
29+ You will need to do the following:
3030
3131##### Apply the middleware
3232
@@ -60,6 +60,32 @@ export default combineReducers({
6060})
6161```
6262
63+ ##### Optionally add the store enhancer
64+
65+ In order to use the enhancer, you will need to provide it with the storage used.
66+ If you do not need a custom storage adapter, you may import the default storage.
67+
68+ ``` javascript
69+ // ...
70+ import {
71+ createAuthMiddleware ,
72+ getInitialAuthState ,
73+ storage // or custom storage creator
74+ } from ' redux-simple-auth'
75+ import { createStore , compose , applyMiddleware } from ' redux'
76+
77+ const authMiddleware = createAuthMiddleware (/* ...*/ )
78+
79+ const store = createStore (
80+ rootReducer,
81+ /* initialState, */
82+ compose (
83+ applyMiddleware (authMiddleware),
84+ getInitialAuthState ({ storage })
85+ )
86+ )
87+ ```
88+
6389## How does it work?
6490
6591Redux Simple Auth aims to make authentication and authorization within your
@@ -429,6 +455,21 @@ const authMiddleware = createAuthMiddleware({
429455 needed for authorization. It accepts a header name for its first argument and
430456 that header's value as its second argument.
431457
458+ ### Store Enhancer
459+
460+ There may be cases where you may want the redux store initialized with the
461+ sesion data from the storage device. The store enhancer does just that. On store
462+ initialization, it will ask the storage device for the session data.
463+
464+ ``` javascript
465+ const enhancer = getInitialAuthState ({ storage })
466+ ```
467+
468+ ** Options:**
469+
470+ * ` storage ` (_ object_ ): The storage mechanism used to store the session. ** This
471+ must** be the same storage device configured with the middleware.
472+
432473## Actions
433474
434475Redux Simple Auth ships with several actions to aide in authentication for your
@@ -498,6 +539,49 @@ import { invalidateSession } from 'redux-simple-auth'
498539store .dispatch (invalidateSession ())
499540```
500541
542+ ## Selectors
543+
544+ To aid in selecting specific session state, redux simple auth ships with a few
545+ selectors for your convenience. All selectors take the store ` state ` as an
546+ argument. Note this is the entire store state, not just the session state.
547+
548+ ### ` getSessionData(state) `
549+
550+ (_ object_ ) Returns the session data set when user was authenticated. If not yet
551+ authenticated, this returns an empty object.
552+
553+ ``` javascript
554+ import { getSessionData } from ' redux-simple-auth'
555+
556+ const mapStateToProps = state => ({
557+ session: getSessionData (state)
558+ })
559+ ```
560+
561+ ### ` getIsAuthenticated(state) `
562+
563+ (_ boolean_ ) Returns whether the user is authenticated.
564+
565+ ``` javascript
566+ import { getIsAuthenticated } from ' redux-simple-auth'
567+
568+ const mapStateToProps = state => ({
569+ isAuthenticated: getIsAuthenticated (state)
570+ })
571+ ```
572+
573+ ### ` getAuthenticator(state) `
574+
575+ (_ string_ ) Returns the ` authenticator ` used when authenticating. If not yet
576+ authenticated, this is set to ` null ` .
577+
578+ ``` javascript
579+ import { getAuthenticator } from ' redux-simple-auth'
580+
581+ const mapStateToProps = state => ({
582+ authenticator: getAuthenticator (state)
583+ })
584+ ```
501585
502586## License
503587
0 commit comments