Skip to content

Commit 3e9cfa3

Browse files
Added user deletion endpoint
1 parent dd9f47e commit 3e9cfa3

11 files changed

Lines changed: 49 additions & 1 deletion

File tree

database.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ CREATE TABLE IF NOT EXISTS "users" (
5050
"firstname" varchar(255) NOT NULL,
5151
"lastname" varchar(255) NOT NULL,
5252
"avatar" varchar(255) DEFAULT NULL,
53+
"status" varchar(255) NOT NULL,
5354
"timestamp" double NOT NULL,
5455
PRIMARY KEY("id")
5556
);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export async function deleteTokensByUser(database: D1Database, user: string): Promise<void> {
2+
await database.prepare("DELETE FROM tokens WHERE user = ?").bind(user).run();
3+
};
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import { UserStatus } from "../../models/UserStatus";
12
import { User } from "../../models/user";
23
import { getUserById } from "./getUserById";
34

45
export async function createUser(database: D1Database, firstname: string, lastname: string, email: string, password: string): Promise<User> {
56
const id = crypto.randomUUID();
67
const timestamp = Date.now();
78

8-
await database.prepare("INSERT INTO users (id, firstname, lastname, email, password, avatar, timestamp) VALUES (?, ?, ?, ?, ?, ?, ?)").bind(id, firstname, lastname, email, password, "f75ffb2d-ecfe-4cf3-dee1-a36f00314a00", timestamp).run();
9+
await database.prepare("INSERT INTO users (id, firstname, lastname, email, password, avatar, status, timestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?)").bind(id, firstname, lastname, email, password, "f75ffb2d-ecfe-4cf3-dee1-a36f00314a00", "CREATED" as UserStatus, timestamp).run();
910

1011
return await getUserById(database, id);
1112
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { UserStatus } from "../../models/UserStatus";
2+
3+
export async function setUserStatus(database: D1Database, id: string, status: UserStatus): Promise<void> {
4+
await database.prepare("UPDATE users SET status = ? WHERE id = ?").bind(status, id).run();
5+
};

src/domains/router.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { handleMapsRouteRequest, mapsRouteSchema } from "../routes/maps/route";
3434
import { handleStatusRequest, statusRequestSchema } from "../routes/status";
3535
import { Token } from "../models/token";
3636
import { createMessageRequestSchema, handleCreateMessageRequest } from "../routes/message";
37+
import { handleUserDeletionRequest } from "../routes/user/delete";
3738

3839
export default function createRouter() {
3940
const router = ThrowableRouter();
@@ -76,6 +77,7 @@ export default function createRouter() {
7677
router.post("/api/profiles/:userId/bikes", withAuth, withParams, withContent, withSchema(profileBikesRequestSchema), handleProfileBikesRequest);
7778

7879
router.post("/api/user/avatar", withAuth, withContent, withSchema(uploadUserImageRequestSchema), handleUploadUserAvatarRequest);
80+
router.delete("/api/user/delete", withAuth, handleUserDeletionRequest);
7981

8082
router.post("/api/message", withAuth, withContent, withSchema(createMessageRequestSchema), handleCreateMessageRequest);
8183

src/models/UserStatus.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type UserStatus = "CREATED" | "DELETING" | "DELETED";

src/models/user.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { UserStatus } from "./UserStatus";
2+
13
export type User = {
24
id: string;
35

@@ -8,6 +10,7 @@ export type User = {
810
lastname: string;
911

1012
avatar: string;
13+
status: UserStatus;
1114

1215
timestamp: number;
1316
};

src/routes/auth/login.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ export async function handleAuthLoginRequest(request: RequestWithKey, env: Env,
2727
if(user === null)
2828
return Response.json({ success: false, message: "This email is not registered to anyone." });
2929

30+
if(user.status === "DELETING")
31+
return Response.json({ success: false, message: "Your account is being processed to be deleted upon your request."});
32+
3033
if(!(await verifyPassword(password, user.password)))
3134
return Response.json({ success: false, message: "Your credentials do not match." });
3235

src/routes/auth/login/verify.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ export async function handleAuthLoginVerificationRequest(request: RequestWithKey
3737
if(user === null)
3838
return Response.json({ success: false, message: "User no longer exists." });
3939

40+
if(user.status === "DELETING")
41+
return Response.json({ success: false, message: "User is being deleted." });
42+
4043
const keyArray = new Uint8Array(64);
4144
crypto.getRandomValues(keyArray);
4245
const key = Array.from(keyArray, (decimal) => decimal.toString(16).padStart(2, '0')).join('');

src/routes/auth/renew.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ export async function handleAuthRenewRequest(request: RequestWithKey, env: Env)
1010
if(!user)
1111
return Response.json({ success: false });
1212

13+
if(user.status === "DELETING")
14+
return Response.json({ success: false });
15+
1316
const keyArray = new Uint8Array(64);
1417
crypto.getRandomValues(keyArray);
1518
const key = Array.from(keyArray, (decimal) => decimal.toString(16).padStart(2, '0')).join('');

0 commit comments

Comments
 (0)