Skip to content

Clean up our to/from JSON implementations #3606

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 4 commits into from
Aug 12, 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
7 changes: 3 additions & 4 deletions packages-exp/auth-compat-exp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import '@firebase/installations';
import { version } from './package.json';
import { Auth } from './src/auth';
import { RecaptchaVerifier } from './src/recaptcha_verifier';
import { EmailAuthProvider } from './src/email_auth_provider';
import { Persistence } from './src/persistence';

const AUTH_TYPE = 'auth';
Expand Down Expand Up @@ -58,7 +57,7 @@ function registerAuth(instance: _FirebaseNamespace): void {
VERIFY_EMAIL: externs.Operation.VERIFY_EMAIL
}
},
EmailAuthProvider,
EmailAuthProvider: impl.EmailAuthProvider,
FacebookAuthProvider: impl.FacebookAuthProvider,
GithubAuthProvider: impl.GithubAuthProvider,
GoogleAuthProvider: impl.GoogleAuthProvider,
Expand All @@ -70,8 +69,8 @@ function registerAuth(instance: _FirebaseNamespace): void {
TwitterAuthProvider: impl.TwitterAuthProvider,
Auth: {
Persistence
}
// 'AuthCredential': fireauth.AuthCredential,
},
AuthCredential: impl.AuthCredential
// 'Error': fireauth.AuthError
})
.setInstantiationMode(InstantiationMode.LAZY)
Expand Down
20 changes: 12 additions & 8 deletions packages-exp/auth-compat-exp/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export class Auth extends impl.AuthImplCompat<User>
);

