Skip to content
Closed
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
43 changes: 43 additions & 0 deletions src/auth/auth-api-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,30 @@ export const FIREBASE_AUTH_SIGN_UP_NEW_USER = new ApiSettings('signupNewUser', '
'INTERNAL ASSERT FAILED: Unable to create new user');
}
});
/**
* Instantiates the getOobConfirmationCode endpoint settings for sending a verification email.
*/
export const FIREBASE_AUTH_GET_OOB_CONFIRMATION_CODE = new ApiSettings('getOobConfirmationCode', 'POST')
// Set request validator.
.setRequestValidator((request: any) => {
// requestType is required
if (typeof request.requestType === 'undefined') {
throw new FirebaseAuthError(
AuthClientErrorCode.INTERNAL_ERROR,
'INTERNAL ASSERT FAILED: Server request is missing request type');
}
});

export const FIREBASE_AUTH_VERIFY_CUSTOM_TOKEN = new ApiSettings('verifyCustomToken', 'POST')
// Set request validator.
.setRequestValidator((request: any) => {
// token is required
if (typeof request.token === 'undefined') {
throw new FirebaseAuthError(
AuthClientErrorCode.INTERNAL_ERROR,
'INTERNAL ASSERT FAILED: Server request is missing custom token');
}
});

/**
* Class that provides mechanism to send requests to the Firebase Auth backend endpoints.
Expand Down Expand Up @@ -595,6 +619,24 @@ export class FirebaseAuthRequestHandler {
});
}

public sendEmailVerification(idToken: string): Promise<object> {
const request = {
requestType: 'VERIFY_EMAIL',
idToken,
};

return this.invokeRequestHandler(FIREBASE_AUTH_GET_OOB_CONFIRMATION_CODE, request);
}

public verifyCustomToken(customToken: string): Promise<any> {
const request = {
token: customToken,
returnSecureToken: true,
};

return this.invokeRequestHandler(FIREBASE_AUTH_VERIFY_CUSTOM_TOKEN, request);
}

/**
* Invokes the request handler based on the API settings object passed.
*
Expand All @@ -610,6 +652,7 @@ export class FirebaseAuthRequestHandler {
// Validate request.
const requestValidator = apiSettings.getRequestValidator();
requestValidator(requestData);

// Process request.
return this.signedApiRequestHandler.sendRequest(
this.host, this.port, path, httpMethod, requestData, this.headers, this.timeout);
Expand Down
9 changes: 9 additions & 0 deletions src/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,15 @@ export class Auth implements FirebaseServiceInterface {
});
}

public async sendEmailVerification(uid: string): Promise<void> {
const customToken = await this.createCustomToken(uid);
const { idToken } = await this.authRequestHandler.verifyCustomToken(customToken);

await this.authRequestHandler.sendEmailVerification(idToken);

return;
}

/**
* Sets additional developer claims on an existing user identified by the provided UID.
*
Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ declare namespace admin.auth {
getUserByPhoneNumber(phoneNumber: string): Promise<admin.auth.UserRecord>;
listUsers(maxResults?: number, pageToken?: string): Promise<admin.auth.ListUsersResult>;
updateUser(uid: string, properties: admin.auth.UpdateRequest): Promise<admin.auth.UserRecord>;
sendEmailVerification(uid: string): Promise<void>;
verifyIdToken(idToken: string, checkRevoked?: boolean): Promise<admin.auth.DecodedIdToken>;
setCustomUserClaims(uid: string, customUserClaims: Object): Promise<void>;
revokeRefreshTokens(uid: string): Promise<void>;
Expand Down