Skip to content

add signInWithEmailAndPassword & signInWithEmailLink to auth-next #3209

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
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
93 changes: 93 additions & 0 deletions packages-exp/auth-exp/src/core/credentials/anonymous.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* @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 { ProviderId, SignInMethod } from '@firebase/auth-types-exp';
import * as mockFetch from '../../../test/mock_fetch';
import { expect, use } from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import { testAuth } from '../../../test/mock_auth';
import { Auth } from '../../model/auth';
import { AnonymousCredential } from './anonymous';
import { mockEndpoint } from '../../../test/api/helper';
import { Endpoint } from '../../api';
import { APIUserInfo } from '../../api/account_management/account';

use(chaiAsPromised);

describe('core/credentials/anonymous', () => {
let auth: Auth;
const credential = new AnonymousCredential();

beforeEach(async () => {
auth = await testAuth();
});

it('should have an anonymous provider', () => {
expect(credential.providerId).to.eq(ProviderId.ANONYMOUS);
});

it('should have an anonymous sign in method', () => {
expect(credential.signInMethod).to.eq(SignInMethod.ANONYMOUS);
});

describe('#toJSON', () => {
it('throws', () => {
expect(credential.toJSON).to.throw(Error);
});
});

describe('#_getIdTokenResponse', () => {
const serverUser: APIUserInfo = {
localId: 'local-id'
};

beforeEach(() => {
mockFetch.setUp();
mockEndpoint(Endpoint.SIGN_UP, {
idToken: 'id-token',
refreshToken: 'refresh-token',
expiresIn: '1234',
localId: serverUser.localId!
});
});
afterEach(mockFetch.tearDown);

it('calls signUp', async () => {
const idTokenResponse = await credential._getIdTokenResponse(auth);
expect(idTokenResponse.idToken).to.eq('id-token');
expect(idTokenResponse.refreshToken).to.eq('refresh-token');
expect(idTokenResponse.expiresIn).to.eq('1234');
expect(idTokenResponse.localId).to.eq(serverUser.localId);
});
});

describe('#_linkToIdToken', () => {
it('throws', async () => {
await expect(
credential._linkToIdToken(auth, 'id-token')
).to.be.rejectedWith(Error);
});
});

describe('#_matchIdTokenWithUid', () => {
it('throws', () => {
expect(() =>
credential._matchIdTokenWithUid(auth, 'other-uid')
).to.throw(Error);
});
});
});
53 changes: 53 additions & 0 deletions packages-exp/auth-exp/src/core/credentials/anonymous.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @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 {
ProviderId,
SignInMethod
} from '@firebase/auth-types-exp';
import { signUp } from '../../api/authentication/sign_up';
import { Auth } from '../../model/auth';
import { IdTokenResponse } from '../../model/id_token';
import { debugFail } from '../util/assert';
import { AuthCredential } from '.';

