Skip to content

Commit a639700

Browse files
authored
Update README.md
1 parent 044de96 commit a639700

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ Keep your session sync with localStorage and Redux :key:
1010
Redux React Session provides an API that allows to manage sessions through the app, with authorization function for [react-router](https://github.com/ReactTraining/react-router) and a persisted session.
1111

1212
## Installation
13+
yarn:
14+
15+
`yarn add redux-react-session`
16+
17+
npm:
18+
1319
`npm install redux-react-session --save`
1420

1521
## Usage
@@ -109,3 +115,59 @@ Returns the current user if exists
109115

110116
### deleteUser : Promise
111117
Deletes the current user
118+
119+
## Server Rendering
120+
`redux-react-session` also provides methods to keep the session with server rendering using cookies. So the session will work on the server side as well as the client side.
121+
122+
### initServerSession(store, refreshOnCheckAuth:Boolean, redirectPath:String)
123+
Initialize an instance of the server session service.
124+
125+
This function is used in the `server.js` to initialize a session service instance in each request.
126+
```javascript
127+
// server.js
128+
import { sessionService } from 'redux-react-session';
129+
130+
// ...
131+
app.use((req, res) => {
132+
const store = createStrore();
133+
sessionService.initServerSession(store, req);
134+
// ...
135+
}
136+
// ...
137+
```
138+
139+
### initClientSession(store, refreshOnCheckAuth:Boolean, redirectPath:String)
140+
Initialize an instance of the client session service.
141+
142+
This function is used in the `client.js` of the server rendering to initialize a session service instance.
143+
```javascript
144+
// client.js
145+
import { createStore } from 'redux';
146+
import { sessionService } from 'redux-react-session';
147+
148+
const store = createStore(reducer)
149+
150+
sessionService.initClientSession(store);
151+
```
152+
153+
### checkAuthServer
154+
Authorization function for [react-router](https://github.com/ReactTraining/react-router) to restrict routes, it checks if exist a session and redirects to the `redirectPath`.
155+
156+
The difference between `checkAuthServer` and `checkAuth` is that the first one is used in the server side and check the authorization with the cookies in the request.
157+
158+
```javascript
159+
// routes.js
160+
import React from 'react';
161+
import { Route, IndexRoute } from 'react-router';
162+
import { sessionService } from 'redux-react-session';
163+
import App from './components/App';
164+
import HomePage from './containers/HomePage';
165+
import LoginPage from './containers/LoginPage';
166+
167+
export default (
168+
<Route path="/" component={App}>
169+
<IndexRoute onEnter={sessionService.checkAuthServer} component={HomePage} />
170+
<Route path="login" component={LoginPage} />
171+
</Route>
172+
);
173+
```

0 commit comments

Comments
 (0)