Skip to content

Commit 59f978c

Browse files
committed
Only show user list to logged in users with appropriate permissions
1 parent c6f7a94 commit 59f978c

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

client/src/util/api-client.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export class AuthEndpoints {
9090
static async getLoggedInUser(): Promise<LoggedInUserInfo> {
9191
const token = loadIdToken();
9292
if (token) {
93-
return callServer<null, JsonMimeType, LoggedInUserInfo>("/api/auth/loggedInUser/", "POST", "application/json");
93+
return callServer<null, JsonMimeType, LoggedInUserInfo>("/api/utility/loggedInUser", "POST", "application/json");
9494
}
9595
return Promise.reject();
9696
}

server/src/routes/admin.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
import { toProviderId, UserWithOAuthProviders } from "@fumix/fu-blog-common";
1+
import { LoggedInUserInfo, toProviderId, UserWithOAuthProviders } from "@fumix/fu-blog-common";
2+
import { authMiddleware } from "../service/middleware/auth.js";
23
import express, { Request, Response, Router } from "express";
34
import { AppDataSource } from "../data-source.js";
45
import { OAuthAccountEntity } from "../entity/OAuthAccount.entity.js";
56

67
const router: Router = express.Router();
78

8-
router.get("/users", async (req, res, next) => {
9+
router.get("/users", authMiddleware, async (req, res, next) => {
10+
const loggedInUser: LoggedInUserInfo | undefined = await req.loggedInUser?.();
11+
if (loggedInUser?.permissions?.canEditUserRoles ?? true) {
12+
return res.status(401).json({ message: "Unauthorized" });
13+
}
14+
915
await AppDataSource.manager
1016
.getRepository(OAuthAccountEntity)
1117
.find({ relations: { user: true }, order: { user: { id: "ASC" } } })

server/src/routes/auth.ts

-10
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,6 @@ async function getAuthorizationUrl(
9797
}
9898
}
9999

100-
router.post("/loggedInUser", authMiddleware, async (req, res) => {
101-
const account = await req.loggedInUser?.();
102-
103-
if (account) {
104-
res.status(200).json(account);
105-
} else {
106-
res.status(403).json({ error: "Unauthorized" });
107-
}
108-
});
109-
110100
/**
111101
* Endpoint to get a {@link OAuthUserInfoDto}.
112102
*

server/src/routes/utility.ts

+10
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,14 @@ router.post("/dallEGenerateImage", authMiddleware, async (req, res, next) => {
116116
.catch((e) => res.status(502).json({ error: e }));
117117
});
118118

119+
router.post("/loggedInUser", authMiddleware, async (req, res) => {
120+
const account = await req.loggedInUser?.();
121+
122+
if (account) {
123+
res.status(200).json(account);
124+
} else {
125+
res.status(403).json({ error: "Unauthorized" });
126+
}
127+
});
128+
119129
export default router;

0 commit comments

Comments
 (0)