11import { env } from '$env/dynamic/private' ;
2- import { emailCodeCooldownSecs } from '$lib/constants.js' ;
32import logger , { APIError } from '$lib/logger.js' ;
43import { error , fail , redirect } from '@sveltejs/kit' ;
54import { ManagementClient } from 'auth0' ;
@@ -55,60 +54,55 @@ export async function load({ locals, url }) {
5554 }
5655 }
5756
58- const listCodesRes = await fetch (
59- `${ OWL_URL } /api/v2/users/verify/email/code/list?${ new URLSearchParams ( [
60- [ 'limit' , '1' ] ,
61- [ 'search_query' , locals . user . email ] ,
62- [ 'search_columns' , 'user_email' ]
63- ] ) } `,
64- {
65- headers : {
66- ...headers ,
67- 'x-user-id' : '0'
68- }
69- }
70- ) ;
71- const listCodesBody = await listCodesRes . json ( ) ;
72-
73- if (
74- listCodesRes . ok &&
75- ( ! listCodesBody . items [ 0 ] ||
76- new Date ( listCodesBody . items [ 0 ] ?. expiry ) . getTime ( ) < new Date ( ) . getTime ( ) )
77- ) {
78- const sendCodeRes = await fetch (
79- `${ OWL_URL } /api/v2/users/verify/email/code?${ new URLSearchParams ( [
80- [ 'user_email' , locals . user . email ] ,
81- [ 'valid_days' , '1' ]
82- ] ) } `,
57+ if ( ! locals . auth0Mode ) {
58+ const listCodesRes = await fetch (
59+ `${ OWL_URL } /api/v2/users/verify/email/code/list?${ new URLSearchParams ( [ [ 'limit' , '1' ] ] ) } ` ,
8360 {
84- method : 'POST' ,
8561 headers : {
8662 ...headers ,
87- 'x-user-id' : '0'
63+ 'x-user-id' : locals . user . id
8864 }
8965 }
9066 ) ;
91- const sendCodeBody = await sendCodeRes . json ( ) ;
67+ const listCodesBody = await listCodesRes . json ( ) ;
9268
93- if ( ! sendCodeRes . ok ) {
94- logger . error ( 'VERIFYEMAIL_LOAD_GETCODE' , sendCodeBody ) ;
95- } else {
96- const sendEmailRes = await fetch ( 'https://api.resend.com/emails' , {
97- method : 'POST' ,
98- headers : {
99- Authorization : `Bearer ${ RESEND_API_KEY } ` ,
100- 'Content-Type' : 'application/json'
101- } ,
102- body : JSON . stringify ( {
103- from : 'JamAI Base <no-reply@jamaibase.com>' ,
104- to : locals . user . email ,
105- subject : 'Verify your JamAI Base email address' ,
106- html : getVerificationEmailBody ( sendCodeBody . id )
107- } )
108- } ) ;
69+ if (
70+ listCodesRes . ok &&
71+ ( ! listCodesBody . items [ 0 ] ||
72+ new Date ( listCodesBody . items [ 0 ] ?. expiry ) . getTime ( ) < new Date ( ) . getTime ( ) )
73+ ) {
74+ const sendCodeRes = await fetch (
75+ `${ OWL_URL } /api/v2/users/verify/email/code?${ new URLSearchParams ( [ [ 'valid_days' , '1' ] ] ) } ` ,
76+ {
77+ method : 'POST' ,
78+ headers : {
79+ ...headers ,
80+ 'x-user-id' : locals . user . id
81+ }
82+ }
83+ ) ;
84+ const sendCodeBody = await sendCodeRes . json ( ) ;
85+
86+ if ( ! sendCodeRes . ok ) {
87+ logger . error ( 'VERIFYEMAIL_LOAD_GETCODE' , sendCodeBody ) ;
88+ } else {
89+ const sendEmailRes = await fetch ( 'https://api.resend.com/emails' , {
90+ method : 'POST' ,
91+ headers : {
92+ Authorization : `Bearer ${ RESEND_API_KEY } ` ,
93+ 'Content-Type' : 'application/json'
94+ } ,
95+ body : JSON . stringify ( {
96+ from : 'JamAI Base <no-reply@jamaibase.com>' ,
97+ to : locals . user . email ,
98+ subject : 'Verify your JamAI Base email address' ,
99+ html : getVerificationEmailBody ( sendCodeBody . id )
100+ } )
101+ } ) ;
109102
110- if ( ! sendEmailRes . ok ) {
111- logger . error ( 'VERIFYEMAIL_LOAD_SENDCODE' , await sendEmailRes . json ( ) ) ;
103+ if ( ! sendEmailRes . ok ) {
104+ logger . error ( 'VERIFYEMAIL_LOAD_SENDCODE' , await sendEmailRes . json ( ) ) ;
105+ }
112106 }
113107 }
114108 }
@@ -163,7 +157,6 @@ export const actions = {
163157 }
164158 ) ;
165159 const responseBody = await response . json ( ) ;
166-
167160 if ( response . ok ) {
168161 if (
169162 new Date ( ) . getTime ( ) - new Date ( responseBody . items [ 0 ] ?. created_at ) . getTime ( ) >
@@ -183,7 +176,6 @@ export const actions = {
183176 }
184177 ) ;
185178 const sendCodeBody = await sendCodeRes . json ( ) ;
186-
187179 if ( ! sendCodeRes . ok ) {
188180 logger . error ( 'VERIFYEMAIL_RESEND_GETCODE' , sendCodeBody ) ;
189181 } else {
@@ -200,7 +192,6 @@ export const actions = {
200192 html : getVerificationEmailBody ( sendCodeBody . id )
201193 } )
202194 } ) ;
203-
204195 if ( ! sendEmailRes . ok ) {
205196 logger . error ( 'VERIFYEMAIL_RESEND_SENDCODE' , await sendEmailRes . json ( ) ) ;
206197 }
@@ -231,6 +222,7 @@ export const actions = {
231222 }
232223} ;
233224
225+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
234226const getVerificationEmailBody = ( verificationToken : string ) => `<html>
235227 <head>
236228 <style type="text/css">
0 commit comments