// TODO: platform needs to be determined using heuristics
impl.assertFn(apiKey, app.name, impl.AuthErrorCode.INVALID_API_KEY);
impl.assertFn(apiKey, impl.AuthErrorCode.INVALID_API_KEY, {
appName: app.name
});
const config: externs.Config = {
apiKey,
authDomain,
Expand Down Expand Up @@ -108,8 +110,8 @@ export class Auth extends impl.AuthImplCompat<User>
async getRedirectResult(): Promise<compat.UserCredential> {
impl.assertFn(
_isPopupRedirectSupported(),
this.app.name,
impl.AuthErrorCode.OPERATION_NOT_SUPPORTED
impl.AuthErrorCode.OPERATION_NOT_SUPPORTED,
{ appName: this.app.name }
);
const credential = await impl.getRedirectResult(
this._asExtern(),
Expand Down Expand Up @@ -181,7 +183,9 @@ export class Auth extends impl.AuthImplCompat<User>
case Persistence.NONE:
return impl.inMemoryPersistence;
default:
return impl.fail(auth.name, impl.AuthErrorCode.ARGUMENT_ERROR);
return impl.fail(impl.AuthErrorCode.ARGUMENT_ERROR, {
appName: auth.name
});
}
}

Expand Down Expand Up @@ -254,8 +258,8 @@ export class Auth extends impl.AuthImplCompat<User>
): Promise<compat.UserCredential> {
impl.assertFn(
_isPopupRedirectSupported(),
this.app.name,
impl.AuthErrorCode.OPERATION_NOT_SUPPORTED
impl.AuthErrorCode.OPERATION_NOT_SUPPORTED,
{ appName: this.app.name }
);
return convertCredential(
this._asExtern(),
Expand All @@ -269,8 +273,8 @@ export class Auth extends impl.AuthImplCompat<User>
async signInWithRedirect(provider: compat.AuthProvider): Promise<void> {
impl.assertFn(
_isPopupRedirectSupported(),
this.app.name,
impl.AuthErrorCode.OPERATION_NOT_SUPPORTED
impl.AuthErrorCode.OPERATION_NOT_SUPPORTED,
{ appName: this.app.name }
);
return impl.signInWithRedirect(
this._asExtern(),
Expand Down
45 changes: 0 additions & 45 deletions packages-exp/auth-compat-exp/src/email_auth_provider.ts

This file was deleted.

20 changes: 10 additions & 10 deletions packages-exp/auth-compat-exp/src/persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,25 @@ export function _validatePersistenceArgument(
): void {
assertFn(
Object.values(Persistence).includes(persistence),
auth.name,
AuthErrorCode.INVALID_PERSISTENCE
AuthErrorCode.INVALID_PERSISTENCE,
{ appName: auth.name }
);
// Validate if the specified type is supported in the current environment.
if (isReactNative()) {
// This is only supported in a browser.
assertFn(
persistence !== Persistence.SESSION,
auth.name,
AuthErrorCode.UNSUPPORTED_PERSISTENCE
AuthErrorCode.UNSUPPORTED_PERSISTENCE,
{ appName: auth.name }
);
return;
}
if (isNode()) {
// Only none is supported in Node.js.
assertFn(
persistence === Persistence.NONE,
auth.name,
AuthErrorCode.UNSUPPORTED_PERSISTENCE
AuthErrorCode.UNSUPPORTED_PERSISTENCE,
{ appName: auth.name }
);
return;
}
Expand All @@ -64,15 +64,15 @@ export function _validatePersistenceArgument(
assertFn(
persistence === Persistence.NONE ||
(persistence === Persistence.LOCAL && isIndexedDBAvailable()),
auth.name,
AuthErrorCode.UNSUPPORTED_PERSISTENCE
AuthErrorCode.UNSUPPORTED_PERSISTENCE,
{ appName: auth.name }
);
return;
}
// This is restricted by what the browser supports.
assertFn(
persistence === Persistence.NONE || _isWebStorageSupported(),
auth.name,
AuthErrorCode.UNSUPPORTED_PERSISTENCE
AuthErrorCode.UNSUPPORTED_PERSISTENCE,
{ appName: auth.name }
);
}
8 changes: 3 additions & 5 deletions packages-exp/auth-compat-exp/src/recaptcha_verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ export class RecaptchaVerifier extends impl.RecaptchaVerifier
app: FirebaseApp = firebase.app()
) {
// API key is required for web client RPC calls.
impl.assertFn(
app.options?.apiKey,
app.name,
impl.AuthErrorCode.INVALID_API_KEY
);
impl.assertFn(app.options?.apiKey, impl.AuthErrorCode.INVALID_API_KEY, {
appName: app.name
});
super(container, parameters, (app.auth!() as unknown) as externs.Auth);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export async function deleteAccount(
}

export interface ProviderUserInfo {
providerId: string;
rawId?: string;
providerId?: string;
email?: string;
displayName?: string;
photoUrl?: string;
Expand Down
6 changes: 3 additions & 3 deletions packages-exp/auth-exp/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,19 @@ export async function _performFetchWithErrorHandling<V>(

const authError = errorMap[serverErrorCode];
if (authError) {
fail(auth.name, authError);
fail(authError, { appName: auth.name });
} else {
// TODO probably should handle improperly formatted errors as well
// If you see this, add an entry to SERVER_ERROR_MAP for the corresponding error
console.error(`Unexpected API error: ${json.error.message}`);
fail(auth.name, AuthErrorCode.INTERNAL_ERROR);
fail(AuthErrorCode.INTERNAL_ERROR, { appName: auth.name });
}
}
} catch (e) {
if (e instanceof FirebaseError) {
throw e;
}
fail(auth.name, AuthErrorCode.NETWORK_REQUEST_FAILED);
fail(AuthErrorCode.NETWORK_REQUEST_FAILED, { appName: auth.name });
}
}

Expand Down
34 changes: 14 additions & 20 deletions packages-exp/auth-exp/src/core/action_code_url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,9 @@ import { expect } from 'chai';

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

import { testAuth, TestAuth } from '../../test/helpers/mock_auth';
import { ActionCodeURL } from './action_code_url';

describe('core/action_code_url', () => {
let auth: TestAuth;
beforeEach(async () => {
auth = await testAuth();
});

describe('._fromLink', () => {
it('should parse correctly formatted links', () => {
const continueUrl = 'https://www.example.com/path/to/file?a=1&b=2#c=3';
Expand All @@ -37,7 +31,7 @@ describe('core/action_code_url', () => {
'continueUrl=' +
encodeURIComponent(continueUrl) +
'&languageCode=en&tenantId=TENANT_ID&state=bla';
const actionCodeUrl = ActionCodeURL.parseLink(auth, actionLink);
const actionCodeUrl = ActionCodeURL.parseLink(actionLink);
expect(actionCodeUrl!.operation).to.eq(Operation.EMAIL_SIGNIN);
expect(actionCodeUrl!.code).to.eq('CODE');
expect(actionCodeUrl!.apiKey).to.eq('API_KEY');
Expand All @@ -53,7 +47,7 @@ describe('core/action_code_url', () => {
'https://www.example.com/finishSignIn?' +
'oobCode=CODE&mode=signIn&apiKey=API_KEY&' +
'languageCode=en';
const actionCodeUrl = ActionCodeURL.parseLink(auth, actionLink);
const actionCodeUrl = ActionCodeURL.parseLink(actionLink);
expect(actionCodeUrl!.operation).to.eq(Operation.EMAIL_SIGNIN);
});

Expand All @@ -62,7 +56,7 @@ describe('core/action_code_url', () => {
'https://www.example.com/finishSignIn?' +
'oobCode=CODE&mode=verifyAndChangeEmail&apiKey=API_KEY&' +
'languageCode=en';
const actionCodeUrl = ActionCodeURL.parseLink(auth, actionLink);
const actionCodeUrl = ActionCodeURL.parseLink(actionLink);
expect(actionCodeUrl!.operation).to.eq(
Operation.VERIFY_AND_CHANGE_EMAIL
);
Expand All @@ -73,7 +67,7 @@ describe('core/action_code_url', () => {
'https://www.example.com/finishSignIn?' +
'oobCode=CODE&mode=verifyEmail&apiKey=API_KEY&' +
'languageCode=en';
const actionCodeUrl = ActionCodeURL.parseLink(auth, actionLink);
const actionCodeUrl = ActionCodeURL.parseLink(actionLink);
expect(actionCodeUrl!.operation).to.eq(Operation.VERIFY_EMAIL);
});

Expand All @@ -82,7 +76,7 @@ describe('core/action_code_url', () => {
'https://www.example.com/finishSignIn?' +
'oobCode=CODE&mode=recoverEmail&apiKey=API_KEY&' +
'languageCode=en';
const actionCodeUrl = ActionCodeURL.parseLink(auth, actionLink);
const actionCodeUrl = ActionCodeURL.parseLink(actionLink);
expect(actionCodeUrl!.operation).to.eq(Operation.RECOVER_EMAIL);
});

Expand All @@ -91,7 +85,7 @@ describe('core/action_code_url', () => {
'https://www.example.com/finishSignIn?' +
'oobCode=CODE&mode=resetPassword&apiKey=API_KEY&' +
'languageCode=en';
const actionCodeUrl = ActionCodeURL.parseLink(auth, actionLink);
const actionCodeUrl = ActionCodeURL.parseLink(actionLink);
expect(actionCodeUrl!.operation).to.eq(Operation.PASSWORD_RESET);
});

Expand All @@ -100,7 +94,7 @@ describe('core/action_code_url', () => {
'https://www.example.com/finishSignIn?' +
'oobCode=CODE&mode=revertSecondFactorAddition&apiKey=API_KEY&' +
'languageCode=en';
const actionCodeUrl = ActionCodeURL.parseLink(auth, actionLink);
const actionCodeUrl = ActionCodeURL.parseLink(actionLink);
expect(actionCodeUrl!.operation).to.eq(
Operation.REVERT_SECOND_FACTOR_ADDITION
);
Expand All @@ -111,7 +105,7 @@ describe('core/action_code_url', () => {
const actionLink =
'https://www.example.com:8080/finishSignIn?' +
'oobCode=CODE&mode=signIn&apiKey=API_KEY&state=bla';
const actionCodeUrl = ActionCodeURL.parseLink(auth, actionLink);
const actionCodeUrl = ActionCodeURL.parseLink(actionLink);
expect(actionCodeUrl!.operation).to.eq(Operation.EMAIL_SIGNIN);
expect(actionCodeUrl!.code).to.eq('CODE');
expect(actionCodeUrl!.apiKey).to.eq('API_KEY');
Expand All @@ -125,7 +119,7 @@ describe('core/action_code_url', () => {
'https://www.example.com/finishSignIn?' +
'oobCode=CODE1&mode=signIn&apiKey=API_KEY1&state=bla' +
'#oobCode=CODE2&mode=signIn&apiKey=API_KEY2&state=bla';
const actionCodeUrl = ActionCodeURL.parseLink(auth, actionLink);
const actionCodeUrl = ActionCodeURL.parseLink(actionLink);
expect(actionCodeUrl!.operation).to.eq(Operation.EMAIL_SIGNIN);
expect(actionCodeUrl!.code).to.eq('CODE1');
expect(actionCodeUrl!.apiKey).to.eq('API_KEY1');
Expand All @@ -137,31 +131,31 @@ describe('core/action_code_url', () => {
context('invalid links', () => {
it('should handle missing API key, code & mode', () => {
const actionLink = 'https://www.example.com/finishSignIn';
expect(ActionCodeURL.parseLink(auth, actionLink)).to.be.null;
expect(ActionCodeURL.parseLink(actionLink)).to.be.null;
});

it('should handle invalid mode', () => {
const actionLink =
'https://www.example.com/finishSignIn?oobCode=CODE&mode=INVALID_MODE&apiKey=API_KEY';
expect(ActionCodeURL.parseLink(auth, actionLink)).to.be.null;
expect(ActionCodeURL.parseLink(actionLink)).to.be.null;
});

it('should handle missing code', () => {
const actionLink =
'https://www.example.com/finishSignIn?mode=signIn&apiKey=API_KEY';
expect(ActionCodeURL.parseLink(auth, actionLink)).to.be.null;
expect(ActionCodeURL.parseLink(actionLink)).to.be.null;
});

it('should handle missing API key', () => {
const actionLink =
'https://www.example.com/finishSignIn?oobCode=CODE&mode=signIn';
expect(ActionCodeURL.parseLink(auth, actionLink)).to.be.null;
expect(ActionCodeURL.parseLink(actionLink)).to.be.null;
});

it('should handle missing mode', () => {
const actionLink =
'https://www.example.com/finishSignIn?oobCode=CODE&apiKey=API_KEY';
expect(ActionCodeURL.parseLink(auth, actionLink)).to.be.null;
expect(ActionCodeURL.parseLink(actionLink)).to.be.null;
});
});
});
Expand Down
Loading