export class AnonymousCredential implements AuthCredential {
providerId = ProviderId.ANONYMOUS;
signInMethod = SignInMethod.ANONYMOUS;

toJSON(): never {
debugFail('Method not implemented.');
}

static fromJSON(_json: object | string): AnonymousCredential | null {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does prefixing it with _ do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

required by linter when parameters are unused

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah we should settle on which thing to do. I was doing void <variable> but your version seems cleaner

debugFail('Method not implemented');
}

async _getIdTokenResponse(auth: Auth): Promise<IdTokenResponse> {
return signUp(auth, {
returnSecureToken: true
});
}

async _linkToIdToken(_auth: Auth, _idToken: string): Promise<never> {
debugFail("Can't link to an anonymous credential");
}

_matchIdTokenWithUid(_auth: Auth, _uid: string): Promise<never> {
debugFail('Method not implemented.');
}
}
161 changes: 161 additions & 0 deletions packages-exp/auth-exp/src/core/credentials/email.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/**
* @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 { ProviderId, SignInMethod } from '@firebase/auth-types-exp';
import { expect, use } from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import { testAuth } from '../../../test/mock_auth';
import { Auth } from '../../model/auth';
import { EmailAuthProvider } from '../providers/email';
import { EmailAuthCredential } from './email';
import * as mockFetch from '../../../test/mock_fetch';
import { mockEndpoint } from '../../../test/api/helper';
import { Endpoint } from '../../api';
import { APIUserInfo } from '../../api/account_management/account';

use(chaiAsPromised);

describe('core/credentials/email', () => {
let auth: Auth;
let apiMock: mockFetch.Route;
const serverUser: APIUserInfo = {
localId: 'local-id'
};

beforeEach(async () => {
auth = await testAuth();
});

context('email & password', () => {
const credential = new EmailAuthCredential('some-email', 'some-password', EmailAuthProvider.PROVIDER_ID, EmailAuthProvider.EMAIL_PASSWORD_SIGN_IN_METHOD);

beforeEach(() => {
mockFetch.setUp();
apiMock = mockEndpoint(Endpoint.SIGN_IN_WITH_PASSWORD, {
idToken: 'id-token',
refreshToken: 'refresh-token',
expiresIn: '1234',
localId: serverUser.localId!
});
});
afterEach(mockFetch.tearDown);

it('should have an email provider', () => {
expect(credential.providerId).to.eq(ProviderId.PASSWORD);
});

it('should have an anonymous sign in method', () => {
expect(credential.signInMethod).to.eq(SignInMethod.EMAIL_PASSWORD);
});

describe('#toJSON', () => {
it('throws', () => {
expect(credential.toJSON).to.throw(Error);
});
});

describe('#_getIdTokenResponse', () => {
it('call sign in with password', async () => {
const idTokenResponse = await credential._getIdTokenResponse(auth);
expect(idTokenResponse.idToken).to.eq('id-token');
expect(idTokenResponse.refreshToken).to.eq('refresh-token');
expect(idTokenResponse.expiresIn).to.eq('1234');
expect(idTokenResponse.localId).to.eq(serverUser.localId);
expect(apiMock.calls[0].request).to.eql({
returnSecureToken: true,
email: 'some-email',
password: 'some-password'
});
});
});

describe('#_linkToIdToken', () => {
it('throws', async () => {
await expect(
credential._linkToIdToken(auth, 'id-token')
).to.be.rejectedWith(Error);
});
});

describe('#_matchIdTokenWithUid', () => {
it('throws', () => {
expect(() =>
credential._matchIdTokenWithUid(auth, 'other-uid')
).to.throw(Error);
});
});
});

context('email link', () => {
const credential = new EmailAuthCredential('some-email', 'oob-code', EmailAuthProvider.PROVIDER_ID, EmailAuthProvider.EMAIL_LINK_SIGN_IN_METHOD);

beforeEach(() => {
mockFetch.setUp();
apiMock = mockEndpoint(Endpoint.SIGN_IN_WITH_EMAIL_LINK, {
idToken: 'id-token',
refreshToken: 'refresh-token',
expiresIn: '1234',
localId: serverUser.localId!
});
});
afterEach(mockFetch.tearDown);

it('should have an email provider', () => {
expect(credential.providerId).to.eq(ProviderId.PASSWORD);
});

it('should have an anonymous sign in method', () => {
expect(credential.signInMethod).to.eq(SignInMethod.EMAIL_LINK);
});

describe('#toJSON', () => {
it('throws', () => {
expect(credential.toJSON).to.throw(Error);
});
});

describe('#_getIdTokenResponse', () => {
it('call sign in with email link', async () => {
const idTokenResponse = await credential._getIdTokenResponse(auth);
expect(idTokenResponse.idToken).to.eq('id-token');
expect(idTokenResponse.refreshToken).to.eq('refresh-token');
expect(idTokenResponse.expiresIn).to.eq('1234');
expect(idTokenResponse.localId).to.eq(serverUser.localId);
expect(apiMock.calls[0].request).to.eql({
email: 'some-email',
oobCode: 'oob-code'
});
});
});

describe('#_linkToIdToken', () => {
it('throws', async () => {
await expect(
credential._linkToIdToken(auth, 'id-token')
).to.be.rejectedWith(Error);
});
});

describe('#_matchIdTokenWithUid', () => {
it('throws', () => {
expect(() =>
credential._matchIdTokenWithUid(auth, 'other-uid')
).to.throw(Error);
});
});
});
});
71 changes: 71 additions & 0 deletions packages-exp/auth-exp/src/core/credentials/email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* @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 * as externs from '@firebase/auth-types-exp';
import { signInWithPassword } from '../../api/authentication/email_and_password';
import { signInWithEmailLink } from '../../api/authentication/email_link';
import { Auth } from '../../model/auth';
import { IdTokenResponse } from '../../model/id_token';
import { AuthErrorCode, AUTH_ERROR_FACTORY } from '../errors';
import { EmailAuthProvider } from '../providers/email';
import { debugFail } from '../util/assert';
import { AuthCredential } from '.';

export class EmailAuthCredential implements AuthCredential {
constructor(
readonly email: string,
readonly password: string,
readonly providerId: typeof EmailAuthProvider.PROVIDER_ID,
readonly signInMethod: externs.SignInMethod
) {}

toJSON(): never {
debugFail('Method not implemented.');
}

static fromJSON(_json: object | string): EmailAuthCredential | null {
debugFail('Method not implemented');
}

async _getIdTokenResponse(auth: Auth): Promise<IdTokenResponse> {
switch (this.signInMethod) {
case EmailAuthProvider.EMAIL_PASSWORD_SIGN_IN_METHOD:
return signInWithPassword(auth, {
returnSecureToken: true,
email: this.email,
password: this.password
});
case EmailAuthProvider.EMAIL_LINK_SIGN_IN_METHOD:
return signInWithEmailLink(auth, {
email: this.email,
oobCode: this.password
});
default:
throw AUTH_ERROR_FACTORY.create(AuthErrorCode.INTERNAL_ERROR, {
appName: auth.name
});
}
}

async _linkToIdToken(_auth: Auth, _idToken: string): Promise<never> {
debugFail('Method not implemented.');
}

_matchIdTokenWithUid(_auth: Auth, _uid: string): Promise<never> {
debugFail('Method not implemented.');
}
}
Loading