Skip to content

Add createUserWithEmailAndPassword to auth-next #3212

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 2 commits into from
Jun 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,31 @@
* limitations under the License.
*/

import {
OperationType,
ProviderId,
SignInMethod
} from '@firebase/auth-types-exp';
import { FirebaseError } from '@firebase/util';
import { expect, use } from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as sinonChai from 'sinon-chai';

import { FirebaseError } from '@firebase/util';

import { mockEndpoint } from '../../../test/api/helper';
import { testAuth } from '../../../test/mock_auth';
import * as mockFetch from '../../../test/mock_fetch';
import { Endpoint } from '../../api';
import { APIUserInfo } from '../../api/account_management/account';
import { ServerError } from '../../api/errors';
import { Operation } from '../../model/action_code_info';
import { Auth } from '../../model/auth';
import {
checkActionCode,
confirmPasswordReset,
createUserWithEmailAndPassword,
sendPasswordResetEmail,
verifyPasswordResetCode,
signInWithEmailAndPassword
signInWithEmailAndPassword,
verifyPasswordResetCode
} from './email_and_password';
import { APIUserInfo } from '../../api/account_management/account';
import {
SignInMethod,
OperationType,
ProviderId
} from '@firebase/auth-types-exp';

use(chaiAsPromised);
use(sinonChai);
Expand Down Expand Up @@ -324,6 +323,45 @@ describe('core/strategies/verifyPasswordResetCode', () => {
});
});

describe('core/strategies/email_and_password/createUserWithEmailAndPassword', () => {
let auth: Auth;
const serverUser: APIUserInfo = {
localId: 'local-id'
};

beforeEach(async () => {
auth = await testAuth();
mockFetch.setUp();
mockEndpoint(Endpoint.SIGN_UP, {
idToken: 'id-token',
refreshToken: 'refresh-token',
expiresIn: '1234',
localId: serverUser.localId!
});
mockEndpoint(Endpoint.GET_ACCOUNT_INFO, {
users: [serverUser]
});
});
afterEach(mockFetch.tearDown);

it('should sign in the user', async () => {
const {
credential,
user,
operationType
} = await createUserWithEmailAndPassword(
auth,
'some-email',
'some-password'
);
expect(credential!.providerId).to.eq(ProviderId.PASSWORD);
expect(credential!.signInMethod).to.eq(SignInMethod.EMAIL_PASSWORD);
expect(operationType).to.eq(OperationType.SIGN_IN);
expect(user.uid).to.eq(serverUser.localId);
expect(user.isAnonymous).to.be.false;
});
});

describe('core/strategies/email_and_password/signInWithEmailAndPassword', () => {
let auth: Auth;
const serverUser: APIUserInfo = {
Expand Down Expand Up @@ -351,8 +389,8 @@ describe('core/strategies/email_and_password/signInWithEmailAndPassword', () =>
user,
operationType
} = await signInWithEmailAndPassword(auth, 'some-email', 'some-password');
expect(credential?.providerId).to.eq(ProviderId.PASSWORD);
expect(credential?.signInMethod).to.eq(SignInMethod.EMAIL_PASSWORD);
expect(credential!.providerId).to.eq(ProviderId.PASSWORD);
expect(credential!.signInMethod).to.eq(SignInMethod.EMAIL_PASSWORD);
expect(operationType).to.eq(OperationType.SIGN_IN);
expect(user.uid).to.eq(serverUser.localId);
expect(user.isAnonymous).to.be.false;
Expand Down
26 changes: 26 additions & 0 deletions packages-exp/auth-exp/src/core/strategies/email_and_password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { AuthErrorCode, AUTH_ERROR_FACTORY } from '../errors';
import { EmailAuthProvider } from '../providers/email';
import { setActionCodeSettingsOnRequest } from './action_code_settings';
import { signInWithCredential } from './credential';
import { UserCredentialImpl } from '../user/user_credential_impl';
import { signUp } from '../../api/authentication/sign_up';

export async function sendPasswordResetEmail(
auth: externs.Auth,
Expand Down Expand Up @@ -85,6 +87,30 @@ export async function verifyPasswordResetCode(
return data.email!;
}

export async function createUserWithEmailAndPassword(
authExtern: externs.Auth,
email: string,
password: string
): Promise<externs.UserCredential> {
const auth = authExtern as Auth;

const response = await signUp(auth, {
returnSecureToken: true,
email,
password
});

const userCredential = await UserCredentialImpl._fromIdTokenResponse(
auth,
EmailAuthProvider.credential(email, password),
externs.OperationType.SIGN_IN,
response
);
await auth.updateCurrentUser(userCredential.user);

return userCredential;
}

export function signInWithEmailAndPassword(
auth: externs.Auth,
email: string,
Expand Down
1 change: 1 addition & 0 deletions packages-exp/auth-exp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export {
confirmPasswordReset,
checkActionCode,
verifyPasswordResetCode,
createUserWithEmailAndPassword,
signInWithEmailAndPassword
} from './core/strategies/email_and_password';
export {
Expand Down