Add Endpoint to verify email codes

This commit is contained in:
Stefan-5422
2022-04-25 08:57:32 +02:00
parent c96537f5b4
commit 62a5daa432
6 changed files with 95 additions and 12 deletions
+25 -8
View File
@@ -22,6 +22,7 @@
"handlebars": "^4.7.7", "handlebars": "^4.7.7",
"jsonwebtoken": "^8.5.1", "jsonwebtoken": "^8.5.1",
"mjml": "^4.11.0", "mjml": "^4.11.0",
"nanoid": "^3.3.3",
"nodemailer": "^6.7.0", "nodemailer": "^6.7.0",
"redis": "^3.1.2" "redis": "^3.1.2"
}, },
@@ -2503,16 +2504,27 @@
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
"dev": true "dev": true
}, },
"node_modules/mocha/node_modules/nanoid": {
"version": "3.1.25",
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.25.tgz",
"integrity": "sha512-rdwtIXaXCLFAQbnfqDRnI6jaRHp9fTcYBjtFKE8eezcZ7LuLjhUaQGNeMXf1HmRoCH32CLz6XwX0TtxEOS/A3Q==",
"dev": true,
"bin": {
"nanoid": "bin/nanoid.cjs"
},
"engines": {
"node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1"
}
},
"node_modules/ms": { "node_modules/ms": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
}, },
"node_modules/nanoid": { "node_modules/nanoid": {
"version": "3.1.25", "version": "3.3.3",
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.25.tgz", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.3.tgz",
"integrity": "sha512-rdwtIXaXCLFAQbnfqDRnI6jaRHp9fTcYBjtFKE8eezcZ7LuLjhUaQGNeMXf1HmRoCH32CLz6XwX0TtxEOS/A3Q==", "integrity": "sha512-p1sjXuopFs0xg+fPASzQ28agW1oHD7xDsd9Xkf3T15H3c/cifrFHVwrh74PdoklAPi+i7MdRsE47vm2r6JoB+w==",
"dev": true,
"bin": { "bin": {
"nanoid": "bin/nanoid.cjs" "nanoid": "bin/nanoid.cjs"
}, },
@@ -5520,6 +5532,12 @@
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
"dev": true "dev": true
},
"nanoid": {
"version": "3.1.25",
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.25.tgz",
"integrity": "sha512-rdwtIXaXCLFAQbnfqDRnI6jaRHp9fTcYBjtFKE8eezcZ7LuLjhUaQGNeMXf1HmRoCH32CLz6XwX0TtxEOS/A3Q==",
"dev": true
} }
} }
}, },
@@ -5529,10 +5547,9 @@
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
}, },
"nanoid": { "nanoid": {
"version": "3.1.25", "version": "3.3.3",
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.25.tgz", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.3.tgz",
"integrity": "sha512-rdwtIXaXCLFAQbnfqDRnI6jaRHp9fTcYBjtFKE8eezcZ7LuLjhUaQGNeMXf1HmRoCH32CLz6XwX0TtxEOS/A3Q==", "integrity": "sha512-p1sjXuopFs0xg+fPASzQ28agW1oHD7xDsd9Xkf3T15H3c/cifrFHVwrh74PdoklAPi+i7MdRsE47vm2r6JoB+w=="
"dev": true
}, },
"negotiator": { "negotiator": {
"version": "0.6.2", "version": "0.6.2",
+1
View File
@@ -32,6 +32,7 @@
"handlebars": "^4.7.7", "handlebars": "^4.7.7",
"jsonwebtoken": "^8.5.1", "jsonwebtoken": "^8.5.1",
"mjml": "^4.11.0", "mjml": "^4.11.0",
"nanoid": "^3.3.3",
"nodemailer": "^6.7.0", "nodemailer": "^6.7.0",
"redis": "^3.1.2" "redis": "^3.1.2"
}, },
+2
View File
@@ -101,6 +101,8 @@ model Participant {
email String email String
gender Gender gender Gender
verified Boolean @default(false)
group Group @relation(fields: [groupId], references: [id]) group Group @relation(fields: [groupId], references: [id])
groupId Int groupId Int
team Team @relation(name: "participants", fields: [teamId], references: [id]) team Team @relation(name: "participants", fields: [teamId], references: [id])
+56
View File
@@ -0,0 +1,56 @@
import { Request, Response } from "express";
import prisma from "../lib/prisma";
import { mailClient } from "../lib/redis";
import { nanoid } from "nanoid";
import { verificationMail } from "../lib/mail";
export const register = async (req: Request, res: Response) => {
//TODO: Implemnt user endpoint and use following code to send verification mail
const user = {
//Supposed to come from database
id: "10",
email: "[email protected]",
};
const usid = nanoid();
(await mailClient).set(usid, user.id);
verificationMail(user.email, "eventname", usid);
//Send status code
};
export const verifyEmail = async (req: Request, res: Response) => {
const { code } = req.body || {};
if (!(typeof code === "string")) {
return res.status(400).json({
type: "error",
payload: {
message: "Invalid Request parameter",
},
});
}
const acc = await mailClient.get(code);
if (!acc) {
return res.status(404).json({
type: "error",
payload: {
message: "Invalid token. It might be expired",
},
});
}
prisma.participant.update({
where: {
id: parseInt(acc),
},
data: {
verified: true,
},
});
};
+1 -4
View File
@@ -1,6 +1,6 @@
import { writeFile } from "fs"; import { writeFile } from "fs";
import Handlebars, { template } from "handlebars"; import Handlebars, { template } from "handlebars";
import { emailClient } from "./redis"; import { mailClient } from "./redis";
import nodemailer from "nodemailer"; import nodemailer from "nodemailer";
import SMTPTransport from "nodemailer/lib/smtp-transport"; import SMTPTransport from "nodemailer/lib/smtp-transport";
@@ -64,9 +64,6 @@ export const verificationMail = async (to: string, eventName: string, verificati
const compiled = message(data); const compiled = message(data);
sendMail(mailAccount.user, to, "Verify Email", undefined, compiled); sendMail(mailAccount.user, to, "Verify Email", undefined, compiled);
//TODO: Implement verification codes
//TODO: Write endpoints to verify codes
}; };
export default { sendMail }; export default { sendMail };
+10
View File
@@ -3,6 +3,7 @@ import { promisify } from "util";
const REDIS_INDICES = { const REDIS_INDICES = {
auth: 0, auth: 0,
mail: 1,
}; };
const REDIS_HOST = process.env.REDIS_HOST || "redis"; const REDIS_HOST = process.env.REDIS_HOST || "redis";
@@ -24,4 +25,13 @@ const auth = redis.createClient({ host: REDIS_HOST, port: REDIS_PORT });
auth.select(REDIS_INDICES.auth); auth.select(REDIS_INDICES.auth);
// ----------------- //
// Redis mail client //
// ----------------- //
const mail = redis.createClient({ host: REDIS_HOST, port: REDIS_PORT });
mail.select(REDIS_INDICES.mail);
export const authClient = createAsyncClient(auth); export const authClient = createAsyncClient(auth);
export const mailClient = createAsyncClient(mail);