@@ -13,12 +13,11 @@ let instance;
1313
1414export class sessionService {
1515 constructor ( store , refreshOnCheckAuth , redirectPath ) {
16- if ( ! instance ) {
17- instance = this ;
18- instance . store = store ;
19- instance . refreshOnCheckAuth = refreshOnCheckAuth ;
20- instance . redirectPath = redirectPath ;
21- }
16+ instance = this ;
17+ instance . store = store ;
18+ instance . refreshOnCheckAuth = refreshOnCheckAuth ;
19+ instance . redirectPath = redirectPath ;
20+ instance . client = false ;
2221 return instance ;
2322 }
2423
@@ -33,6 +32,36 @@ export class sessionService {
3332 sessionService . refreshFromLocalStorage ( ) ;
3433 }
3534
35+ static initClientSession ( store , refreshOnCheckAuth = false , redirectPath = 'login' ) {
36+ instance = new sessionService ( store , refreshOnCheckAuth , redirectPath ) ;
37+ instance . client = true ;
38+ sessionService . refreshFromLocalStorage ( ) ;
39+ }
40+
41+ static initServerSession ( store , req , refreshOnCheckAuth = false , redirectPath = 'login' ) {
42+ instance = new sessionService ( store , refreshOnCheckAuth , redirectPath ) ;
43+ instance . client = true ;
44+ const parseCookies = ( req ) => {
45+ const list = { } ;
46+ const rc = req . get ( 'cookie' ) ;
47+
48+ rc && rc . split ( ';' ) . forEach ( cookie => {
49+ const parts = cookie . split ( '=' ) ;
50+ list [ parts [ 0 ] . trim ( ) ] = JSON . parse ( decodeURIComponent ( parts [ 1 ] ) ) ;
51+ } ) ;
52+
53+ return list ;
54+ } ;
55+ sessionService . refreshFromClient ( parseCookies ( req ) ) ;
56+ }
57+
58+ static refreshFromClient ( cookies ) {
59+ if ( Object . keys ( cookies ) . length > 0 ) {
60+ sessionService . saveSession ( cookies [ constant . USER_SESSION ] ) ;
61+ sessionService . saveUser ( cookies [ constant . USER_DATA ] ) ;
62+ }
63+ }
64+
3665 static refreshFromLocalStorage ( ) {
3766 return sessionService . loadSession ( )
3867 . then ( ( ) => {
@@ -67,33 +96,58 @@ export class sessionService {
6796 } ) ;
6897 }
6998
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+
70113 static saveSession ( session ) {
71114 return new Promise ( ( resolve ) => {
72- localForage . setItem ( constant . USER_SESSION , session )
73- . then ( ( ) => {
74- instance . store . dispatch ( getSessionSuccess ( ) ) ;
75- resolve ( ) ;
76- } )
77- . catch ( ( ) => {
115+ if ( instance . client ) {
78116 Cookies . set ( constant . USER_SESSION , session ) ;
79117 instance . store . dispatch ( getSessionSuccess ( ) ) ;
80118 resolve ( ) ;
81- } ) ;
119+ } else {
120+ localForage . setItem ( constant . USER_SESSION , session )
121+ . then ( ( ) => {
122+ instance . store . dispatch ( getSessionSuccess ( ) ) ;
123+ resolve ( ) ;
124+ } )
125+ . catch ( ( ) => {
126+ Cookies . set ( constant . USER_SESSION , session ) ;
127+ instance . store . dispatch ( getSessionSuccess ( ) ) ;
128+ resolve ( ) ;
129+ } ) ;
130+ }
82131 } ) ;
83132 }
84133
85134 static loadSession ( ) {
86135 return new Promise ( ( resolve , reject ) => {
87- localForage . getItem ( constant . USER_SESSION )
88- . then ( ( currentSession ) => {
89- if ( currentSession ) {
90- resolve ( currentSession ) ;
91- } else {
92- const cookies = Cookies . getJSON ( constant . USER_SESSION ) ;
93- cookies ? resolve ( cookies ) : reject ( 'Session not found' ) ;
94- }
95- } )
96- . catch ( err => reject ( err ) ) ;
136+ if ( instance . client ) {
137+ const cookies = Cookies . getJSON ( constant . USER_SESSION ) ;
138+ cookies ? resolve ( cookies ) : reject ( 'Session not found' ) ;
139+ } else {
140+ localForage . getItem ( constant . USER_SESSION )
141+ . then ( ( currentSession ) => {
142+ if ( currentSession ) {
143+ resolve ( currentSession ) ;
144+ } else {
145+ const cookies = Cookies . getJSON ( constant . USER_SESSION ) ;
146+ cookies ? resolve ( cookies ) : reject ( 'Session not found' ) ;
147+ }
148+ } )
149+ . catch ( err => reject ( err ) ) ;
150+ }
97151 } ) ;
98152 }
99153
@@ -106,31 +160,42 @@ export class sessionService {
106160
107161 static saveUser ( user ) {
108162 return new Promise ( ( resolve ) => {
109- localForage . setItem ( constant . USER_DATA , user )
110- . then ( ( user ) => {
111- instance . store . dispatch ( getUserSessionSuccess ( user ) ) ;
112- resolve ( ) ;
113- } )
114- . catch ( ( ) => {
163+ if ( instance . client ) {
115164 instance . store . dispatch ( getUserSessionSuccess ( user ) ) ;
116165 Cookies . set ( constant . USER_DATA , user ) ;
117166 resolve ( ) ;
118- } ) ;
167+ } else {
168+ localForage . setItem ( constant . USER_DATA , user )
169+ . then ( ( user ) => {
170+ instance . store . dispatch ( getUserSessionSuccess ( user ) ) ;
171+ resolve ( ) ;
172+ } )
173+ . catch ( ( ) => {
174+ instance . store . dispatch ( getUserSessionSuccess ( user ) ) ;
175+ Cookies . set ( constant . USER_DATA , user ) ;
176+ resolve ( ) ;
177+ } ) ;
178+ }
119179 } ) ;
120180 }
121181
122182 static loadUser ( ) {
123183 return new Promise ( ( resolve , reject ) => {
124- localForage . getItem ( constant . USER_DATA )
125- . then ( ( currentUser ) => {
126- if ( currentUser ) {
127- resolve ( currentUser ) ;
128- } else {
129- const cookies = Cookies . getJSON ( constant . USER_DATA ) ;
130- cookies ? resolve ( cookies ) : reject ( 'User not found' ) ;
131- }
132- } )
133- . catch ( err => reject ( err ) ) ;
184+ if ( instance . client ) {
185+ const cookies = Cookies . getJSON ( constant . USER_DATA ) ;
186+ cookies ? resolve ( cookies ) : reject ( 'User not found' ) ;
187+ } else {
188+ localForage . getItem ( constant . USER_DATA )
189+ . then ( ( currentUser ) => {
190+ if ( currentUser ) {
191+ resolve ( currentUser ) ;
192+ } else {
193+ const cookies = Cookies . getJSON ( constant . USER_DATA ) ;
194+ cookies ? resolve ( cookies ) : reject ( 'User not found' ) ;
195+ }
196+ } )
197+ . catch ( err => reject ( err ) ) ;
198+ }
134199 } ) ;
135200 }
136201
0 commit comments