Skip to content

Commit bd2a47b

Browse files
committed
[UI] fix email verification for cloud mode (#895)
1 parent 998c8e8 commit bd2a47b

File tree

2 files changed

+48
-51
lines changed

2 files changed

+48
-51
lines changed

services/app/src/routes/verify-email/+page.server.ts

Lines changed: 42 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { env } from '$env/dynamic/private';
2-
import { emailCodeCooldownSecs } from '$lib/constants.js';
32
import logger, { APIError } from '$lib/logger.js';
43
import { error, fail, redirect } from '@sveltejs/kit';
54
import { 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
234226
const getVerificationEmailBody = (verificationToken: string) => `<html>
235227
<head>
236228
<style type="text/css">

services/app/src/routes/verify-email/+page.svelte

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script lang="ts">
22
import { signOut } from '@auth/sveltekit/client';
33
import { page } from '$app/state';
4+
import { goto } from '$app/navigation';
45
import { enhance } from '$app/forms';
56
import type { User } from '$lib/types';
67
@@ -77,7 +78,11 @@
7778
</Button>
7879
</form>
7980

80-
<Button type="button" variant="destructive" onclick={signOut}>
81+
<Button
82+
type="button"
83+
variant="destructive"
84+
onclick={page.data.auth0Mode ? () => goto('/logout') : signOut}
85+
>
8186
<span>Log Out</span>
8287
</Button>
8388
</div>

0 commit comments

Comments
 (0)