Skip to content

Commit 6d93966

Browse files
committed
Add remaining API methods to auth-next
1 parent ddf9e69 commit 6d93966

21 files changed

+1584
-2
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { Auth } from '../../model/auth';
19+
import { performApiRequest, HttpMethod, Endpoint } from '..';
20+
import { APIMFAInfo } from '../../model/id_token';
21+
22+
export interface DeleteAccountRequest {
23+
idToken: string;
24+
}
25+
26+
export async function deleteAccount(
27+
auth: Auth,
28+
request: DeleteAccountRequest
29+
): Promise<void> {
30+
return performApiRequest<DeleteAccountRequest, void>(
31+
auth,
32+
HttpMethod.POST,
33+
Endpoint.DELETE_ACCOUNT,
34+
request
35+
);
36+
}
37+
38+
export interface DeleteLinkedAccountsRequest {
39+
idToken: string;
40+
deleteProvider: string[];
41+
}
42+
43+
export interface DeleteLinkedAccountsResponse {
44+
providerUserInfo: ProviderUserInfo[];
45+
}
46+
47+
export async function deleteLinkedAccounts(
48+
auth: Auth,
49+
request: DeleteLinkedAccountsRequest
50+
): Promise<DeleteLinkedAccountsResponse> {
51+
return performApiRequest<
52+
DeleteLinkedAccountsRequest,
53+
DeleteLinkedAccountsResponse
54+
>(auth, HttpMethod.POST, Endpoint.SET_ACCOUNT_INFO, request);
55+
}
56+
57+
export interface APIUserInfo {
58+
localId?: string;
59+
displayName?: string;
60+
photoUrl?: string;
61+
email?: string;
62+
emailVerified?: boolean;
63+
phoneNumber?: string;
64+
lastLoginAt?: number;
65+
createdAt?: number;
66+
tenantId?: string;
67+
passwordHash?: string;
68+
providerUserInfo: ProviderUserInfo[];
69+
mfaInfo?: APIMFAInfo[];
70+
}
71+
72+
export interface ProviderUserInfo {
73+
rawId?: string;
74+
providerId?: string;
75+
email?: string;
76+
displayName?: string;
77+
photoUrl?: string;
78+
phoneNumber?: string;
79+
}
80+
81+
export interface GetAccountInfoRequest {
82+
idToken: string;
83+
}
84+
85+
export interface GetAccountInfoResponse {
86+
users: APIUserInfo[];
87+
}
88+
89+
export async function getAccountInfo(
90+
auth: Auth,
91+
request: GetAccountInfoRequest
92+
): Promise<GetAccountInfoResponse> {
93+
return performApiRequest<GetAccountInfoRequest, GetAccountInfoResponse>(
94+
auth,
95+
HttpMethod.POST,
96+
Endpoint.GET_ACCOUNT_INFO,
97+
request
98+
);
99+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { performApiRequest, Endpoint, HttpMethod } from '..';
2+
import { Auth } from '../../model/auth';
3+
import { IdTokenResponse } from '../../model/id_token';
4+
import { Operation } from '../../model/action_code_info';
5+
6+
export interface ResetPasswordRequest {
7+
oobCode: string;
8+
newPassword?: string;
9+
}
10+
11+
export interface ResetPasswordResponse {
12+
email: string;
13+
newEmail?: string;
14+
requestType?: Operation;
15+
}
16+
17+
export async function resetPassword(
18+
auth: Auth,
19+
request: ResetPasswordRequest
20+
): Promise<ResetPasswordResponse> {
21+
return performApiRequest<ResetPasswordRequest, ResetPasswordResponse>(
22+
auth,
23+
HttpMethod.POST,
24+
Endpoint.RESET_PASSWORD,
25+
request
26+
);
27+
}
28+
export interface UpdateEmailPasswordRequest {
29+
idToken: string;
30+
returnSecureToken?: boolean;
31+
email?: string;
32+
password?: string;
33+
}
34+
35+
export interface UpdateEmailPasswordResponse extends IdTokenResponse { }
36+
37+
export async function updateEmailPassword(
38+
auth: Auth,
39+
request: UpdateEmailPasswordRequest
40+
): Promise<UpdateEmailPasswordResponse> {
41+
return performApiRequest<
42+
UpdateEmailPasswordRequest,
43+
UpdateEmailPasswordResponse
44+
>(auth, HttpMethod.POST, Endpoint.SET_ACCOUNT_INFO, request);
45+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { Endpoint, HttpMethod, performApiRequest } from '..';
2+
import { SignInWithPhoneNumberRequest } from '../authentication/sms';
3+
import { IdTokenResponse } from '../../model/id_token';
4+
import { Auth } from '../../model/auth';
5+
6+
export interface StartPhoneMfaEnrollmentRequest {
7+
idToken: string;
8+
9+
phoneEnrollmentInfo: {
10+
phoneNumber: string;
11+
recaptchaToken: string;
12+
};
13+
}
14+
15+
export interface StartPhoneMfaEnrollmentResponse {
16+
phoneSessionInfo: {
17+
sessionInfo: string;
18+
};
19+
}
20+
21+
export function startEnrollPhoneMfa(
22+
auth: Auth,
23+
request: StartPhoneMfaEnrollmentRequest
24+
): Promise<StartPhoneMfaEnrollmentResponse> {
25+
return performApiRequest<
26+
StartPhoneMfaEnrollmentRequest,
27+
StartPhoneMfaEnrollmentResponse
28+
>(auth, HttpMethod.POST, Endpoint.START_PHONE_MFA_ENROLLMENT, request);
29+
}
30+
31+
export interface PhoneMfaEnrollmentRequest {
32+
phoneVerificationInfo: SignInWithPhoneNumberRequest;
33+
}
34+
35+
export interface PhoneMfaEnrollmentResponse extends IdTokenResponse { }
36+
37+
export function enrollPhoneMfa(
38+
auth: Auth,
39+
request: PhoneMfaEnrollmentRequest
40+
): Promise<PhoneMfaEnrollmentResponse> {
41+
return performApiRequest<
42+
PhoneMfaEnrollmentRequest,
43+
PhoneMfaEnrollmentResponse
44+
>(auth, HttpMethod.POST, Endpoint.FINALIZE_PHONE_MFA_ENROLLMENT, request);
45+
}
46+
47+
export interface WithdrawMfaRequest {
48+
idToken: string;
49+
mfaEnrollmentId: string;
50+
}
51+
52+
export interface WithdrawMfaResponse extends IdTokenResponse { }
53+
54+
export function withdrawMfa(
55+
auth: Auth,
56+
request: WithdrawMfaRequest
57+
): Promise<WithdrawMfaResponse> {
58+
return performApiRequest<WithdrawMfaRequest, WithdrawMfaResponse>(
59+
auth,
60+
HttpMethod.POST,
61+
Endpoint.WITHDRAW_MFA,
62+
request
63+
);
64+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { IdTokenResponse } from '../../model/id_token';
2+
import { Auth } from '../../model/auth';
3+
import { performApiRequest, HttpMethod, Endpoint } from '..';
4+
5+
export interface UpdateProfileRequest {
6+
idToken: string;
7+
displayName?: string | null;
8+
photoUrl?: string | null;
9+
}
10+
11+
export interface UpdateProfileResponse extends IdTokenResponse {
12+
displayName?: string | null;
13+
photoUrl?: string | null;
14+
}
15+
16+
export async function updateProfile(
17+
auth: Auth,
18+
request: UpdateProfileRequest
19+
): Promise<UpdateProfileResponse> {
20+
return performApiRequest<UpdateProfileRequest, UpdateProfileResponse>(
21+
auth,
22+
HttpMethod.POST,
23+
Endpoint.SET_ACCOUNT_INFO,
24+
request
25+
);
26+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { expect, use } from 'chai';
19+
import * as chaiAsPromised from 'chai-as-promised';
20+
import { createAuthUri } from './create_auth_uri';
21+
import { Endpoint } from '..';
22+
import { ServerError } from '../errors';
23+
import { FirebaseError } from '@firebase/util';
24+
import * as mockFetch from '../../../test/mock_fetch';
25+
import { mockEndpoint, mockAuth } from '../../../test/api/helper';
26+
27+
use(chaiAsPromised);
28+
29+
describe('createAuthUri', () => {
30+
const request = {
31+
identifier: 'my-id',
32+
continueUri: 'example.com/redirectUri'
33+
};
34+
35+
beforeEach(mockFetch.setUp);
36+
afterEach(mockFetch.tearDown);
37+
38+
it('should POST to the correct endpoint', async () => {
39+
const mock = mockEndpoint(Endpoint.CREATE_AUTH_URI, {
40+
signinMethods: ['email']
41+
});
42+
43+
const response = await createAuthUri(mockAuth, request);
44+
expect(response.signinMethods).to.include('email');
45+
expect(mock.calls[0].request).to.eql(request);
46+
expect(mock.calls[0].method).to.eq('POST');
47+
expect(mock.calls[0].headers).to.eql({
48+
'Content-Type': 'application/json'
49+
});
50+
});
51+
52+
it('should handle errors', async () => {
53+
const mock = mockEndpoint(
54+
Endpoint.CREATE_AUTH_URI,
55+
{
56+
error: {
57+
code: 400,
58+
message: ServerError.INVALID_PROVIDER_ID,
59+
errors: [
60+
{
61+
message: ServerError.INVALID_PROVIDER_ID
62+
}
63+
]
64+
}
65+
},
66+
400
67+
);
68+
69+
await expect(createAuthUri(mockAuth, request)).to.be.rejectedWith(
70+
FirebaseError,
71+
'Firebase: The specified provider ID is invalid. (auth/invalid-provider-id).'
72+
);
73+
expect(mock.calls[0].request).to.eql(request);
74+
});
75+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Auth } from '../../model/auth';
2+
import { performApiRequest, HttpMethod, Endpoint } from '..';
3+
4+
export interface CreateAuthUriRequest {
5+
identifier: string;
6+
continueUri: string;
7+
}
8+
9+
export interface CreateAuthUriResponse {
10+
signinMethods: string[];
11+
}
12+
13+
export async function createAuthUri(
14+
auth: Auth,
15+
request: CreateAuthUriRequest
16+
): Promise<CreateAuthUriResponse> {
17+
return performApiRequest<CreateAuthUriRequest, CreateAuthUriResponse>(
18+
auth,
19+
HttpMethod.POST,
20+
Endpoint.CREATE_AUTH_URI,
21+
request
22+
);
23+
}

0 commit comments

Comments
 (0)