mirror of
https://github.com/detleph/server.git
synced 2026-09-04 08:36:06 +02:00
Added user_auth system
+ 1 Mega commit yay
This commit is contained in:
@@ -3,27 +3,79 @@ import prisma from "../lib/prisma";
|
||||
import { mailClient } from "../lib/redis";
|
||||
import { nanoid } from "nanoid";
|
||||
import { verificationMail } from "../lib/mail";
|
||||
import { DataType, generateInvalidBodyError } from "./common";
|
||||
import { generateTeamleaderJWT } from "../Middleware/auth/teamleaderAuth";
|
||||
|
||||
export const register = async (req: Request, res: Response) => {
|
||||
//TODO: Implemnt user endpoint and use following code to send verification mail
|
||||
interface CreateTeamBody {
|
||||
name: string;
|
||||
leaderEmail: string;
|
||||
disciplineId: string;
|
||||
}
|
||||
|
||||
const user = {
|
||||
//Supposed to come from database
|
||||
id: "10",
|
||||
email: "[email protected]",
|
||||
};
|
||||
export const register = async (req: Request<{}, {}, CreateTeamBody>, res: Response) => {
|
||||
if (req.body.name == null || req.body.disciplineId == null || req.body.leaderEmail == null) {
|
||||
res.status(400).json(
|
||||
generateInvalidBodyError({
|
||||
name: DataType.STRING,
|
||||
leaderEmail: DataType.STRING,
|
||||
disciplineId: DataType.STRING,
|
||||
})
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const team = await prisma.team.create({
|
||||
data: {
|
||||
leaderEmail: req.body.leaderEmail,
|
||||
name: req.body.name,
|
||||
roles: undefined,
|
||||
discipline: { connect: { pid: req.body.disciplineId } },
|
||||
},
|
||||
select: {
|
||||
pid: true,
|
||||
name: true,
|
||||
disciplineId: true,
|
||||
},
|
||||
});
|
||||
|
||||
const usid = nanoid();
|
||||
|
||||
(await mailClient).set(usid, user.id);
|
||||
(await mailClient).set(usid, team.pid);
|
||||
|
||||
verificationMail(user.email, "eventname", usid);
|
||||
verificationMail(req.body.leaderEmail, "eventname", usid);
|
||||
|
||||
//Send status code
|
||||
res.status(201).json({ type: "success", payload: { team } });
|
||||
};
|
||||
|
||||
export const requestToken = async (req: Request, res: Response) => {
|
||||
const { teamId } = req.body || {};
|
||||
|
||||
if (!(typeof teamId === "string")) {
|
||||
return res.status(400).json(generateInvalidBodyError({ teamId: DataType.STRING }));
|
||||
}
|
||||
|
||||
const team = await prisma.team.findUnique({
|
||||
where: {
|
||||
pid: teamId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!team) {
|
||||
return res.status(404).json();
|
||||
}
|
||||
|
||||
const usid = nanoid();
|
||||
|
||||
(await mailClient).set(usid, team.pid);
|
||||
|
||||
verificationMail(team.leaderEmail, "eventname", usid);
|
||||
|
||||
res.status(200).json({ type: "sucess", message: "Email sent!" });
|
||||
};
|
||||
|
||||
//TODO: This should be a get request with the code as a veriable part in the url
|
||||
export const verifyEmail = async (req: Request, res: Response) => {
|
||||
const { code } = req.body || {};
|
||||
const { code } = req.params || {};
|
||||
|
||||
if (!(typeof code === "string")) {
|
||||
return res.status(400).json({
|
||||
@@ -45,12 +97,16 @@ export const verifyEmail = async (req: Request, res: Response) => {
|
||||
});
|
||||
}
|
||||
|
||||
prisma.participant.update({
|
||||
const team = await prisma.team.update({
|
||||
where: {
|
||||
id: parseInt(acc),
|
||||
pid: acc,
|
||||
},
|
||||
data: {
|
||||
verified: true,
|
||||
},
|
||||
});
|
||||
|
||||
mailClient.set(code, "");
|
||||
|
||||
res.status(200).json({ type: "succes", payload: { token: generateTeamleaderJWT(team) } }); //TODO: This needs to set a cookie or smth so that the client also gets this info
|
||||
};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Participant } from "@prisma/client";
|
||||
import { Team } from "@prisma/client";
|
||||
import e, { NextFunction, Request, Response } from "express";
|
||||
import jwt, { JsonWebTokenError } from "jsonwebtoken";
|
||||
import AuthError from "../error/AuthError";
|
||||
@@ -6,20 +6,18 @@ import { getBearerToken, verifyAuthorizationFormat } from "./auth";
|
||||
import prisma from "../../lib/prisma";
|
||||
|
||||
export interface TeamleaderJWTPayload {
|
||||
pid: string;
|
||||
team: string;
|
||||
}
|
||||
|
||||
const JWT_SECRET = process.env.JWT_SECRET;
|
||||
|
||||
export function generateTeamleaderJWT(teamleader: Participant & { relevance: "TEAMLEADER"; team: { pid: string } }) {
|
||||
export function generateTeamleaderJWT(teamleader: Team) {
|
||||
if (!JWT_SECRET) {
|
||||
throw new Error("JWT_SECRET not set");
|
||||
}
|
||||
|
||||
const payload: TeamleaderJWTPayload = {
|
||||
pid: teamleader.pid,
|
||||
team: teamleader.team.pid,
|
||||
team: teamleader.pid,
|
||||
};
|
||||
|
||||
return jwt.sign(payload, JWT_SECRET, { expiresIn: "4 days" });
|
||||
@@ -57,7 +55,6 @@ export async function requireTeamleaderAuthentication(req: Request, res: Respons
|
||||
|
||||
req.teamleader = {
|
||||
isAuthenticated: true,
|
||||
pid: token_payload.pid,
|
||||
team: token_payload.team,
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import express from "express";
|
||||
import { register, requestToken, verifyEmail } from "../Controllers/user_auth.controller";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.post("/", register);
|
||||
router.get("/verify/:code", verifyEmail);
|
||||
router.get("/token", requestToken);
|
||||
|
||||
export default router;
|
||||
+4
-1
@@ -13,6 +13,7 @@ import defaultErrorHandler from "./Middleware/error/handler";
|
||||
import logger from "./Middleware/error/logger";
|
||||
import debugLogger from "./Middleware/debug/logger";
|
||||
import mediaRouter from "./Routes/media.routes";
|
||||
import userRouter from "./Routes/user_auth.routes";
|
||||
import { notFoundHandler, rootHandler } from "./Middleware/error/defaultRoutes";
|
||||
|
||||
// Set up async error handling
|
||||
@@ -82,7 +83,9 @@ async function main() {
|
||||
app.use("/api/role-schemas", roleSchemaRouter);
|
||||
|
||||
app.use("/api/media", mediaRouter);
|
||||
|
||||
|
||||
app.use("/api/users", userRouter);
|
||||
|
||||
app.get("/", rootHandler);
|
||||
app.get("/api", rootHandler);
|
||||
|
||||
|
||||
+14
-3
@@ -6,13 +6,21 @@ import SMTPTransport from "nodemailer/lib/smtp-transport";
|
||||
|
||||
import mjml from "./mjml";
|
||||
import { randomUUID } from "crypto";
|
||||
import logger from "../Middleware/error/logger";
|
||||
|
||||
export let mailAccount = { user: process.env.MAILUSER + "@mail." + process.env.DOMAIN, pass: process.env.MAILPASSWORD };
|
||||
|
||||
let transporter =
|
||||
process.env.DEV == "true" || process.env.DOMAIN == undefined
|
||||
process.env.NODE_ENV == "development" || process.env.DOMAIN == undefined
|
||||
? (async () => {
|
||||
mailAccount = await nodemailer.createTestAccount();
|
||||
if (process.env.ETHEREAL_EMAIL == undefined || process.env.ETHEREAL_PASSWORD == undefined) {
|
||||
mailAccount = await nodemailer.createTestAccount();
|
||||
} else {
|
||||
mailAccount = {
|
||||
user: process.env.ETHEREAL_EMAIL,
|
||||
pass: process.env.ETHEREAL_PASSWORD,
|
||||
};
|
||||
}
|
||||
if (process.env.NODE_ENV != "test") {
|
||||
console.log(mailAccount);
|
||||
}
|
||||
@@ -43,6 +51,8 @@ let transporter =
|
||||
);
|
||||
|
||||
const sendMail = async (from: string, to: string, subject: string, text?: string, html?: string) => {
|
||||
logger.debug(`Sent email to: ${to}`);
|
||||
|
||||
return await (
|
||||
await transporter
|
||||
).sendMail({
|
||||
@@ -57,7 +67,8 @@ const sendMail = async (from: string, to: string, subject: string, text?: string
|
||||
export const verificationMail = async (to: string, eventName: string, verificationLink: string) => {
|
||||
const raw = mjml.getTemplate("emailVerification");
|
||||
|
||||
//TODO: Replace other handlebars with final values
|
||||
//TODO: Set the verification link to the correct endpoint
|
||||
verificationLink = "https://" + (process.env.DOMAIN ?? "localhost:3000") + "/api/users/verify/" + verificationLink;
|
||||
const message = Handlebars.compile(raw);
|
||||
|
||||
const data = { eventName, verificationLink };
|
||||
|
||||
Reference in New Issue
Block a user