Skip to content

Commit 64045c5

Browse files
authored
Next (#184)
* basic onboard * remove ant design * checks * internal users + auth * admin check
1 parent 09f94ff commit 64045c5

File tree

37 files changed

+886
-837
lines changed

37 files changed

+886
-837
lines changed

apps/api/src/controllers/auth.ts

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ export function authRoutes(fastify: FastifyInstance) {
9898
throw new Error("Password is not valid");
9999
}
100100

101-
var b64string = "TOMATOSOUP";
102-
var buf = new Buffer(b64string, "base64"); // Ta-da
101+
var b64string = process.env.SECRET;
102+
var buf = new Buffer(b64string!, "base64"); // Ta-da
103103

104104
let token = jwt.sign(
105105
{
106106
data: { id: user!.id },
107107
},
108108
buf,
109-
{ expiresIn: "1d" }
109+
{ expiresIn: "7d" }
110110
);
111111

112112
await prisma.session.create({
@@ -127,6 +127,7 @@ export function authRoutes(fastify: FastifyInstance) {
127127
ticket_status_changed: user!.notify_ticket_status_changed,
128128
ticket_comments: user!.notify_ticket_comments,
129129
ticket_assigned: user!.notify_ticket_assigned,
130+
firstLogin: user!.firstLogin,
130131
};
131132

132133
reply.send({
@@ -140,25 +141,25 @@ export function authRoutes(fastify: FastifyInstance) {
140141
fastify.delete(
141142
"/api/v1/auth/user/:id",
142143
async (request: FastifyRequest, reply: FastifyReply) => {
143-
const { id } = request.params as { id: string };
144+
const bearer = request.headers.authorization!.split(" ")[1];
145+
const token = checkToken(bearer);
144146

145-
await prisma.user.delete({
146-
where: { id },
147-
});
147+
if (token) {
148+
const { id } = request.params as { id: string };
149+
150+
await prisma.user.delete({
151+
where: { id },
152+
});
148153

149-
reply.send({ success: true });
154+
reply.send({ success: true });
155+
}
150156
}
151157
);
152158

153159
// User Profile
154160
fastify.get(
155161
"/api/v1/auth/profile",
156162
async (request: FastifyRequest, reply: FastifyReply) => {
157-
// check token
158-
// see if token exists on session table
159-
// if not, return 401
160-
// if yes, return user data
161-
162163
const bearer = request.headers.authorization!.split(" ")[1];
163164

164165
const token = checkToken(bearer);
@@ -210,8 +211,6 @@ export function authRoutes(fastify: FastifyInstance) {
210211
};
211212

212213
const bearer = request.headers.authorization!.split(" ")[1];
213-
214-
//checks if token is valid and returns valid token
215214
const token = checkToken(bearer);
216215

217216
if (token) {
@@ -245,21 +244,59 @@ export function authRoutes(fastify: FastifyInstance) {
245244
fastify.put(
246245
"/api/v1/auth/profile",
247246
async (request: FastifyRequest, reply: FastifyReply) => {
248-
//
247+
const bearer = request.headers.authorization!.split(" ")[1];
248+
249+
//checks if token is valid and returns valid token
250+
const token = checkToken(bearer);
251+
252+
if (token) {
253+
let session = await prisma.session.findUnique({
254+
where: {
255+
sessionToken: bearer,
256+
},
257+
});
258+
259+
const { name, email, language } = request.body as {
260+
name: string;
261+
email: string;
262+
language: string;
263+
};
264+
265+
let user = await prisma.user.update({
266+
where: { id: session?.userId },
267+
data: {
268+
name: name,
269+
email: email,
270+
language: language,
271+
},
272+
});
273+
274+
reply.send({
275+
user,
276+
});
277+
} else {
278+
reply.send({
279+
sucess: false,
280+
});
281+
}
249282
}
250283
);
251284

252285
// Logout a user (deletes session)
253286
fastify.get(
254287
"/api/v1/auth/user/:id/logout",
255288
async (request: FastifyRequest, reply: FastifyReply) => {
256-
const { id } = request.params as { id: string };
289+
const bearer = request.headers.authorization!.split(" ")[1];
290+
const token = checkToken(bearer);
291+
if (token) {
292+
const { id } = request.params as { id: string };
257293

258-
await prisma.session.deleteMany({
259-
where: { userId: id },
260-
});
294+
await prisma.session.deleteMany({
295+
where: { userId: id },
296+
});
261297

262-
reply.send({ success: true });
298+
reply.send({ success: true });
299+
}
263300
}
264301
);
265302
}
Lines changed: 62 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
2+
import { checkToken } from "../lib/jwt";
23
import { prisma } from "../prisma";
34

45
export function clientRoutes(fastify: FastifyInstance) {
@@ -7,20 +8,25 @@ export function clientRoutes(fastify: FastifyInstance) {
78
"/api/v1/client/create",
89

910
async (request: FastifyRequest, reply: FastifyReply) => {
10-
const { name, email, number, contactName }: any = request.body;
11-
12-
await prisma.client.create({
13-
data: {
14-
name,
15-
contactName,
16-
email,
17-
number: String(number),
18-
},
19-
});
20-
21-
reply.send({
22-
success: true,
23-
});
11+
const bearer = request.headers.authorization!.split(" ")[1];
12+
const token = checkToken(bearer);
13+
14+
if (token) {
15+
const { name, email, number, contactName }: any = request.body;
16+
17+
await prisma.client.create({
18+
data: {
19+
name,
20+
contactName,
21+
email,
22+
number: String(number),
23+
},
24+
});
25+
26+
reply.send({
27+
success: true,
28+
});
29+
}
2430
}
2531
);
2632

@@ -29,21 +35,26 @@ export function clientRoutes(fastify: FastifyInstance) {
2935
"/api/v1/client/update",
3036

3137
async (request: FastifyRequest, reply: FastifyReply) => {
32-
const { name, email, number, contactName, id }: any = request.body;
33-
34-
await prisma.client.update({
35-
where: { id: id },
36-
data: {
37-
name,
38-
contactName,
39-
email,
40-
number: String(number),
41-
},
42-
});
43-
44-
reply.send({
45-
success: true,
46-
});
38+
const bearer = request.headers.authorization!.split(" ")[1];
39+
const token = checkToken(bearer);
40+
41+
if (token) {
42+
const { name, email, number, contactName, id }: any = request.body;
43+
44+
await prisma.client.update({
45+
where: { id: id },
46+
data: {
47+
name,
48+
contactName,
49+
email,
50+
number: String(number),
51+
},
52+
});
53+
54+
reply.send({
55+
success: true,
56+
});
57+
}
4758
}
4859
);
4960

@@ -52,12 +63,17 @@ export function clientRoutes(fastify: FastifyInstance) {
5263
"/api/v1/clients/all",
5364

5465
async (request: FastifyRequest, reply: FastifyReply) => {
55-
const clients = await prisma.client.findMany({});
66+
const bearer = request.headers.authorization!.split(" ")[1];
67+
const token = checkToken(bearer);
5668

57-
reply.send({
58-
success: true,
59-
clients: clients,
60-
});
69+
if (token) {
70+
const clients = await prisma.client.findMany({});
71+
72+
reply.send({
73+
success: true,
74+
clients: clients,
75+
});
76+
}
6177
}
6278
);
6379

@@ -66,15 +82,20 @@ export function clientRoutes(fastify: FastifyInstance) {
6682
"/api/v1/clients/:id/delete-client",
6783

6884
async (request: FastifyRequest, reply: FastifyReply) => {
69-
const { id }: any = request.params;
85+
const bearer = request.headers.authorization!.split(" ")[1];
86+
const token = checkToken(bearer);
87+
88+
if (token) {
89+
const { id }: any = request.params;
7090

71-
await prisma.client.delete({
72-
where: { id: id },
73-
});
91+
await prisma.client.delete({
92+
where: { id: id },
93+
});
7494

75-
reply.send({
76-
success: true,
77-
});
95+
reply.send({
96+
success: true,
97+
});
98+
}
7899
}
79100
);
80101
}

apps/api/src/controllers/data.ts

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
2+
import { checkToken } from "../lib/jwt";
23
import { prisma } from "../prisma";
34

45
export function dataRoutes(fastify: FastifyInstance) {
@@ -7,8 +8,14 @@ export function dataRoutes(fastify: FastifyInstance) {
78
"/api/v1/data/tickets/all",
89

910
async (request: FastifyRequest, reply: FastifyReply) => {
10-
// check jwt is valid
11-
// check user is admin
11+
const bearer = request.headers.authorization!.split(" ")[1];
12+
const token = checkToken(bearer);
13+
14+
if (token) {
15+
const result = await prisma.ticket.count();
16+
17+
reply.send({ count: result });
18+
}
1219
}
1320
);
1421

@@ -17,11 +24,16 @@ export function dataRoutes(fastify: FastifyInstance) {
1724
"/api/v1/data/tickets/completed",
1825

1926
async (request: FastifyRequest, reply: FastifyReply) => {
20-
const result = await prisma.ticket.count({
21-
where: { isComplete: true },
22-
});
27+
const bearer = request.headers.authorization!.split(" ")[1];
28+
const token = checkToken(bearer);
29+
30+
if (token) {
31+
const result = await prisma.ticket.count({
32+
where: { isComplete: true },
33+
});
2334

24-
reply.send({ count: result });
35+
reply.send({ count: result });
36+
}
2537
}
2638
);
2739

@@ -30,11 +42,16 @@ export function dataRoutes(fastify: FastifyInstance) {
3042
"/api/v1/data/tickets/open",
3143

3244
async (request: FastifyRequest, reply: FastifyReply) => {
33-
const result = await prisma.ticket.count({
34-
where: { isComplete: false },
35-
});
45+
const bearer = request.headers.authorization!.split(" ")[1];
46+
const token = checkToken(bearer);
3647

37-
reply.send({ count: result });
48+
if (token) {
49+
const result = await prisma.ticket.count({
50+
where: { isComplete: false },
51+
});
52+
53+
reply.send({ count: result });
54+
}
3855
}
3956
);
4057

@@ -43,11 +60,16 @@ export function dataRoutes(fastify: FastifyInstance) {
4360
"/api/v1/data/tickets/unassigned",
4461

4562
async (request: FastifyRequest, reply: FastifyReply) => {
46-
const result = await prisma.ticket.count({
47-
where: { userId: null },
48-
});
63+
const bearer = request.headers.authorization!.split(" ")[1];
64+
const token = checkToken(bearer);
65+
66+
if (token) {
67+
const result = await prisma.ticket.count({
68+
where: { userId: null },
69+
});
4970

50-
reply.send({ count: result });
71+
reply.send({ count: result });
72+
}
5173
}
5274
);
5375
}

0 commit comments

Comments
 (0)