From 11ec7786babe5d27f707fd67858f8b00de680adf Mon Sep 17 00:00:00 2001
From: Lukas <69743171+quick007@users.noreply.github.com>
Date: Mon, 15 Jan 2024 00:18:05 -0800
Subject: [PATCH] emails for login !
---
islands/loginForm.tsx | 4 +---
routes/api/auth/login.ts | 20 +++++++++++++++++---
utils/db/auth.ts | 2 +-
utils/db/kv.ts | 1 +
utils/email/client.ts | 37 +++++++++++++++++++++++++++++++++++++
5 files changed, 57 insertions(+), 7 deletions(-)
create mode 100644 utils/email/client.ts
diff --git a/islands/loginForm.tsx b/islands/loginForm.tsx
index fb5a1b6..c114211 100644
--- a/islands/loginForm.tsx
+++ b/islands/loginForm.tsx
@@ -43,8 +43,6 @@ const LoginForm = ({ attending }: { attending: boolean }) => {
return;
}
- alert("yeur logn code: " + code.otp);
-
setStage(1);
if (codeRef.current) {
setTimeout(() => {
@@ -85,7 +83,7 @@ const LoginForm = ({ attending }: { attending: boolean }) => {
return (
- {/* damn were going jank already */}
+ {/* damn we're going jank already */}
()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|.(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
@@ -44,9 +45,21 @@ export const handler: Handlers<{ email: string; otp: string }> = {
}
const otp = await generateOTP(email);
-
- // Currently used for development as we don't have a way to send emails currently
- const response = new Response(JSON.stringify({ otp }), {
+ console.log("test")
+ try {
+ await sendEmail([email], "Your Events Authorization Code", {
+ content: `Your one time login code is ${otp}.
Do not share it with anyone`,
+ html: true,
+ });
+ } catch (err) {
+ return new Response(
+ JSON.stringify({ error: "An error occured while sending the confirmation email. Please try again." }),
+ {
+ status: 400,
+ },
+ );
+ }
+ const response = new Response(JSON.stringify({ success: true }), {
status: 200,
});
@@ -112,6 +125,7 @@ export const handler: Handlers<{ email: string; otp: string }> = {
expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 30),
value: userAuthToken!,
path: "/",
+ sameSite: "Strict"
});
return resp;
diff --git a/utils/db/auth.ts b/utils/db/auth.ts
index 16400eb..accba8b 100644
--- a/utils/db/auth.ts
+++ b/utils/db/auth.ts
@@ -3,7 +3,7 @@ export const genCode = async (email: string) => {
method: "GET",
});
- const res = (await req.json()) as { otp?: string; error?: string };
+ const res = (await req.json()) as { success?: true; error?: string };
return res;
};
diff --git a/utils/db/kv.ts b/utils/db/kv.ts
index 9aa26e7..39a48d2 100644
--- a/utils/db/kv.ts
+++ b/utils/db/kv.ts
@@ -88,6 +88,7 @@ export const getUserEmailCode = async (
expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 30),
value: user.value.authToken,
path: "/",
+ sameSite: "Strict"
});
return user.value;
diff --git a/utils/email/client.ts b/utils/email/client.ts
new file mode 100644
index 0000000..f5a3285
--- /dev/null
+++ b/utils/email/client.ts
@@ -0,0 +1,37 @@
+import {
+ SESClient,
+ SendEmailCommand,
+ SendEmailRequest,
+} from "npm:@aws-sdk/client-ses";
+
+export const sesClient = new SESClient({
+ region: "us-east-1",
+ credentials: {
+ accessKeyId: Deno.env.get("AWS_ACCESS_KEY_ID")!,
+ secretAccessKey: Deno.env.get("AWS_SECRET_ACCESS_KEY")!,
+ },
+});
+
+export const sendEmail = async (to: string[], subject: string, message: {content: string, html: boolean}) => {
+ const params: SendEmailRequest = {
+ Source: `"Events" `,
+ Destination: {
+ ToAddresses: to
+ },
+ Message: {
+ /* required */
+ Body: {
+ /* required */
+ [message.html ? "Html" : "Text"]: {
+ Charset: "UTF-8",
+ Data: message.content,
+ },
+ },
+ Subject: {
+ Charset: "UTF-8",
+ Data: subject,
+ },
+ },
+ };
+ return await sesClient.send(new SendEmailCommand(params as SendEmailRequest));
+};