Skip to content

Commit 29ea077

Browse files
authored
Merge pull request #9 from bernabe9/release/2.0.0
Release/2.0.0
2 parents a639700 + 92ac4c6 commit 29ea077

File tree

8 files changed

+321
-146
lines changed

8 files changed

+321
-146
lines changed

README.md

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const store = createStore(reducer)
4141
sessionService.initSessionService(store);
4242
```
4343
## Examples
44-
The examples simulates a simple login/logut that sends requests to a server.
44+
The examples simulates a simple login/logout that sends requests to a server.
4545

4646
### Run the example
4747
1. **get into the folder**:`cd examples/example`
@@ -56,13 +56,19 @@ The examples simulates a simple login/logut that sends requests to a server.
5656

5757
## API
5858

59-
### initSessionService(store, refreshOnCheckAuth:Boolean, redirectPath:String)
60-
Initialize a singleton instance of the session service.
59+
### initSessionService(store, options)
60+
Initialize an instance of the session service.
6161

6262
Options:
63-
- store: Mandatory option, is used to keep sync the localStorage with Redux store
6463
- refreshOnCheckAuth(**default**: false): Refresh Redux store in the `checkAuth` function
6564
- redirectPath(**default**: `"login"`): Path used when a session is rejected or doesn't exist
65+
- driver: Force to use a particular driver, should be: 'INDEXEDDB', 'WEBSQL', 'LOCALSTORAGE' or 'COOKIES'
66+
67+
Example:
68+
```javascript
69+
const options = { refreshOnCheckAuth: true, redirectPath: '/home', driver: 'COOKIES' };
70+
sessionService.initSessionService(store, options);
71+
```
6672

6773
### refreshFromLocalStorage
6874
Force to refresh the Redux Store from the localStorage.
@@ -91,8 +97,10 @@ export default (
9197

9298
Note: If you're using react-router v4 this function it's not necessary. Check out the [react-router-v4-example](https://github.com/bernabe9/redux-react-session/tree/master/examples/react-router-v4-example)
9399

100+
Note: This function could be used in the client side as well as the server side.
101+
94102
### saveSession(session:object) : Promise
95-
Saves the session object in the localStorage and changes the `authenticated` flag to `true` in Redux Store
103+
Saves the session object in the storage/cookies and changes the `authenticated` flag to `true` in Redux Store
96104

97105
### loadSession : Promise(currentSession:Object)
98106
Returns the current session if exists
@@ -105,21 +113,21 @@ loadSession
105113
```
106114

107115
### deleteSession : Promise
108-
Deletes the current session
116+
Deletes the current session from the storage/cookies
109117

110118
### saveUser(user:object) : Promise
111-
Saves the user object in the localStorage and in the Redux Store
119+
Saves the user object in the storage/cookies and in the Redux Store
112120

113121
### loadUser : Promise
114122
Returns the current user if exists
115123

116124
### deleteUser : Promise
117-
Deletes the current user
125+
Deletes the current user from the storage/cookies
118126

119127
## Server Rendering
120128
`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.
121129

122-
### initServerSession(store, refreshOnCheckAuth:Boolean, redirectPath:String)
130+
### initServerSession(store, req)
123131
Initialize an instance of the server session service.
124132

125133
This function is used in the `server.js` to initialize a session service instance in each request.
@@ -136,8 +144,8 @@ app.use((req, res) => {
136144
// ...
137145
```
138146
139-
### initClientSession(store, refreshOnCheckAuth:Boolean, redirectPath:String)
140-
Initialize an instance of the client session service.
147+
### initSessionService(store, { driver: 'COOKIES' })
148+
Initialize an instance of the client session service, IMPORTANT to set the option 'COOKIES'(this is the way that the client send the session data to the server).
141149
142150
This function is used in the `client.js` of the server rendering to initialize a session service instance.
143151
```javascript
@@ -147,27 +155,5 @@ import { sessionService } from 'redux-react-session';
147155

148156
const store = createStore(reducer)
149157

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-
);
158+
initSessionService(store, { driver: 'COOKIES' });
173159
```

examples/example/src/actions/sessionActions.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import * as sessionApi from '../api/sessionApi';
55
export const login = (user) => {
66
return () => {
77
return sessionApi.login(user).then(response => {
8-
sessionService.saveSession(response.token)
8+
const { token } = response;
9+
sessionService.saveSession({ token })
910
.then(() => {
1011
sessionService.saveUser(response.data)
1112
.then(() => {

examples/example/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const reducer = combineReducers({
1717
const store = createStore(reducer, undefined, compose(applyMiddleware(thunkMiddleware)));
1818

1919
// Init the session service
20-
sessionService.initSessionService(store);
20+
sessionService.initSessionService(store, { driver: 'COOKIES' });
2121

2222
render(
2323
<Provider store={store}>

examples/react-router-v4-example/src/actions/sessionActions.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import * as sessionApi from '../api/sessionApi';
44
export const login = (user, history) => {
55
return () => {
66
return sessionApi.login(user).then(response => {
7-
sessionService.saveSession(response.token)
7+
const { token } = response;
8+
sessionService.saveSession({ token })
89
.then(() => {
910
sessionService.saveUser(response.data)
1011
.then(() => {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-react-session",
3-
"version": "1.3.0",
3+
"version": "2.0.1",
44
"description": "",
55
"main": "dist/index.js",
66
"scripts": {
@@ -51,7 +51,7 @@
5151
"webpack": "2.2.1"
5252
},
5353
"dependencies": {
54-
"localforage": "^1.4.0 || ^1.5.0",
54+
"localforage": "1.5.0",
5555
"js-cookie": "2.1.4"
5656
},
5757
"peerDependencies": {

0 commit comments

Comments
 (0)