Skip to content

Commit fa2b37c

Browse files
committed
Add createUserWithEmailAndPassword to auth-next (#3212)
* Add createUserWithEmailAndPassword to auth-next * PR feedback
1 parent 825adcf commit fa2b37c

File tree

3 files changed

+78
-13
lines changed

3 files changed

+78
-13
lines changed

packages-exp/auth-exp/src/core/strategies/email_and_password.test.ts

+51-13
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,31 @@
1515
* limitations under the License.
1616
*/
1717

18+
import {
19+
OperationType,
20+
ProviderId,
21+
SignInMethod
22+
} from '@firebase/auth-types-exp';
23+
import { FirebaseError } from '@firebase/util';
1824
import { expect, use } from 'chai';
1925
import * as chaiAsPromised from 'chai-as-promised';
2026
import * as sinonChai from 'sinon-chai';
21-
22-
import { FirebaseError } from '@firebase/util';
23-
2427
import { mockEndpoint } from '../../../test/api/helper';
2528
import { testAuth } from '../../../test/mock_auth';
2629
import * as mockFetch from '../../../test/mock_fetch';
2730
import { Endpoint } from '../../api';
31+
import { APIUserInfo } from '../../api/account_management/account';
2832
import { ServerError } from '../../api/errors';
2933
import { Operation } from '../../model/action_code_info';
3034
import { Auth } from '../../model/auth';
3135
import {
3236
checkActionCode,
3337
confirmPasswordReset,
38+
createUserWithEmailAndPassword,
3439
sendPasswordResetEmail,
35-
verifyPasswordResetCode,
36-
signInWithEmailAndPassword
40+
signInWithEmailAndPassword,
41+
verifyPasswordResetCode
3742
} from './email_and_password';
38-
import { APIUserInfo } from '../../api/account_management/account';
39-
import {
40-
SignInMethod,
41-
OperationType,
42-
ProviderId
43-
} from '@firebase/auth-types-exp';
4443

4544
use(chaiAsPromised);
4645
use(sinonChai);
@@ -324,6 +323,45 @@ describe('core/strategies/verifyPasswordResetCode', () => {
324323
});
325324
});
326325

326+
describe('core/strategies/email_and_password/createUserWithEmailAndPassword', () => {
327+
let auth: Auth;
328+
const serverUser: APIUserInfo = {
329+
localId: 'local-id'
330+
};
331+
332+
beforeEach(async () => {
333+
auth = await testAuth();
334+
mockFetch.setUp();
335+
mockEndpoint(Endpoint.SIGN_UP, {
336+
idToken: 'id-token',
337+
refreshToken: 'refresh-token',
338+
expiresIn: '1234',
339+
localId: serverUser.localId!
340+
});
341+
mockEndpoint(Endpoint.GET_ACCOUNT_INFO, {
342+
users: [serverUser]
343+
});
344+
});
345+
afterEach(mockFetch.tearDown);
346+
347+
it('should sign in the user', async () => {
348+
const {
349+
credential,
350+
user,
351+
operationType
352+
} = await createUserWithEmailAndPassword(
353+
auth,
354+
'some-email',
355+
'some-password'
356+
);
357+
expect(credential!.providerId).to.eq(ProviderId.PASSWORD);
358+
expect(credential!.signInMethod).to.eq(SignInMethod.EMAIL_PASSWORD);
359+
expect(operationType).to.eq(OperationType.SIGN_IN);
360+
expect(user.uid).to.eq(serverUser.localId);
361+
expect(user.isAnonymous).to.be.false;
362+
});
363+
});
364+
327365
describe('core/strategies/email_and_password/signInWithEmailAndPassword', () => {
328366
let auth: Auth;
329367
const serverUser: APIUserInfo = {
@@ -351,8 +389,8 @@ describe('core/strategies/email_and_password/signInWithEmailAndPassword', () =>
351389
user,
352390
operationType
353391
} = await signInWithEmailAndPassword(auth, 'some-email', 'some-password');
354-
expect(credential?.providerId).to.eq(ProviderId.PASSWORD);
355-
expect(credential?.signInMethod).to.eq(SignInMethod.EMAIL_PASSWORD);
392+
expect(credential!.providerId).to.eq(ProviderId.PASSWORD);
393+
expect(credential!.signInMethod).to.eq(SignInMethod.EMAIL_PASSWORD);
356394
expect(operationType).to.eq(OperationType.SIGN_IN);
357395
expect(user.uid).to.eq(serverUser.localId);
358396
expect(user.isAnonymous).to.be.false;

packages-exp/auth-exp/src/core/strategies/email_and_password.ts

+26
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import { AuthErrorCode, AUTH_ERROR_FACTORY } from '../errors';
2525
import { EmailAuthProvider } from '../providers/email';
2626
import { setActionCodeSettingsOnRequest } from './action_code_settings';
2727
import { signInWithCredential } from './credential';
28+
import { UserCredentialImpl } from '../user/user_credential_impl';
29+
import { signUp } from '../../api/authentication/sign_up';
2830

2931
export async function sendPasswordResetEmail(
3032
auth: externs.Auth,
@@ -85,6 +87,30 @@ export async function verifyPasswordResetCode(
8587
return data.email!;
8688
}
8789

90+
export async function createUserWithEmailAndPassword(
91+
authExtern: externs.Auth,
92+
email: string,
93+
password: string
94+
): Promise<externs.UserCredential> {
95+
const auth = authExtern as Auth;
96+
97+
const response = await signUp(auth, {
98+
returnSecureToken: true,
99+
email,
100+
password
101+
});
102+
103+
const userCredential = await UserCredentialImpl._fromIdTokenResponse(
104+
auth,
105+
EmailAuthProvider.credential(email, password),
106+
externs.OperationType.SIGN_IN,
107+
response
108+
);
109+
await auth.updateCurrentUser(userCredential.user);
110+
111+
return userCredential;
112+
}
113+
88114
export function signInWithEmailAndPassword(
89115
auth: externs.Auth,
90116
email: string,

packages-exp/auth-exp/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export {
4343
confirmPasswordReset,
4444
checkActionCode,
4545
verifyPasswordResetCode,
46+
createUserWithEmailAndPassword,
4647
signInWithEmailAndPassword
4748
} from './core/strategies/email_and_password';
4849
export {

0 commit comments

Comments
 (0)