Skip to content
This repository was archived by the owner on Oct 5, 2024. It is now read-only.

Commit 60dd1e1

Browse files
Resolves #13
1 parent d5629c0 commit 60dd1e1

File tree

6 files changed

+80
-16
lines changed

6 files changed

+80
-16
lines changed

backend/prisma/schema.prisma

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ datasource db {
1111
model User {
1212
id String @id
1313
email String @unique
14-
username String
14+
username String @unique
15+
name String
1516
hashedPassword String
1617
sessions Session[]
1718
messages Message[]

backend/src/index.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import express from "express";
1+
import cookieParser from "cookie-parser";
22
import * as dotenv from "dotenv";
3-
import cors from "cors";
3+
import express from "express";
44
import { lucia } from "./lib/auth.js";
5+
import { existsRouter } from "./routes/auth/exists.js";
6+
import { loginRouter } from "./routes/auth/login.js";
7+
import { logoutRouter } from "./routes/auth/logout.js";
8+
import { signupRouter } from "./routes/auth/signup.js";
59
import { mainRouter } from "./routes/index.js";
6-
import { loginRouter } from "./routes/login/route.js";
7-
import { signupRouter } from "./routes/signup/route.js";
8-
import { logoutRouter } from "./routes/logout/index.js";
9-
import cookieParser from "cookie-parser";
1010

1111
dotenv.config();
1212

@@ -37,7 +37,7 @@ app.use(async (req, res, next) => {
3737
return next();
3838
});
3939

40-
app.use(mainRouter, loginRouter, logoutRouter, signupRouter);
40+
app.use(mainRouter, loginRouter, logoutRouter, signupRouter, existsRouter);
4141

4242
app.listen(PORT, () => {
4343
console.log(`Listening on port ${PORT}`);

backend/src/routes/auth/exists.ts

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import express from "express";
2+
import { z } from "zod";
3+
import { prisma } from "../../lib/prisma.js";
4+
5+
export const existsRouter = express.Router();
6+
7+
existsRouter.post("/auth/exists", async (req, res) => {
8+
if (!req.body) {
9+
return res.status(400).json({ error: { message: "No input provided" } });
10+
}
11+
12+
const schema = z.object({
13+
email: z.string().email().optional(),
14+
username: z.string().min(3).max(30).optional(),
15+
});
16+
17+
const parsed = schema.safeParse(req.body);
18+
if (!parsed.success) {
19+
const validationErrors = parsed.error.flatten().fieldErrors;
20+
return res.status(400).json({ error: { message: "Wrong input", errors: validationErrors } });
21+
}
22+
const { email, username } = parsed.data;
23+
24+
let existingUser;
25+
if (email != null && username != null) {
26+
existingUser = await prisma.user.findUnique({
27+
where: {
28+
email,
29+
username,
30+
},
31+
});
32+
} else if (email != null) {
33+
existingUser = await prisma.user.findUnique({
34+
where: {
35+
email,
36+
},
37+
});
38+
} else if (username != null) {
39+
existingUser = await prisma.user.findUnique({
40+
where: {
41+
username,
42+
},
43+
});
44+
}
45+
46+
if (existingUser) {
47+
return res.status(200).json({ exists: true });
48+
} else {
49+
return res.status(200).json({ exists: false });
50+
}
51+
});

backend/src/routes/login/route.ts backend/src/routes/auth/login.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import express from "express";
22
import { Argon2id } from "oslo/password";
3-
import { lucia } from "../../lib/auth.js";
43
import { z } from "zod";
4+
import { lucia } from "../../lib/auth.js";
55
import { prisma } from "../../lib/prisma.js";
66

77
export const loginRouter = express.Router();
@@ -14,14 +14,15 @@ loginRouter.post("/auth/login", async (req, res) => {
1414
const schema = z.object({
1515
email: z.string().email(),
1616
password: z.string(),
17+
persistent: z.boolean(),
1718
});
1819

1920
const parsed = schema.safeParse(req.body);
2021
if (!parsed.success) {
2122
const validationErrors = parsed.error.flatten().fieldErrors;
2223
return res.status(400).json({ error: { message: "Wrong input", errors: validationErrors } });
2324
}
24-
const { email, password } = parsed.data;
25+
const { email, password, persistent } = parsed.data;
2526

2627
const existingUser = await prisma.user.findUnique({
2728
where: {
@@ -39,5 +40,9 @@ loginRouter.post("/auth/login", async (req, res) => {
3940

4041
const session = await lucia.createSession(existingUser.id, {});
4142
const sessionCookie = lucia.createSessionCookie(session.id);
42-
return res.appendHeader("Set-Cookie", sessionCookie.serialize()).status(200).send("Success");
43+
if (persistent == true) {
44+
return res.cookie(sessionCookie.name, sessionCookie.value, { maxAge: 7776000000 }).status(200).send("Success");
45+
} else {
46+
return res.cookie(sessionCookie.name, sessionCookie.value).status(200).send("Success");
47+
}
4348
});

backend/src/routes/logout/index.ts backend/src/routes/auth/logout.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ logoutRouter.post("/auth/logout", async (req, res) => {
1010

1111
await lucia.invalidateSession(res.locals.session.id);
1212
const sessionCookie = lucia.createBlankSessionCookie();
13-
return res.appendHeader("Set-Cookie", sessionCookie.serialize()).status(200).send("Success");
13+
return res.cookie(sessionCookie.name, sessionCookie.value).status(200).send("Success");
1414
});

backend/src/routes/signup/route.ts backend/src/routes/auth/signup.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import express from "express";
2+
import { generateId } from "lucia";
23
import { Argon2id } from "oslo/password";
4+
import { z } from "zod";
35
import { lucia } from "../../lib/auth.js";
4-
import { generateId } from "lucia";
56
import { prisma } from "../../lib/prisma.js";
6-
import { z } from "zod";
77
import { formatPrismaError } from "../../lib/utils.js";
88

99
export const signupRouter = express.Router();
@@ -16,15 +16,17 @@ signupRouter.post("/auth/signup", async (req, res) => {
1616
const schema = z.object({
1717
email: z.string().email(),
1818
username: z.string().min(3).max(30),
19+
name: z.string().min(1).max(50),
1920
password: z.string().min(6).max(255),
21+
persistent: z.boolean(),
2022
});
2123

2224
const parsed = schema.safeParse(req.body);
2325
if (!parsed.success) {
2426
const validationErrors = parsed.error.flatten().fieldErrors;
2527
return res.status(400).json({ error: { message: "Wrong input", errors: validationErrors } });
2628
}
27-
const { email, username, password } = parsed.data;
29+
const { email, username, name, password, persistent } = parsed.data;
2830

2931
const userId = generateId(15);
3032
const hashedPassword = await new Argon2id().hash(password);
@@ -35,6 +37,7 @@ signupRouter.post("/auth/signup", async (req, res) => {
3537
id: userId,
3638
email,
3739
username,
40+
name,
3841
hashedPassword,
3942
},
4043
});
@@ -46,5 +49,9 @@ signupRouter.post("/auth/signup", async (req, res) => {
4649

4750
const session = await lucia.createSession(userId, {});
4851
const sessionCookie = lucia.createSessionCookie(session.id);
49-
return res.appendHeader("Set-Cookie", sessionCookie.serialize()).status(201).send("Success");
52+
if (persistent == true) {
53+
return res.cookie(sessionCookie.name, sessionCookie.value, { maxAge: 7776000000 }).status(200).send("Success");
54+
} else {
55+
return res.cookie(sessionCookie.name, sessionCookie.value).status(200).send("Success");
56+
}
5057
});

0 commit comments

Comments
 (0)