diff --git a/src/lib/i18n/en.json b/src/lib/i18n/en.json index afa7947..d96934c 100644 --- a/src/lib/i18n/en.json +++ b/src/lib/i18n/en.json @@ -19,5 +19,6 @@ "Failed to create account: {reason}": "Failed to create account: {reason}", "Error creating account": "Error creating account", "Error creating account: {reason}": "Error creating account: {reason}", - "Unknown error": "Unknown error" + "Unknown error": "Unknown error", + "You've either already received your free account or an account creation code has been sent to your email. To create additional accounts, please proceed with the purchase below.": "You've either already received your free account or an account creation code has been sent to your email. To create additional accounts, please proceed with the purchase below." } \ No newline at end of file diff --git a/src/lib/sendgrid.ts b/src/lib/sendgrid.ts index caa868a..9f86566 100644 --- a/src/lib/sendgrid.ts +++ b/src/lib/sendgrid.ts @@ -1,45 +1,41 @@ -// /** Sendgrid email helpers, backend only. */ -// This is currently being handled by the Whalesplainer hosted at https://create-api.anchor.link -// If this service ends up replacing Whalesplainer then, this will need to be uncommented. +import { SENDGRID_KEY, SENDGRID_TEMPLATE, SENDGRID_FROM } from '$env/static/private' -// import { SENDGRID_KEY, SENDGRID_TEMPLATE, SENDGRID_FROM} from '$env/static/private' +const key = SENDGRID_KEY +const templateId = SENDGRID_TEMPLATE || 'd-1106a932fc984f14be0230c670820b38' +const from = SENDGRID_FROM || 'no-reply@greymass.com' -// const key = SENDGRID_KEY -// const templateId = SENDGRID_TEMPLATE || 'd-1106a932fc984f14be0230c670820b38' -// const from = SENDGRID_FROM || 'no-reply@greymass.com' +if (!key) { + console.log('Missing SENDGRID_KEY, will not send emails') +} -// if (!key) { -// console.log('Missing SENDGRID_KEY, will not send emails') -// } +export async function sendEmail(to: string, data: Record) { + if (!key) return -// export async function sendEmail(to: string, data: Record) { -// if (!key) return - -// const res = await fetch('https://api.sendgrid.com/v3/mail/send', { -// body: JSON.stringify({ -// from: { -// email: from -// }, -// personalizations: [ -// { -// to: [{ email: to }], -// dynamic_template_data: data -// } -// ], -// template_id: templateId -// }), -// headers: { -// Authorization: `Bearer ${key}`, -// 'Content-Type': 'application/json' -// }, -// method: 'POST' -// }) -// if (res.status >= 400) { -// const body = -// parseInt(res.headers.get('content-length') || '0') > 0 -// ? await res.text() -// : `${res.status} ${res.statusText}` -// throw new Error(`Sendgrid error: ${body}`) -// } -// } + const res = await fetch('https://api.sendgrid.com/v3/mail/send', { + body: JSON.stringify({ + from: { + email: from + }, + personalizations: [ + { + to: [{ email: to }], + dynamic_template_data: data + } + ], + template_id: templateId + }), + headers: { + Authorization: `Bearer ${key}`, + 'Content-Type': 'application/json' + }, + method: 'POST' + }) + if (res.status >= 400) { + const body = + parseInt(res.headers.get('content-length') || '0') > 0 + ? await res.text() + : `${res.status} ${res.statusText}` + throw new Error(`Sendgrid error: ${body}`) + } +} diff --git a/src/routes/buy/+page.svelte b/src/routes/buy/+page.svelte index 25edb7a..94ef0f4 100644 --- a/src/routes/buy/+page.svelte +++ b/src/routes/buy/+page.svelte @@ -111,7 +111,7 @@ {:else}

{$t('Free Account Not Available')}

-

{$t('You\'ve already received your free account. To create additional accounts, please proceed with the purchase below.')}

+

{$t('You\'ve either already received your free account or an account creation code has been sent to your email. To create additional accounts, please proceed with the purchase below.')}

{/if} diff --git a/src/routes/ticket/+page.server.ts b/src/routes/ticket/+page.server.ts index 4699d0b..9d03101 100644 --- a/src/routes/ticket/+page.server.ts +++ b/src/routes/ticket/+page.server.ts @@ -3,13 +3,14 @@ import type { Actions } from './$types'; import { CreateRequest } from '@greymass/account-creation'; import { createTicket, generateCreationRequest, getSextantProductId } from '$lib/sextant'; import { PUBLIC_WHALESPLAINER_URL } from '$env/static/public'; +import { sendEmail } from '$lib/sendgrid'; export const actions: Actions = { default: async ({ url, locals, request }) => { const session = await locals.auth(); if (!session) { - return fail(401, { error: 'Unauthorized' }); + return fail(401, { error: 'Unauthorized' }); } const formData = await request.formData(); @@ -17,14 +18,22 @@ export const actions: Actions = { const searchParams = new URLSearchParams(String(formData.get('searchParams'))); const createRequestArguments = { - login_scope: String(searchParams.get('scope') ?? ''), - return_path: String(searchParams.get('return_url') ?? ''), + login_scope: String(searchParams.get('scope') ?? ''), + return_path: String(searchParams.get('return_url') ?? ''), }; + + if (!session.user?.email) { + return fail(403, { error: 'Session must contain an email address.' }); + } const createRequest: CreateRequest = generateCreationRequest(createRequestArguments) const sextantProductId = await getSextantProductId(); - await createTicket(createRequest.code, sextantProductId, 'free account - google login', session.user?.email ?? undefined); + await createTicket(createRequest.code, sextantProductId, 'free account', session.user.email); + + await sendEmail(session.user.email, { + createurl: `${PUBLIC_WHALESPLAINER_URL}/activate/${createRequest.toString(false)}`, + }); if (searchParams.get('owner_key') || searchParams.get('active_key')) { redirect(302, `/create?ticket=${createRequest.toString(false)}&${searchParams}`);