Skip to content

Update internal types to work with external types #3075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages-exp/auth-exp/src/core/auth/auth_impl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe('core/auth/auth_impl', () => {
const user = testUser('uid');
auth.currentUser = user;
auth._isInitialized = true;
auth.onIdTokenChange(user => {
auth.onIdTokenChanged(user => {
expect(user).to.eq(user);
done();
});
Expand All @@ -147,7 +147,7 @@ describe('core/auth/auth_impl', () => {
it('immediate callback is done async', () => {
auth._isInitialized = true;
let callbackCalled = false;
auth.onIdTokenChange(() => {
auth.onIdTokenChanged(() => {
callbackCalled = true;
});

Expand All @@ -168,7 +168,7 @@ describe('core/auth/auth_impl', () => {
context('initially currentUser is null', () => {
beforeEach(async () => {
auth.onAuthStateChanged(authStateCallback);
auth.onIdTokenChange(idTokenCallback);
auth.onIdTokenChanged(idTokenCallback);
await auth.updateCurrentUser(null);
authStateCallback.resetHistory();
idTokenCallback.resetHistory();
Expand All @@ -188,7 +188,7 @@ describe('core/auth/auth_impl', () => {
context('initially currentUser is user', () => {
beforeEach(async () => {
auth.onAuthStateChanged(authStateCallback);
auth.onIdTokenChange(idTokenCallback);
auth.onIdTokenChanged(idTokenCallback);
await auth.updateCurrentUser(user);
authStateCallback.resetHistory();
idTokenCallback.resetHistory();
Expand Down Expand Up @@ -240,8 +240,8 @@ describe('core/auth/auth_impl', () => {
it('onIdTokenChange works for multiple listeners', async () => {
const cb1 = sinon.spy();
const cb2 = sinon.spy();
auth.onIdTokenChange(cb1);
auth.onIdTokenChange(cb2);
auth.onIdTokenChanged(cb1);
auth.onIdTokenChanged(cb2);
await auth.updateCurrentUser(null);
cb1.resetHistory();
cb2.resetHistory();
Expand Down
12 changes: 10 additions & 2 deletions packages-exp/auth-exp/src/core/auth/auth_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import { getApp } from '@firebase/app-exp';
import { FirebaseApp } from '@firebase/app-types-exp';
import * as externs from '@firebase/auth-types-exp';
import {
CompleteFn,
createSubscribe,
Expand All @@ -33,7 +34,7 @@ import { AuthErrorCode } from '../errors';
import { Persistence } from '../persistence';
import { PersistenceUserManager } from '../persistence/persistence_user_manager';
import { assert } from '../util/assert';
import { ClientPlatform, _getClientVersion } from '../util/version';
import { _getClientVersion, ClientPlatform } from '../util/version';

interface AsyncAction {
(): Promise<void>;
Expand Down Expand Up @@ -79,6 +80,13 @@ class AuthImpl implements Auth {
this._notifyStateListeners();
});
}
languageCode: string | null = null;
tenantId?: string | null | undefined;
settings: externs.AuthSettings = { appVerificationDisabledForTesting: false };

useDeviceLanguage(): void {
throw new Error('Method not implemented.');
}

updateCurrentUser(user: User | null): Promise<void> {
return this.queue(() => this.directlySetCurrentUser(user));
Expand Down Expand Up @@ -107,7 +115,7 @@ class AuthImpl implements Auth {
);
}

onIdTokenChange(
onIdTokenChanged(
nextOrObserver: NextOrObserver<User>,
error?: ErrorFn,
completed?: CompleteFn
Expand Down
8 changes: 3 additions & 5 deletions packages-exp/auth-exp/src/core/persistence/react_native.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2019 Google Inc.
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,7 +21,7 @@ import {
PersistenceValue,
STORAGE_AVAILABLE_KEY
} from './';
import {ReactNativeAsyncStorage} from '@firebase/auth-types-exp';
import { ReactNativeAsyncStorage } from '@firebase/auth-types-exp';

/**
* Persistence class that wraps AsyncStorage imported from `react-native` or `@react-native-community/async-storage`.
Expand All @@ -48,9 +48,7 @@ export class ReactNativePersistence implements Persistence {
await this.storage.setItem(key, JSON.stringify(value));
}

async get<T extends PersistenceValue>(
key: string
): Promise<T | null> {
async get<T extends PersistenceValue>(key: string): Promise<T | null> {
const json = await this.storage.getItem(key);
return json ? JSON.parse(json) : null;
}
Expand Down
10 changes: 6 additions & 4 deletions packages-exp/auth-exp/src/core/strategies/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* limitations under the License.
*/

import * as externs from '@firebase/auth-types-exp';

import {
createAuthUri,
CreateAuthUriRequest
Expand All @@ -30,7 +32,7 @@ import { User } from '../../model/user';
import { _getCurrentUrl, _isHttpOrHttps } from '../util/location';

export async function fetchSignInMethodsForEmail(
auth: Auth,
auth: externs.Auth,
email: string
): Promise<string[]> {
// createAuthUri returns an error if continue URI is not http or https.
Expand All @@ -42,13 +44,13 @@ export async function fetchSignInMethodsForEmail(
continueUri
};

const { signinMethods } = await createAuthUri(auth, request);
const { signinMethods } = await createAuthUri(auth as Auth, request);

return signinMethods || [];
}

export async function sendEmailVerification(
auth: Auth,
auth: externs.Auth,
user: User,
actionCodeSettings?: ActionCodeSettings
): Promise<void> {
Expand All @@ -61,7 +63,7 @@ export async function sendEmailVerification(
setActionCodeSettingsOnRequest(request, actionCodeSettings);
}

const { email } = await api.sendEmailVerification(auth, request);
const { email } = await api.sendEmailVerification(auth as Auth, request);

if (email !== user.email) {
await user.reload();
Expand Down
18 changes: 10 additions & 8 deletions packages-exp/auth-exp/src/core/strategies/email_and_password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* limitations under the License.
*/

import * as externs from '@firebase/auth-types-exp';

import { resetPassword } from '../../api/account_management/email_and_password';
import * as api from '../../api/authentication/email_and_password';
import { ActionCodeInfo, Operation } from '../../model/action_code_info';
Expand All @@ -23,10 +25,10 @@ import {
setActionCodeSettingsOnRequest
} from '../../model/action_code_settings';
import { Auth } from '../../model/auth';
import { AuthErrorCode, AUTH_ERROR_FACTORY } from '../errors';
import { AUTH_ERROR_FACTORY, AuthErrorCode } from '../errors';

export async function sendPasswordResetEmail(
auth: Auth,
auth: externs.Auth,
email: string,
actionCodeSettings?: ActionCodeSettings
): Promise<void> {
Expand All @@ -38,26 +40,26 @@ export async function sendPasswordResetEmail(
setActionCodeSettingsOnRequest(request, actionCodeSettings);
}

await api.sendPasswordResetEmail(auth, request);
await api.sendPasswordResetEmail(auth as Auth, request);
}

export async function confirmPasswordReset(
auth: Auth,
auth: externs.Auth,
oobCode: string,
newPassword: string
): Promise<void> {
await resetPassword(auth, {
await resetPassword(auth as Auth, {
oobCode,
newPassword
});
// Do not return the email.
}

export async function checkActionCode(
auth: Auth,
auth: externs.Auth,
oobCode: string
): Promise<ActionCodeInfo> {
const response = await resetPassword(auth, {
const response = await resetPassword(auth as Auth, {
oobCode
});
if (!response.requestType) {
Expand All @@ -76,7 +78,7 @@ export async function checkActionCode(
}

export async function verifyPasswordResetCode(
auth: Auth,
auth: externs.Auth,
code: string
): Promise<string> {
const { data } = await checkActionCode(auth, code);
Expand Down
15 changes: 10 additions & 5 deletions packages-exp/auth-exp/src/core/strategies/email_link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@
* limitations under the License.
*/

import * as externs from '@firebase/auth-types-exp';

import * as api from '../../api/authentication/email_and_password';
import { Operation } from '../../model/action_code_info';
import {
ActionCodeSettings,
setActionCodeSettingsOnRequest
} from '../../model/action_code_settings';
import { ActionCodeURL } from '../action_code_url';
import { Auth } from '../../model/auth';
import { ActionCodeURL } from '../action_code_url';

export async function sendSignInLinkToEmail(
auth: Auth,
auth: externs.Auth,
email: string,
actionCodeSettings?: ActionCodeSettings
): Promise<void> {
Expand All @@ -37,10 +39,13 @@ export async function sendSignInLinkToEmail(
setActionCodeSettingsOnRequest(request, actionCodeSettings);
}

await api.sendSignInLinkToEmail(auth, request);
await api.sendSignInLinkToEmail(auth as Auth, request);
}

export function isSignInWithEmailLink(auth: Auth, emailLink: string): boolean {
const actionCodeUrl = ActionCodeURL._fromLink(auth, emailLink);
export function isSignInWithEmailLink(
auth: externs.Auth,
emailLink: string
): boolean {
const actionCodeUrl = ActionCodeURL._fromLink(auth as Auth, emailLink);
return actionCodeUrl?.operation === Operation.EMAIL_SIGNIN;
}
2 changes: 1 addition & 1 deletion packages-exp/auth-exp/src/core/user/id_token_result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
* limitations under the License.
*/

import { IdTokenResult, ParsedToken } from '@firebase/auth-types-exp';
import { base64Decode } from '@firebase/util';

import { IdTokenResult, ParsedToken } from '../../model/id_token';
import { User } from '../../model/user';
import { ProviderId } from '../providers';
import { assert } from '../util/assert';
Expand Down
2 changes: 1 addition & 1 deletion packages-exp/auth-exp/src/core/user/reload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as chaiAsPromised from 'chai-as-promised';
import * as sinon from 'sinon';
import * as sinonChai from 'sinon-chai';

import { UserInfo } from '@firebase/auth-types-exp';
import { FirebaseError } from '@firebase/util';

import { mockEndpoint } from '../../../test/api/helper';
Expand All @@ -30,7 +31,6 @@ import {
APIUserInfo,
ProviderUserInfo
} from '../../api/account_management/account';
import { UserInfo } from '../../model/user';
import { ProviderId } from '../providers';
import { _reloadWithoutSaving, reload } from './reload';

Expand Down
15 changes: 9 additions & 6 deletions packages-exp/auth-exp/src/core/user/reload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
* limitations under the License.
*/

import * as externs from '@firebase/auth-types-exp';

import {
getAccountInfo,
ProviderUserInfo
} from '../../api/account_management/account';
import { User, UserInfo } from '../../model/user';
import { User } from '../../model/user';
import { ProviderId } from '../providers';
import { assert } from '../util/assert';

Expand Down Expand Up @@ -52,7 +54,8 @@ export async function _reloadWithoutSaving(user: User): Promise<void> {
Object.assign(user, updates);
}

export async function reload(user: User): Promise<void> {
export async function reload(externUser: externs.User): Promise<void> {
const user: User = externUser as User;
await _reloadWithoutSaving(user);

// Even though the current user hasn't changed, update
Expand All @@ -62,9 +65,9 @@ export async function reload(user: User): Promise<void> {
}

function mergeProviderData(
original: UserInfo[],
newData: UserInfo[]
): UserInfo[] {
original: externs.UserInfo[],
newData: externs.UserInfo[]
): externs.UserInfo[] {
const deduped = original.filter(
o => !newData.some(n => n.providerId === o.providerId)
);
Expand All @@ -74,7 +77,7 @@ function mergeProviderData(
function extractProviderData(
providers: ProviderUserInfo[],
appName: string
): UserInfo[] {
): externs.UserInfo[] {
return providers.map(({ providerId, ...provider }) => {
assert(
providerId && Object.values<string>(ProviderId).includes(providerId),
Expand Down
4 changes: 3 additions & 1 deletion packages-exp/auth-exp/src/core/user/user_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
* limitations under the License.
*/

import { IdTokenResult } from '@firebase/auth-types-exp';

import { Auth } from '../../model/auth';
import { IdTokenResult } from '../../model/id_token';
import { User } from '../../model/user';
import { PersistedBlob } from '../persistence';
import { ProviderId } from '../providers';
Expand Down Expand Up @@ -64,6 +65,7 @@ export class UserImpl implements User {
email: string | null;
phoneNumber: string | null;
photoURL: string | null;
isAnonymous = false;

constructor({ uid, auth, stsTokenManager, ...opt }: UserParameters) {
this.uid = uid;
Expand Down
5 changes: 3 additions & 2 deletions packages-exp/auth-exp/src/model/auth.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/

import * as externs from '@firebase/auth-types-exp';
import {
CompleteFn,
ErrorFn,
Expand All @@ -40,7 +41,7 @@ export interface Config {
authDomain?: AuthDomain;
}

export interface Auth {
export interface Auth extends externs.Auth {
currentUser: User | null;
readonly name: AppName;
readonly config: Config;
Expand All @@ -54,7 +55,7 @@ export interface Auth {
error?: ErrorFn,
completed?: CompleteFn
): Unsubscribe;
onIdTokenChange(
onIdTokenChanged(
nextOrObserver: NextOrObserver<User>,
error?: ErrorFn,
completed?: CompleteFn
Expand Down
Loading