1- import { Injectable } from '@nestjs/common' ;
1+ import { HttpException , Injectable } from '@nestjs/common' ;
22import fetch from 'node-fetch' ;
3- import Config from '../../config' ;
3+ import * as Sentry from '@sentry/node' ;
4+ import Config from '../../../config' ;
5+ import type { Auth0Response } from './auth0.types' ;
46
57@Injectable ( )
68export class Auth0Service {
79 // Get an access token for the Auth0 Admin API
8- async getAdminAccessToken ( ) {
10+ async getAdminAccessToken ( ) : Promise < { access_token : string } > {
911 const options = {
1012 method : 'POST' ,
1113 headers : { 'content-type' : 'application/json' } ,
@@ -27,7 +29,7 @@ export class Auth0Service {
2729 }
2830
2931 // Get the user's profile from auth0
30- async getUserProfile ( accessToken , userID ) {
32+ async getUserProfile ( accessToken : string , userID : string ) {
3133 const options = {
3234 headers : {
3335 Authorization : `Bearer ${ accessToken } ` ,
@@ -59,4 +61,41 @@ export class Auth0Service {
5961
6062 return response ;
6163 }
64+
65+ async sendVerificationEmail (
66+ accessToken : string ,
67+ auth0UserId : string ,
68+ ) : Promise < Auth0Response > {
69+ try {
70+ const [ provider , userId ] = auth0UserId . split ( '|' ) ;
71+ const payload = {
72+ user_id : auth0UserId ,
73+ identity : { user_id : userId , provider } ,
74+ } ;
75+ const options = {
76+ method : 'POST' ,
77+ headers : {
78+ Authorization : `Bearer ${ accessToken } ` ,
79+ 'content-type' : 'application/json' ,
80+ } ,
81+ body : JSON . stringify ( payload ) ,
82+ } ;
83+
84+ const response : Auth0Response = await (
85+ await fetch (
86+ `https://${ Config . auth0 . backend . DOMAIN } /api/v2/jobs/verification-email` ,
87+ options ,
88+ )
89+ ) . json ( ) ;
90+
91+ if ( 'statusCode' in response ) {
92+ throw new HttpException ( response , response . statusCode ) ;
93+ }
94+
95+ return response ;
96+ } catch ( error ) {
97+ Sentry . captureException ( error ) ;
98+ throw error ;
99+ }
100+ }
62101}
0 commit comments