Skip to content

Commit

Permalink
emails for login !
Browse files Browse the repository at this point in the history
  • Loading branch information
quick007 committed Jan 15, 2024
1 parent 0aaccb8 commit 11ec778
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
4 changes: 1 addition & 3 deletions islands/loginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ const LoginForm = ({ attending }: { attending: boolean }) => {
return;
}

alert("yeur logn code: " + code.otp);

setStage(1);
if (codeRef.current) {
setTimeout(() => {
Expand Down Expand Up @@ -85,7 +83,7 @@ const LoginForm = ({ attending }: { attending: boolean }) => {

return (
<div className="w-[16.5rem] [@media(min-width:300px)]:w-[18.5rem] overflow-hidden p-1">
{/* damn were going jank already */}
{/* damn we're going jank already */}
<div
className={`flex ${
stage == 1
Expand Down
20 changes: 17 additions & 3 deletions routes/api/auth/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
validateOTP,
} from "@/utils/db/kv.ts";
import { deleteCookie, setCookie } from "$std/http/cookie.ts";
import { sendEmail } from "@/utils/email/client.ts";

const emailRegex =
/^(([^<>()[\]\\.,;:\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,}))$/;
Expand Down Expand Up @@ -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}.<br/> <b>Do not share it with anyone</b>`,
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,
});

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion utils/db/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
1 change: 1 addition & 0 deletions utils/db/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
37 changes: 37 additions & 0 deletions utils/email/client.ts
Original file line number Diff line number Diff line change
@@ -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" <[email protected]>`,
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));
};

0 comments on commit 11ec778

Please sign in to comment.