mirror of
https://github.com/detleph/server.git
synced 2026-09-04 23:45:00 +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
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user