/.ts
Created
July 27, 2022 11:54
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private async createUserOnAuth0( | |
email: string, | |
password: string, | |
createdBy: string, | |
retryCount = 0, | |
): Promise<Record<string, string>> { | |
try { | |
const axiosResponse = await this.httpService | |
.post( | |
`https://${configService.getAuth0Domain()}/dbconnections/signup`, | |
{ | |
client_id: configService.getAuth0ClientId(), | |
client_secret: configService.getAuth0ClientSecret(), | |
connection: configService.getAuth0Connection(), | |
email, | |
password, | |
}, | |
) | |
.toPromise(); | |
this.logger.log( | |
axiosResponse.data.email, | |
'Auth0 user created with email', | |
); | |
// Send password reset email | |
this.sendPasswordResetEmail(email); | |
return { auth0Response: axiosResponse.data, password }; | |
} catch (err) { | |
this.logger.error(err); | |
/** | |
* {@link https://auth0.com/docs/connections/database/password-strength} | |
* Auth0 does not send any specific response, so here we are calling create user again | |
* assuming password failed to meet the requirement of auth0 | |
* But here also we are gonna try it ERROR_RETRY_COUNT times and after that stop call, | |
* so we don't get in infinite loop | |
*/ | |
if (retryCount < ERROR_RETRY_COUNT) { | |
return this.createUserOnAuth0( | |
email, | |
Utilities.generatePassword(16), | |
createdBy, | |
retryCount + 1, | |
); | |
} | |
throw new HttpException(err, HttpStatus.BAD_REQUEST); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment