-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_auth.controller.ts
153 lines (125 loc) · 3.75 KB
/
user_auth.controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { Request, Response } from "express";
import prisma from "../lib/prisma";
import { mailClient } from "../lib/redis";
import { nanoid } from "nanoid";
import { verificationMail } from "../lib/mail";
import { createInsufficientPermissionsError, DataType, generateInvalidBodyError } from "./common";
import { generateTeamleaderJWT, requireLeaderOfTeam } from "../Middleware/auth/teamleaderAuth";
import { createRolesForTeam } from "./role.controller";
import { any, z } from "zod";
const TeamBody = z.object({
teamName: z.string().min(1),
leaderEmail: z.string().email(),
disciplineId: z.string().uuid(),
partFirstName: z.string().min(1),
partLastName: z.string().min(1),
partGroupId: z.string().uuid(),
});
interface CreateTeamBody {
teamName: string;
leaderEmail: string;
disciplineId: string;
partFirstName: string;
partLastName: string;
partGroupId: string;
}
// TODO: Some kind of auth (Teamleader probably)
export const register = async (req: Request<{}, {}, CreateTeamBody>, res: Response) => {
const result = TeamBody.safeParse(req.body);
if (result.success === false) {
return res.status(400).json(
generateInvalidBodyError(
{
teamName: DataType.STRING,
leaderEmail: DataType.STRING,
disciplineId: DataType.UUID,
partFirstName: DataType.STRING,
partLastName: DataType.STRING,
partGroupId: DataType.UUID,
},
result.error
)
);
}
const body = result.data;
const team = await prisma.team.create({
data: {
leaderEmail: body.leaderEmail,
name: body.teamName,
roles: undefined,
discipline: { connect: { pid: body.disciplineId } },
participants: {
create: {
firstName: body.partFirstName,
lastName: body.partLastName,
relevance: "TEAMLEADER",
group: { connect: { pid: body.partGroupId } },
},
},
},
select: {
pid: true,
name: true,
disciplineId: true,
},
});
//TODO: maybe use returned amount of created use?
createRolesForTeam(team.pid);
const usid = nanoid();
(await mailClient).set(usid, team.pid);
verificationMail(req.body.leaderEmail, "eventname", usid);
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!" });
};
export const verifyEmail = async (req: Request, res: Response) => {
const { code } = req.params || {};
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",
},
});
}
const team = await prisma.team.update({
where: {
pid: acc,
},
data: {
verified: true,
},
});
mailClient.set(code, "");
const token = generateTeamleaderJWT(team);
res.cookie("teamLeaderToken", token, {
path: "/",
maxAge: 1000 * 60 * 60 * 24 * 4,
});
res.status(200).json({ type: "succes", payload: { token } }); //TODO: This needs to set a cookie or smth so that the client also gets this info
};