@@ -27,12 +27,14 @@ import {
2727 StorageEventListener
2828} from '../../core/persistence' ;
2929
30+ // Pull a cookie value from document.cookie
3031function getDocumentCookie ( name : string ) : string | null {
3132 const escapedName = name . replace ( / [ \\ ^ $ . * + ? ( ) [ \] { } | ] / g, '\\$&' ) ;
3233 const matcher = RegExp ( `${ escapedName } =([^;]+)` ) ;
3334 return document . cookie . match ( matcher ) ?. [ 1 ] ?? null ;
3435}
3536
37+ // Produce a sanitized cookie name from the persistence key
3638function getCookieName ( key : string ) : string {
3739 // __HOST- doesn't work in localhost https://issues.chromium.org/issues/40196122 but it has
3840 // desirable security properties, so lets use a different cookie name while in dev-mode.
@@ -46,6 +48,7 @@ export class CookiePersistence implements PersistenceInternal {
4648 readonly type = PersistenceType . COOKIE ;
4749 listenerUnsubscribes : Map < StorageEventListener , ( ) => void > = new Map ( ) ;
4850
51+ // used to get the URL to the backend to proxy to
4952 _getFinalTarget ( originalUrl : string ) : URL | string {
5053 if ( typeof window === undefined ) {
5154 return originalUrl ;
@@ -55,6 +58,9 @@ export class CookiePersistence implements PersistenceInternal {
5558 return url ;
5659 }
5760
61+ // To be a usable persistence method in a chain browserCookiePersistence ensures that
62+ // prerequisites have been met, namely that we're in a secureContext, navigator and document are
63+ // available and cookies are enabled. Not all UAs support these method, so fallback accordingly.
5864 async _isAvailable ( ) : Promise < boolean > {
5965 if ( typeof isSecureContext === 'boolean' && ! isSecureContext ) {
6066 return false ;
@@ -65,10 +71,12 @@ export class CookiePersistence implements PersistenceInternal {
6571 return navigator . cookieEnabled ?? true ;
6672 }
6773
74+ // Set should be a noop as we expect middleware to handle this
6875 async _set ( _key : string , _value : PersistenceValue ) : Promise < void > {
6976 return ;
7077 }
7178
79+ // Attempt to get the cookie from cookieStore, fallback to document.cookie
7280 async _get < T extends PersistenceValue > ( key : string ) : Promise < T | null > {
7381 if ( ! this . _isAvailable ( ) ) {
7482 return null ;
@@ -81,6 +89,7 @@ export class CookiePersistence implements PersistenceInternal {
8189 return getDocumentCookie ( name ) as T ;
8290 }
8391
92+ // Log out by overriding the idToken with a sentinel value of ""
8493 async _remove ( key : string ) : Promise < void > {
8594 if ( ! this . _isAvailable ( ) ) {
8695 return ;
@@ -97,6 +106,7 @@ export class CookiePersistence implements PersistenceInternal {
97106 await fetch ( `/__cookies__` , { method : 'DELETE' } ) . catch ( ( ) => undefined ) ;
98107 }
99108
109+ // Listen for cookie changes, both cookieStore and fallback to polling document.cookie
100110 _addListener ( key : string , listener : StorageEventListener ) : void {
101111 if ( ! this . _isAvailable ( ) ) {
102112 return ;
0 commit comments