Skip to content

Commit a14995b

Browse files
committed
refactor: improve JWT guard role-based authentication logic
- Restructure JWT guard to use switch statement for role-based authentication
1 parent cc9e7f1 commit a14995b

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

.env.sample

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ API_INSTANCES=1
1313
DATABASE_URL="postgresql://postgres:postgres@remnawave-db:5432/postgres"
1414

1515
### REDIS ###
16-
# FORMAT: redis://{user}:{password}@{host}:{port}/{database}
1716
REDIS_HOST=remnawave-redis
1817
REDIS_PORT=6379
1918

src/common/guards/jwt-guards/def-jwt-guard.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,32 @@ export class JwtDefaultGuard extends AuthGuard('registeredUserJWT') {
2828
return false;
2929
}
3030

31-
if (ROLE.API) {
32-
const token = await this.getTokenByUuid({ uuid: user.uuid });
33-
if (!token.isOk) {
34-
return false;
31+
switch (user.role) {
32+
case ROLE.API: {
33+
const token = await this.getTokenByUuid({ uuid: user.uuid });
34+
if (!token.isOk) {
35+
return false;
36+
}
37+
return true;
3538
}
39+
case ROLE.ADMIN: {
40+
if (!user.username) {
41+
return false;
42+
}
3643

37-
return true;
38-
}
39-
40-
if (user.role === ROLE.ADMIN) {
41-
if (!user.username) {
42-
return false;
43-
}
44+
const adminEntity = await this.getAdminByUsername({
45+
username: user.username,
46+
role: user.role,
47+
});
4448

45-
const adminEntity = await this.getAdminByUsername({
46-
username: user.username,
47-
role: user.role,
48-
});
49-
50-
if (!adminEntity.isOk || !adminEntity.response) {
51-
return false;
52-
}
49+
if (!adminEntity.isOk || !adminEntity.response) {
50+
return false;
51+
}
5352

54-
if (adminEntity.response.uuid !== user.uuid) {
55-
return false;
53+
if (adminEntity.response.uuid !== user.uuid) {
54+
return false;
55+
}
56+
break;
5657
}
5758
}
5859

0 commit comments

Comments
 (0)