Skip to content

Commit 0ac4cad

Browse files
committed
Cleanup
1 parent 5568a01 commit 0ac4cad

File tree

9 files changed

+56
-35
lines changed

9 files changed

+56
-35
lines changed

common/api-review/auth.api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ export interface AuthSettings {
258258
// @public
259259
export function beforeAuthStateChanged(auth: Auth, callback: (user: User | null) => void | Promise<void>, onAbort?: () => void): Unsubscribe;
260260

261+
// @beta
262+
export const browserCookiePersistence: Persistence;
263+
261264
// @public
262265
export const browserLocalPersistence: Persistence;
263266

@@ -296,9 +299,6 @@ export function connectAuthEmulator(auth: Auth, url: string, options?: {
296299
disableWarnings: boolean;
297300
}): void;
298301

299-
// @beta
300-
export const cookiePersistence: Persistence;
301-
302302
// @public
303303
export function createUserWithEmailAndPassword(auth: Auth, email: string, password: string): Promise<UserCredential>;
304304

docs-devsite/auth.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ Firebase Authentication
150150
| --- | --- |
151151
| [ActionCodeOperation](./auth.md#actioncodeoperation) | An enumeration of the possible email action types. |
152152
| [AuthErrorCodes](./auth.md#autherrorcodes) | A map of potential <code>Auth</code> error codes, for easier comparison with errors thrown by the SDK. |
153+
| [browserCookiePersistence](./auth.md#browsercookiepersistence) | <b><i>(Public Preview)</i></b> An implementation of [Persistence](./auth.persistence.md#persistence_interface) of type 'COOKIE', for use on the client-side in applications leveraging hybrid rendering and middleware. \* |
153154
| [browserLocalPersistence](./auth.md#browserlocalpersistence) | An implementation of [Persistence](./auth.persistence.md#persistence_interface) of type <code>LOCAL</code> using <code>localStorage</code> for the underlying storage. |
154155
| [browserPopupRedirectResolver](./auth.md#browserpopupredirectresolver) | An implementation of [PopupRedirectResolver](./auth.popupredirectresolver.md#popupredirectresolver_interface) suitable for browser based applications. |
155156
| [browserSessionPersistence](./auth.md#browsersessionpersistence) | An implementation of [Persistence](./auth.persistence.md#persistence_interface) of <code>SESSION</code> using <code>sessionStorage</code> for the underlying storage. |
156-
| [cookiePersistence](./auth.md#cookiepersistence) | <b><i>(Public Preview)</i></b> An implementation of [Persistence](./auth.persistence.md#persistence_interface) of type 'COOKIE', for use in applications leveraging server-side rendering and middleware. |
157157
| [cordovaPopupRedirectResolver](./auth.md#cordovapopupredirectresolver) | An implementation of [PopupRedirectResolver](./auth.popupredirectresolver.md#popupredirectresolver_interface) suitable for Cordova based applications. |
158158
| [debugErrorMap](./auth.md#debugerrormap) | A verbose error map with detailed descriptions for most error codes.<!-- -->See discussion at [AuthErrorMap](./auth.autherrormap.md#autherrormap_interface) |
159159
| [FactorId](./auth.md#factorid) | An enum of factors that may be used for multifactor authentication. |
@@ -1961,6 +1961,21 @@ AUTH_ERROR_CODES_MAP_DO_NOT_USE_INTERNALLY: {
19611961
}
19621962
```
19631963

1964+
## browserCookiePersistence
1965+
1966+
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
1967+
>
1968+
1969+
An implementation of [Persistence](./auth.persistence.md#persistence_interface) of type 'COOKIE', for use on the client-side in applications leveraging hybrid rendering and middleware. \*
1970+
1971+
This persistence method requires companion middleware to function, such as that provided by for NextJS.
1972+
1973+
<b>Signature:</b>
1974+
1975+
```typescript
1976+
browserCookiePersistence: Persistence
1977+
```
1978+
19641979
## browserLocalPersistence
19651980

19661981
An implementation of [Persistence](./auth.persistence.md#persistence_interface) of type `LOCAL` using `localStorage` for the underlying storage.
@@ -1993,19 +2008,6 @@ An implementation of [Persistence](./auth.persistence.md#persistence_interface)
19932008
browserSessionPersistence: Persistence
19942009
```
19952010

1996-
## cookiePersistence
1997-
1998-
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
1999-
>
2000-
2001-
An implementation of [Persistence](./auth.persistence.md#persistence_interface) of type 'COOKIE', for use in applications leveraging server-side rendering and middleware.
2002-
2003-
<b>Signature:</b>
2004-
2005-
```typescript
2006-
cookiePersistence: Persistence
2007-
```
2008-
20092011
## cordovaPopupRedirectResolver
20102012

20112013
An implementation of [PopupRedirectResolver](./auth.popupredirectresolver.md#popupredirectresolver_interface) suitable for Cordova based applications.

packages/auth/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export * from './src';
4343

4444
// persistence
4545
import { browserLocalPersistence } from './src/platform_browser/persistence/local_storage';
46-
import { cookiePersistence } from './src/platform_browser/persistence/cookie_storage';
46+
import { browserCookiePersistence } from './src/platform_browser/persistence/cookie_storage';
4747
import { browserSessionPersistence } from './src/platform_browser/persistence/session_storage';
4848
import { indexedDBLocalPersistence } from './src/platform_browser/persistence/indexed_db';
4949

@@ -84,7 +84,7 @@ import { getAuth } from './src/platform_browser';
8484

8585
export {
8686
browserLocalPersistence,
87-
cookiePersistence,
87+
browserCookiePersistence,
8888
browserSessionPersistence,
8989
indexedDBLocalPersistence,
9090
PhoneAuthProvider,

packages/auth/src/api/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { FirebaseError, isCloudflareWorker, querystring } from '@firebase/util';
18+
import { FirebaseError, getModularInstance, isCloudflareWorker, querystring } from '@firebase/util';
1919

2020
import { AuthErrorCode, NamedErrorParams } from '../core/errors';
2121
import {
@@ -32,6 +32,7 @@ import { IdTokenResponse, TaggedWithTokenResponse } from '../model/id_token';
3232
import { IdTokenMfaResponse } from './authentication/mfa';
3333
import { SERVER_ERROR_MAP, ServerError, ServerErrorMap } from './errors';
3434
import { PersistenceType } from '../core/persistence';
35+
import { CookiePersistence } from '../platform_browser/persistence/cookie_storage';
3536

3637
export const enum HttpMethod {
3738
POST = 'POST',
@@ -280,14 +281,15 @@ export function _getFinalTarget(
280281
? _emulatorUrl(auth.config as ConfigInternal, base)
281282
: `${auth.config.apiScheme}://${base}`;
282283

283-
// TODO get the exchange URL from the persistence method
284-
// this solves the window test
284+
// Cookie auth works by MiTMing the signIn and token endpoints from the developer's backend,
285+
// saving the idToken and refreshToken into cookies, and then redacting the refreshToken
286+
// from the response
285287
if (
286-
authInternal._getPersistence() === PersistenceType.COOKIE &&
288+
authInternal._getPersistenceType() === PersistenceType.COOKIE &&
287289
CookieAuthProxiedEndpoints.includes(path)
288290
) {
289-
const params = new URLSearchParams({ finalTarget });
290-
return `${window.location.origin}/__cookies__?${params.toString()}`;
291+
const cookiePersistence = authInternal._getPersistence() as CookiePersistence;
292+
return cookiePersistence._getFinalTarget(finalTarget).toString();
291293
}
292294

293295
return finalTarget;

packages/auth/src/core/auth/auth_impl.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,10 +524,14 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
524524
}
525525
}
526526

527-
_getPersistence(): string {
527+
_getPersistenceType(): string {
528528
return this.assertedPersistence.persistence.type;
529529
}
530530

531+
_getPersistence(): PersistenceInternal {
532+
return this.assertedPersistence.persistence;
533+
}
534+
531535
_updateErrorMap(errorMap: AuthErrorMap): void {
532536
this._errorFactory = new ErrorFactory<AuthErrorCode, AuthErrorParams>(
533537
'auth',

packages/auth/src/core/auth/initialize.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ describe('core/auth/initialize', () => {
170170
sdkClientVersion: expectedSdkClientVersion,
171171
tokenApiHost: 'securetoken.googleapis.com'
172172
});
173-
expect(auth._getPersistence()).to.eq('NONE');
173+
expect(auth._getPersistenceType()).to.eq('NONE');
174174
});
175175

176176
it('should set persistence', async () => {
@@ -179,7 +179,7 @@ describe('core/auth/initialize', () => {
179179
}) as AuthInternal;
180180
await auth._initializationPromise;
181181

182-
expect(auth._getPersistence()).to.eq('SESSION');
182+
expect(auth._getPersistenceType()).to.eq('SESSION');
183183
});
184184

185185
it('should set persistence with fallback', async () => {
@@ -188,7 +188,7 @@ describe('core/auth/initialize', () => {
188188
}) as AuthInternal;
189189
await auth._initializationPromise;
190190

191-
expect(auth._getPersistence()).to.eq('SESSION');
191+
expect(auth._getPersistenceType()).to.eq('SESSION');
192192
});
193193

194194
it('should set resolver', async () => {

packages/auth/src/model/auth.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { UserInternal } from './user';
3333
import { ClientPlatform } from '../core/util/version';
3434
import { RecaptchaConfig } from '../platform_browser/recaptcha/recaptcha';
3535
import { PasswordPolicyInternal } from './password_policy';
36+
import { PersistenceInternal } from '../core/persistence';
3637

3738
export type AppName = string;
3839
export type ApiKey = string;
@@ -86,7 +87,8 @@ export interface AuthInternal extends Auth {
8687
_key(): string;
8788
_startProactiveRefresh(): void;
8889
_stopProactiveRefresh(): void;
89-
_getPersistence(): string;
90+
_getPersistenceType(): string;
91+
_getPersistence(): PersistenceInternal;
9092
_getRecaptchaConfig(): RecaptchaConfig | null;
9193
_getPasswordPolicyInternal(): PasswordPolicyInternal | null;
9294
_updatePasswordPolicy(): Promise<void>;

packages/auth/src/platform_browser/persistence/cookie_storage.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ export class CookiePersistence implements PersistenceInternal {
4545
readonly type = PersistenceType.COOKIE;
4646
listenerUnsubscribes: Map<StorageEventListener, () => void> = new Map();
4747

48+
// TODO define hostname in the constructor
49+
_getFinalTarget(originalUrl: string): URL|string {
50+
if (typeof window === undefined) {
51+
return originalUrl;
52+
}
53+
return new URL(`${window.location.origin}/__cookies__`);
54+
}
55+
4856
async _isAvailable(): Promise<boolean> {
4957
// TODO isSecureContext
5058
if (typeof navigator === 'undefined' || typeof document === 'undefined') {
@@ -135,9 +143,12 @@ export class CookiePersistence implements PersistenceInternal {
135143
}
136144

137145
/**
138-
* An implementation of {@link Persistence} of type 'COOKIE', for use in applications leveraging
139-
* server-side rendering and middleware.
140-
*
146+
* An implementation of {@link Persistence} of type 'COOKIE', for use on the client-side in
147+
* applications leveraging hybrid rendering and middleware.
148+
* *
149+
* @remarks This persistence method requires companion middleware to function, such as that provided
150+
* by {@link https://firebaseopensource.com/projects/firebaseextended/reactfire/ ReactFire} for
151+
* NextJS.
141152
* @beta
142153
*/
143-
export const cookiePersistence: Persistence = CookiePersistence;
154+
export const browserCookiePersistence: Persistence = CookiePersistence;

packages/auth/src/platform_node/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class FailClass {
8181

8282
export const browserLocalPersistence = inMemoryPersistence;
8383
export const browserSessionPersistence = inMemoryPersistence;
84-
export const cookiePersistence = inMemoryPersistence;
84+
export const browserCookiePersistence = inMemoryPersistence;
8585
export const indexedDBLocalPersistence = inMemoryPersistence;
8686
export const browserPopupRedirectResolver = NOT_AVAILABLE_ERROR;
8787
export const PhoneAuthProvider = FailClass;

0 commit comments

Comments
 (0)