Skip to content

Commit 97f93e0

Browse files
committed
fix(auth): restric readonly users in user routes
1 parent 3c2c4ad commit 97f93e0

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

src/modules/user/user.repository.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ export function findUserById(id: string) {
1111
and(
1212
eq(usersTable.id, id),
1313
eq(usersTable.isActive, true),
14-
isNull(usersTable.deletedAt)
15-
)
14+
isNull(usersTable.deletedAt),
15+
),
1616
)
1717
.limit(1);
1818
}
@@ -25,8 +25,8 @@ export function findUserByEmail(email: string) {
2525
and(
2626
eq(usersTable.email, email),
2727
eq(usersTable.isActive, true),
28-
isNull(usersTable.deletedAt)
29-
)
28+
isNull(usersTable.deletedAt),
29+
),
3030
)
3131
.limit(1);
3232
}
@@ -52,13 +52,7 @@ export function createUser(data: {
5252

5353
export function updateUser(
5454
userId: string,
55-
data: Partial<{
56-
username: string;
57-
bio: string;
58-
avatarUrl: string;
59-
isActive: boolean;
60-
role: "admin" | "author" | "user";
61-
}>
55+
data: Partial<typeof usersTable.$inferInsert>,
6256
) {
6357
return db
6458
.update(usersTable)

src/modules/user/user.routes.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// src/modules/user/user.routes.ts
22
import { Router } from "express";
3-
import { requireAuth, requireRole } from "@/middlewares/auth.middleware.js";
3+
import {
4+
blockReadOnly,
5+
requireAuth,
6+
requireRole,
7+
} from "@/middlewares/auth.middleware.js";
48
import { Role } from "@/constants/roles.js";
59
import * as UserController from "./user.controller.js";
610
import { validateParams } from "@/middlewares/validate.middleware.js";
@@ -12,22 +16,23 @@ userRoutes.get(
1216
"/",
1317
requireAuth,
1418
requireRole(Role.ADMIN),
15-
UserController.adminListUsers
19+
UserController.adminListUsers,
1620
);
1721

18-
userRoutes.put("/me", requireAuth, UserController.updateMe);
22+
userRoutes.put("/me", requireAuth, blockReadOnly, UserController.updateMe);
1923

2024
userRoutes.put(
2125
"/:userId",
2226
requireAuth,
2327
requireRole(Role.ADMIN),
28+
blockReadOnly,
2429
validateParams(userIdParamSchema),
25-
UserController.adminUpdateUser
30+
UserController.adminUpdateUser,
2631
);
2732
userRoutes.get(
2833
"/me/avatar/upload",
2934
requireAuth,
30-
UserController.getAvatarUpload
35+
UserController.getAvatarUpload,
3136
);
3237

3338
userRoutes.put("/me/avatar", requireAuth, UserController.updateAvatar);

src/modules/user/user.service.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,21 @@ import { AuthUser } from "@/@types/auth.js";
66
import { cloudinary } from "@/config/cloudinary.js";
77
import crypto from "node:crypto";
88

9-
export async function updateMe(user: AuthUser, input: any) {
9+
export async function updateMe(
10+
user: AuthUser,
11+
input: { username?: string; bio?: string },
12+
) {
1013
const [updated] = await UserRepo.updateUser(user.id, input);
1114
return updated;
1215
}
1316

1417
export async function adminUpdateUser(
1518
admin: AuthUser,
1619
targetUserId: string,
17-
input: any
20+
input: {
21+
username?: string;
22+
bio?: string;
23+
},
1824
) {
1925
if (!canUpdateUser(admin, targetUserId)) {
2026
throw new ForbiddenError();
@@ -42,7 +48,7 @@ export function getAvatarUploadSignature(userId: string) {
4248
public_id: publicId,
4349
folder: "avatars",
4450
},
45-
cloudinary.config().api_secret!
51+
cloudinary.config().api_secret!,
4652
);
4753

4854
return {
@@ -71,7 +77,7 @@ export async function adminListUsers(query: any) {
7177
pageInfo: {
7278
hasNextPage,
7379
nextCursor: hasNextPage
74-
? items[items.length - 1].createdAt?.toISOString() ?? null
80+
? (items[items.length - 1].createdAt?.toISOString() ?? null)
7581
: null,
7682
},
7783
};

0 commit comments

Comments
 (0)