1- import * as constant from './constants' ;
1+ import { USER_SESSION , USER_DATA } from './constants' ;
22import * as localForage from 'localforage' ;
33import * as Cookies from "js-cookie" ;
44import {
@@ -12,39 +12,36 @@ import reducer from './reducer';
1212let instance ;
1313
1414export class sessionService {
15- constructor ( store , refreshOnCheckAuth , redirectPath ) {
15+ constructor ( store , options ) {
1616 instance = this ;
17- instance . store = store ;
18- instance . refreshOnCheckAuth = refreshOnCheckAuth ;
19- instance . redirectPath = redirectPath ;
20- instance . client = false ;
17+ sessionService . setOptions ( store , options ) ;
2118 return instance ;
2219 }
2320
24- static setOptions ( store , refreshOnCheckAuth = false , redirectPath = 'login' ) {
21+ static setOptions ( store , {
22+ driver,
23+ refreshOnCheckAuth = false ,
24+ redirectPath = 'login' ,
25+ server = false
26+ } = { } ) {
2527 instance . store = store ;
2628 instance . refreshOnCheckAuth = refreshOnCheckAuth ;
2729 instance . redirectPath = redirectPath ;
30+ instance . driver = driver ;
31+ instance . server = server ;
32+ driver && driver !== 'COOKIES' && localForage . setDriver ( localForage [ driver ] ) ;
2833 }
2934
30- static initSessionService ( store , refreshOnCheckAuth = false , redirectPath = 'login' ) {
31- instance = new sessionService ( store , refreshOnCheckAuth , redirectPath ) ;
32- sessionService . refreshFromLocalStorage ( ) ;
33- }
34-
35- static initClientSession ( store , refreshOnCheckAuth = false , redirectPath = 'login' ) {
36- instance = new sessionService ( store , refreshOnCheckAuth , redirectPath ) ;
37- instance . client = true ;
35+ static initSessionService ( store , options ) {
36+ instance = new sessionService ( store , options ) ;
3837 sessionService . refreshFromLocalStorage ( ) ;
3938 }
4039
41- static initServerSession ( store , req , refreshOnCheckAuth = false , redirectPath = 'login' ) {
42- instance = new sessionService ( store , refreshOnCheckAuth , redirectPath ) ;
43- instance . client = true ;
40+ static initServerSession ( store , req , options ) {
41+ instance = new sessionService ( store , { ...options , server : true } ) ;
4442 const parseCookies = ( req ) => {
4543 const list = { } ;
4644 const rc = req . get ( 'cookie' ) ;
47-
4845 rc && rc . split ( ';' ) . forEach ( cookie => {
4946 const parts = cookie . split ( '=' ) ;
5047 list [ parts [ 0 ] . trim ( ) ] = JSON . parse ( decodeURIComponent ( parts [ 1 ] ) ) ;
@@ -57,8 +54,8 @@ export class sessionService {
5754
5855 static refreshFromClient ( cookies ) {
5956 if ( Object . keys ( cookies ) . length > 0 ) {
60- sessionService . saveSession ( cookies [ constant . USER_SESSION ] ) ;
61- sessionService . saveUser ( cookies [ constant . USER_DATA ] ) ;
57+ sessionService . saveSession ( cookies [ USER_SESSION ] ) ;
58+ sessionService . saveUser ( cookies [ USER_DATA ] ) ;
6259 }
6360 }
6461
@@ -96,34 +93,24 @@ export class sessionService {
9693 } ) ;
9794 }
9895
99- static checkAuthServer ( nextState , replace , next ) {
100- const { store } = instance ;
101- const { authenticated } = store . getState ( ) . session ;
102- if ( authenticated ) {
103- next ( ) ;
104- } else {
105- replace ( {
106- pathname : instance . redirectPath ,
107- state : { nextPathname : nextState . location . pathname }
108- } ) ;
109- next ( ) ;
110- }
111- }
112-
11396 static saveSession ( session ) {
11497 return new Promise ( ( resolve ) => {
115- if ( instance . client ) {
116- Cookies . set ( constant . USER_SESSION , session ) ;
98+ if ( instance . server ) {
99+ instance [ USER_SESSION ] = session ;
100+ instance . store . dispatch ( getSessionSuccess ( ) ) ;
101+ resolve ( ) ;
102+ } else if ( instance . driver === 'COOKIES' ) {
103+ Cookies . set ( USER_SESSION , session ) ;
117104 instance . store . dispatch ( getSessionSuccess ( ) ) ;
118105 resolve ( ) ;
119106 } else {
120- localForage . setItem ( constant . USER_SESSION , session )
107+ localForage . setItem ( USER_SESSION , session )
121108 . then ( ( ) => {
122109 instance . store . dispatch ( getSessionSuccess ( ) ) ;
123110 resolve ( ) ;
124111 } )
125112 . catch ( ( ) => {
126- Cookies . set ( constant . USER_SESSION , session ) ;
113+ Cookies . set ( USER_SESSION , session ) ;
127114 instance . store . dispatch ( getSessionSuccess ( ) ) ;
128115 resolve ( ) ;
129116 } ) ;
@@ -133,16 +120,18 @@ export class sessionService {
133120
134121 static loadSession ( ) {
135122 return new Promise ( ( resolve , reject ) => {
136- if ( instance . client ) {
137- const cookies = Cookies . getJSON ( constant . USER_SESSION ) ;
123+ if ( instance . server ) {
124+ instance [ USER_SESSION ] ? resolve ( instance [ USER_SESSION ] ) : reject ( ) ;
125+ } else if ( instance . driver === 'COOKIES' ) {
126+ const cookies = Cookies . getJSON ( USER_SESSION ) ;
138127 cookies ? resolve ( cookies ) : reject ( 'Session not found' ) ;
139128 } else {
140- localForage . getItem ( constant . USER_SESSION )
129+ localForage . getItem ( USER_SESSION )
141130 . then ( ( currentSession ) => {
142131 if ( currentSession ) {
143132 resolve ( currentSession ) ;
144133 } else {
145- const cookies = Cookies . getJSON ( constant . USER_SESSION ) ;
134+ const cookies = Cookies . getJSON ( USER_SESSION ) ;
146135 cookies ? resolve ( cookies ) : reject ( 'Session not found' ) ;
147136 }
148137 } )
@@ -152,27 +141,32 @@ export class sessionService {
152141 }
153142
154143 static deleteSession ( ) {
155- return localForage . removeItem ( constant . USER_SESSION ) . then ( ( ) => {
144+ return localForage . removeItem ( USER_SESSION ) . then ( ( ) => {
156145 instance . store . dispatch ( getSessionError ( ) ) ;
157- Cookies . remove ( constant . USER_SESSION ) ;
146+ Cookies . remove ( USER_SESSION ) ;
147+ delete instance [ USER_SESSION ] ;
158148 } ) ;
159149 }
160150
161151 static saveUser ( user ) {
162152 return new Promise ( ( resolve ) => {
163- if ( instance . client ) {
153+ if ( instance . server ) {
154+ instance [ USER_DATA ] = user ;
155+ instance . store . dispatch ( getSessionSuccess ( ) ) ;
156+ resolve ( ) ;
157+ } else if ( instance . driver === 'COOKIES' ) {
158+ Cookies . set ( USER_DATA , user ) ;
164159 instance . store . dispatch ( getUserSessionSuccess ( user ) ) ;
165- Cookies . set ( constant . USER_DATA , user ) ;
166160 resolve ( ) ;
167161 } else {
168- localForage . setItem ( constant . USER_DATA , user )
162+ localForage . setItem ( USER_DATA , user )
169163 . then ( ( user ) => {
170164 instance . store . dispatch ( getUserSessionSuccess ( user ) ) ;
171165 resolve ( ) ;
172166 } )
173167 . catch ( ( ) => {
174168 instance . store . dispatch ( getUserSessionSuccess ( user ) ) ;
175- Cookies . set ( constant . USER_DATA , user ) ;
169+ Cookies . set ( USER_DATA , user ) ;
176170 resolve ( ) ;
177171 } ) ;
178172 }
@@ -181,16 +175,18 @@ export class sessionService {
181175
182176 static loadUser ( ) {
183177 return new Promise ( ( resolve , reject ) => {
184- if ( instance . client ) {
185- const cookies = Cookies . getJSON ( constant . USER_DATA ) ;
178+ if ( instance . server ) {
179+ instance [ USER_DATA ] ? resolve ( instance [ USER_DATA ] ) : reject ( ) ;
180+ } else if ( instance . driver === 'COOKIES' ) {
181+ const cookies = Cookies . getJSON ( USER_DATA ) ;
186182 cookies ? resolve ( cookies ) : reject ( 'User not found' ) ;
187183 } else {
188- localForage . getItem ( constant . USER_DATA )
184+ localForage . getItem ( USER_DATA )
189185 . then ( ( currentUser ) => {
190186 if ( currentUser ) {
191187 resolve ( currentUser ) ;
192188 } else {
193- const cookies = Cookies . getJSON ( constant . USER_DATA ) ;
189+ const cookies = Cookies . getJSON ( USER_DATA ) ;
194190 cookies ? resolve ( cookies ) : reject ( 'User not found' ) ;
195191 }
196192 } )
@@ -200,9 +196,10 @@ export class sessionService {
200196 }
201197
202198 static deleteUser ( ) {
203- return localForage . removeItem ( constant . USER_DATA ) . then ( ( ) => {
199+ return localForage . removeItem ( USER_DATA ) . then ( ( ) => {
204200 instance . store . dispatch ( getUserSessionError ( ) ) ;
205- Cookies . remove ( constant . USER_DATA ) ;
201+ Cookies . remove ( USER_DATA ) ;
202+ delete instance [ USER_DATA ] ;
206203 } ) ;
207204 }
208205}
0 commit comments