@@ -5,6 +5,7 @@ import actions from '../store/actions/user/actions';
55import * as asyncActions from '../store/actions/user/asyncActions' ;
66import bindActionCreators from '../util/bindActionCreators' ;
77import BrowserPersistence from '../util/simplePersistence' ;
8+ import { getTokenFromCookie , shouldPreferCookies } from '../util/cookieHelper' ;
89
910const UserContext = createContext ( ) ;
1011
@@ -25,20 +26,35 @@ const UserContextProvider = props => {
2526 ] ) ;
2627
2728 useEffect ( ( ) => {
28- // check if the user's token is not expired
29+ // Check for authentication tokens (localStorage or cookie)
2930 const storage = new BrowserPersistence ( ) ;
30- const item = storage . getRawItem ( 'signin_token' ) ;
31+ let hasValidToken = false ;
3132
33+ // First, check localStorage token (existing behavior)
34+ const item = storage . getRawItem ( 'signin_token' ) ;
3235 if ( item ) {
3336 const { ttl, timeStored } = JSON . parse ( item ) ;
3437 const now = Date . now ( ) ;
3538
36- // if the token's TTYL has expired, we need to sign out
39+ // if the token's TTL has expired, we need to sign out
3740 if ( ttl && now - timeStored > ttl * 1000 ) {
3841 asyncActions . signOut ( ) ;
42+ return ;
43+ }
44+ hasValidToken = true ;
45+ }
46+
47+ // Check for cookie token (for PWA session sharing)
48+ // If we're in standalone PWA mode and have a cookie token but no localStorage token
49+ if ( ! hasValidToken && shouldPreferCookies ( ) ) {
50+ const cookieToken = getTokenFromCookie ( ) ;
51+ if ( cookieToken && userState && ! userState . isSignedIn ) {
52+ // Set the token in Redux so the app knows user is authenticated
53+ actions . setToken ( cookieToken ) ;
54+ hasValidToken = true ;
3955 }
4056 }
41- } , [ asyncActions ] ) ;
57+ } , [ asyncActions , actions , userState ] ) ;
4258
4359 return (
4460 < UserContext . Provider value = { contextValue } >
0 commit comments