Skip to content

Add sendEmailVerification to auth-next #2926

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
Apr 20, 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
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import * as mockFetch from '../../../test/mock_fetch';
import { ServerError } from '../errors';
import {
EmailSignInRequest,
GetOobCodeRequestType,
PasswordResetRequest,
sendEmailVerification,
sendPasswordResetEmail,
sendSignInLinkToEmail,
signInWithPassword,
VerifyEmailRequest
} from './email_and_password';
import { Operation } from '../../model/action_code_info';

use(chaiAsPromised);

Expand Down Expand Up @@ -91,7 +91,7 @@ describe('signInWithPassword', () => {
describe('sendOobCode', () => {
context('VERIFY_EMAIL', () => {
const request: VerifyEmailRequest = {
requestType: GetOobCodeRequestType.VERIFY_EMAIL,
requestType: Operation.VERIFY_EMAIL,
idToken: 'my-token'
};

Expand Down Expand Up @@ -140,7 +140,7 @@ describe('sendOobCode', () => {

context('PASSWORD_RESET', () => {
const request: PasswordResetRequest = {
requestType: GetOobCodeRequestType.PASSWORD_RESET,
requestType: Operation.PASSWORD_RESET,
email: '[email protected]'
};

Expand Down Expand Up @@ -191,7 +191,7 @@ describe('sendOobCode', () => {

context('EMAIL_SIGNIN', () => {
const request: EmailSignInRequest = {
requestType: GetOobCodeRequestType.EMAIL_SIGNIN,
requestType: Operation.EMAIL_SIGNIN,
email: '[email protected]'
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from '..';
import { Auth } from '../../model/auth';
import { IdToken, IdTokenResponse } from '../../model/id_token';
import { Operation } from '../../model/action_code_info';

export interface SignInWithPasswordRequest {
returnSecureToken?: boolean;
Expand All @@ -45,14 +46,7 @@ export async function signInWithPassword(
>(auth, HttpMethod.POST, Endpoint.SIGN_IN_WITH_PASSWORD, request);
}

export enum GetOobCodeRequestType {
PASSWORD_RESET = 'PASSWORD_RESET',
EMAIL_SIGNIN = 'EMAIL_SIGNIN',
VERIFY_EMAIL = 'VERIFY_EMAIL',
VERIFY_AND_CHANGE_EMAIL = 'VERIFY_AND_CHANGE_EMAIL'
}

interface GetOobCodeRequest {
export interface GetOobCodeRequest {
email?: string; // Everything except VERIFY_AND_CHANGE_EMAIL
continueUrl?: string;
iosBundleId?: string;
Expand All @@ -67,19 +61,19 @@ interface GetOobCodeRequest {
}

export interface VerifyEmailRequest extends GetOobCodeRequest {
requestType: GetOobCodeRequestType.VERIFY_EMAIL;
requestType: Operation.VERIFY_EMAIL;
idToken: IdToken;
}

export interface PasswordResetRequest extends GetOobCodeRequest {
requestType: GetOobCodeRequestType.PASSWORD_RESET;
requestType: Operation.PASSWORD_RESET;
email: string;
captchaResp?: string;
userIp?: string;
}

export interface EmailSignInRequest extends GetOobCodeRequest {
requestType: GetOobCodeRequestType.EMAIL_SIGNIN;
requestType: Operation.EMAIL_SIGNIN;
email: string;
}

Expand Down
116 changes: 113 additions & 3 deletions packages-exp/auth-exp/src/core/strategies/email.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@
import { FirebaseError } from '@firebase/util';
import { expect, use } from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import { SinonStub, stub } from 'sinon';
import * as sinonChai from 'sinon-chai';
import { SinonStub, stub, restore } from 'sinon';
import { mockEndpoint } from '../../../test/api/helper';
import { mockAuth } from '../../../test/mock_auth';
import { mockAuth, testUser } from '../../../test/mock_auth';
import * as mockFetch from '../../../test/mock_fetch';
import { Endpoint } from '../../api';
import { ServerError } from '../../api/errors';
import { ProviderId } from '../providers';
import * as location from '../util/location';
import { fetchSignInMethodsForEmail } from './email';
import { fetchSignInMethodsForEmail, sendEmailVerification } from './email';
import { Operation } from '../../model/action_code_info';

use(chaiAsPromised);
use(sinonChai);

describe('fetchSignInMethodsForEmail', () => {
const email = '[email protected]';
Expand Down Expand Up @@ -94,3 +97,110 @@ describe('fetchSignInMethodsForEmail', () => {
expect(mock.calls.length).to.eq(1);
});
});

describe('sendEmailVerification', () => {
const email = '[email protected]';
const user = testUser('my-user-uid', email);
const idToken = 'id-token';
let idTokenStub: SinonStub;
let reloadStub: SinonStub;

beforeEach(() => {
mockFetch.setUp();
idTokenStub = stub(user, 'getIdToken');
idTokenStub.callsFake(async () => idToken);
reloadStub = stub(user, 'reload');
});

afterEach(() => {
mockFetch.tearDown();
restore();
});

it('should send the email verification', async () => {
const mock = mockEndpoint(Endpoint.SEND_OOB_CODE, {
requestType: Operation.VERIFY_EMAIL,
email
});

await sendEmailVerification(mockAuth, user);

expect(reloadStub).to.not.have.been.called;
expect(mock.calls[0].request).to.eql({
requestType: Operation.VERIFY_EMAIL,
idToken
});
});

it('should reload the user if the API returns a different email', async () => {
const mock = mockEndpoint(Endpoint.SEND_OOB_CODE, {
requestType: Operation.VERIFY_EMAIL,
email: '[email protected]'
});

await sendEmailVerification(mockAuth, user);

expect(reloadStub).to.have.been.calledOnce;
expect(mock.calls[0].request).to.eql({
requestType: Operation.VERIFY_EMAIL,
idToken
});
});

context('on iOS', () => {
it('should pass action code parameters', async () => {
const mock = mockEndpoint(Endpoint.SEND_OOB_CODE, {
requestType: Operation.VERIFY_EMAIL,
email
});
await sendEmailVerification(mockAuth, user, {
handleCodeInApp: true,
iOS: {
bundleId: 'my-bundle',
appStoreId: 'my-appstore-id'
},
url: 'my-url',
dynamicLinkDomain: 'fdl-domain'
});

expect(mock.calls[0].request).to.eql({
requestType: Operation.VERIFY_EMAIL,
idToken,
continueUrl: 'my-url',
dynamicLinkDomain: 'fdl-domain',
canHandleCodeInApp: true,
iosBundleId: 'my-bundle',
iosAppStoreId: 'my-appstore-id'
});
});
});

context('on Android', () => {
it('should pass action code parameters', async () => {
const mock = mockEndpoint(Endpoint.SEND_OOB_CODE, {
requestType: Operation.VERIFY_EMAIL,
email
});
await sendEmailVerification(mockAuth, user, {
handleCodeInApp: true,
android: {
installApp: false,
minimumVersion: 'my-version',
packageName: 'my-package'
},
url: 'my-url',
dynamicLinkDomain: 'fdl-domain'
});
expect(mock.calls[0].request).to.eql({
requestType: Operation.VERIFY_EMAIL,
idToken,
continueUrl: 'my-url',
dynamicLinkDomain: 'fdl-domain',
canHandleCodeInApp: true,
androidInstallApp: false,
androidMinimumVersionCode: 'my-version',
androidPackageName: 'my-package'
});
});
});
});
30 changes: 26 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,11 +15,12 @@
* limitations under the License.
*/

import {
createAuthUri,
CreateAuthUriRequest
} from '../../api/authentication/create_auth_uri';
import { createAuthUri, CreateAuthUriRequest } from '../../api/authentication/create_auth_uri';
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 { Auth } from '../../model/auth';
import { User } from '../../model/user';
import { getCurrentUrl, isHttpOrHttps } from '../util/location';

export async function fetchSignInMethodsForEmail(
Expand All @@ -39,3 +40,24 @@ export async function fetchSignInMethodsForEmail(

return signinMethods || [];
}

export async function sendEmailVerification(
auth: Auth,
user: User,
actionCodeSettings?: ActionCodeSettings
): Promise<void> {
const idToken = await user.getIdToken();
const request: api.VerifyEmailRequest = {
requestType: Operation.VERIFY_EMAIL,
idToken
};
if (actionCodeSettings) {
setActionCodeSettingsOnRequest(request, actionCodeSettings);
}

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

if (email !== user.email) {
await user.reload();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,14 @@ export enum Operation {
PASSWORD_RESET = 'PASSWORD_RESET',
RECOVER_EMAIL = 'RECOVER_EMAIL',
EMAIL_SIGNIN = 'EMAIL_SIGNIN',
VERIFY_EMAIL = 'VERIFY_EMAIL'
VERIFY_EMAIL = 'VERIFY_EMAIL',
VERIFY_AND_CHANGE_EMAIL = 'VERIFY_AND_CHANGE_EMAIL'
}

export interface ActionCodeInfo {
data: {
email: string | null;
fromEmail: string | null;
};
operation: string;
}
54 changes: 54 additions & 0 deletions packages-exp/auth-exp/src/model/action_code_settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { GetOobCodeRequest } from '../api/authentication/email_and_password';

export interface ActionCodeSettings {
android?: {
installApp?: boolean;
minimumVersion?: string;
packageName: string;
};
handleCodeInApp?: boolean;
iOS?: {
bundleId: string;
appStoreId: string;
};
url: string;
dynamicLinkDomain?: string;
}

export function setActionCodeSettingsOnRequest(
request: GetOobCodeRequest,
actionCodeSettings: ActionCodeSettings
): void {
request.continueUrl = actionCodeSettings.url;
request.dynamicLinkDomain = actionCodeSettings.dynamicLinkDomain;
request.canHandleCodeInApp = actionCodeSettings.handleCodeInApp;

if (actionCodeSettings.iOS) {
request.iosBundleId = actionCodeSettings.iOS.bundleId;
request.iosAppStoreId = actionCodeSettings.iOS.appStoreId;
}

if (actionCodeSettings.android) {
request.androidInstallApp = actionCodeSettings.android.installApp;
request.androidMinimumVersionCode =
actionCodeSettings.android.minimumVersion;
request.androidPackageName = actionCodeSettings.android.packageName;
}
}
5 changes: 3 additions & 2 deletions packages-exp/auth-exp/test/mock_auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ export const mockAuth: Auth = {
}
};

export function testUser(uid: string): User {
export function testUser(uid: string, email?: string): User {
return new UserImpl({
uid,
auth: mockAuth,
stsTokenManager: new StsTokenManager()
stsTokenManager: new StsTokenManager(),
email
});
}