Skip to content

Commit 9203287

Browse files
committed
auth0 job -> ticket to control redirect url
todo: create verification email template
1 parent 298ab9f commit 9203287

File tree

6 files changed

+36
-9
lines changed

6 files changed

+36
-9
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ After that you will need to select the following scopes:
8484
- `read:users`
8585
- `read:roles`
8686
- `delete:users`
87-
- `update:users` (Sending email verification)
87+
- `create:user_tickets` (Sending email verification)
8888

8989
These are the only scopes we need, but you can select all if you want.
9090

src/config/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ const config = {
3232
pagination: {
3333
limit: 20,
3434
},
35+
urls: {
36+
CLIENT_BASE_URL: process.env.CLIENT_BASE_URL,
37+
},
3538
};
3639

3740
export default config;

src/modules/common/auth0/auth0.service.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { HttpException, Injectable } from '@nestjs/common';
22
import fetch from 'node-fetch';
33
import * as Sentry from '@sentry/node';
44
import Config from '../../../config';
5-
import type { Auth0Response } from './auth0.types';
5+
import type { Auth0Response, EmailVerificationTicket } from './auth0.types';
66

77
@Injectable()
88
export class Auth0Service {
@@ -62,28 +62,30 @@ export class Auth0Service {
6262
return response;
6363
}
6464

65-
async sendVerificationEmail(
65+
async createVerificationEmailTicket(
6666
accessToken: string,
6767
auth0UserId: string,
6868
): Promise<Auth0Response> {
6969
try {
7070
const [provider, userId] = auth0UserId.split('|');
7171
const payload = {
72+
result_url: Config.urls.CLIENT_BASE_URL,
7273
user_id: auth0UserId,
7374
identity: { user_id: userId, provider },
7475
};
76+
7577
const options = {
7678
method: 'POST',
7779
headers: {
78-
Authorization: `Bearer ${accessToken}`,
80+
'Authorization': `Bearer ${accessToken}`,
7981
'content-type': 'application/json',
8082
},
8183
body: JSON.stringify(payload),
8284
};
8385

84-
const response: Auth0Response = await (
86+
const response: Auth0Response<EmailVerificationTicket> = await (
8587
await fetch(
86-
`https://${Config.auth0.backend.DOMAIN}/api/v2/jobs/verification-email`,
88+
`https://${Config.auth0.backend.DOMAIN}/api/v2/tickets/email-verification`,
8789
options,
8890
)
8991
).json();

src/modules/common/auth0/auth0.types.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
interface Auth0ResponseSuccess {} // tslint:disable-line
1+
type Auth0ResponseSuccess<T> = T;
2+
3+
export interface EmailVerificationTicket {
4+
ticket: string;
5+
}
26

37
interface Auth0ResponseError {
48
statusCode: number;
@@ -7,7 +11,9 @@ interface Auth0ResponseError {
711
errorCode: string;
812
}
913

10-
export type Auth0Response = Auth0ResponseSuccess | Auth0ResponseError;
14+
export type Auth0Response<T = any> =
15+
| Auth0ResponseSuccess<T>
16+
| Auth0ResponseError;
1117

1218
interface Auth0UserIdentity {
1319
connection: string;

src/modules/email/interfaces/email.interface.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ interface WelcomePayload {
1919
};
2020
}
2121

22+
interface VerificationEmailPayload {
23+
name: 'verification-email';
24+
data: {
25+
ticket: string;
26+
};
27+
}
28+
2229
interface MentorshipAccepted {
2330
name: 'mentorship-accepted';
2431
data: {
@@ -109,6 +116,7 @@ interface MentorFreeze {
109116
export type EmailParams = Required<Pick<MailData, 'to' | 'subject'>> &
110117
(
111118
| WelcomePayload
119+
| VerificationEmailPayload
112120
| MentorshipAccepted
113121
| MentorshipCancelled
114122
| MentorshipDeclined

src/modules/users/users.controller.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,11 +401,19 @@ export class UsersController {
401401
}
402402

403403
const data = await this.auth0Service.getAdminAccessToken();
404-
const response = await this.auth0Service.sendVerificationEmail(
404+
const response = await this.auth0Service.createVerificationEmailTicket(
405405
data.access_token,
406406
user.auth0Id,
407407
);
408408

409+
// TODO - create email verification template
410+
this.emailService.sendLocalTemplate({
411+
name: 'verification-email',
412+
data: response,
413+
to: user.email,
414+
subject: 'Verify your email',
415+
});
416+
409417
return {
410418
success: true,
411419
data: response,

0 commit comments

Comments
 (0)