Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ model User {
phone String?
role UserRole @default(USER)
isVerified Boolean @default(false) @map("is_verified")
isBlocked Boolean @default(false) @map("is_blocked")
twoFactorEnabled Boolean @default(false) @map("two_factor_enabled")
twoFactorSecret String? @map("two_factor_secret")
twoFactorBackupCodes String[] @default([]) @map("two_factor_backup_codes")
Expand Down
12 changes: 12 additions & 0 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@
throw new UnauthorizedException('Invalid credentials');
}

if (user.isBlocked) {
throw new UnauthorizedException('Your account has been blocked. Please contact support.');
}

const passwordMatches = await comparePassword(data.password, user.password);
if (!passwordMatches) {
throw new UnauthorizedException('Invalid credentials');
Expand Down Expand Up @@ -166,6 +170,10 @@
throw new UnauthorizedException('User no longer exists');
}

if (user.isBlocked) {
throw new UnauthorizedException('Your account has been blocked');
}

if (user.id !== payload.sub) {
throw new UnauthorizedException('Refresh token does not match the authenticated user');
}
Expand Down Expand Up @@ -410,7 +418,7 @@
orderBy: { createdAt: 'desc' },
});

return apiKeys.map((apiKey: any) => this.toApiKeyResponse(apiKey));

Check warning on line 421 in src/auth/auth.service.ts

View workflow job for this annotation

GitHub Actions / Lint & Build

Unexpected any. Specify a different type
}

async rotateApiKey(user: AuthUserPayload, apiKeyId: string) {
Expand Down Expand Up @@ -491,6 +499,10 @@
throw new UnauthorizedException('Invalid API key');
}

if (apiKey.user.isBlocked) {
throw new UnauthorizedException('User account is blocked');
}

await this.prisma.apiKey.update({
where: { id: apiKey.id },
data: {
Expand All @@ -506,7 +518,7 @@
};
}

private async issueTokenPair(user: any) {

Check warning on line 521 in src/auth/auth.service.ts

View workflow job for this annotation

GitHub Actions / Lint & Build

Unexpected any. Specify a different type
const accessJti = randomUUID();
const refreshJti = randomUUID();

Expand Down Expand Up @@ -588,7 +600,7 @@
return `pc_${randomToken(24)}`;
}

private toApiKeyResponse(apiKey: any) {

Check warning on line 603 in src/auth/auth.service.ts

View workflow job for this annotation

GitHub Actions / Lint & Build

Unexpected any. Specify a different type
return {
id: apiKey.id,
name: apiKey.name,
Expand Down
10 changes: 10 additions & 0 deletions src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ export class UsersController {
return this.usersService.update(id, updateUserDto);
}

@Post(':id/block')
block(@Param('id') id: string) {
return this.usersService.block(id);
}

@Post(':id/unblock')
unblock(@Param('id') id: string) {
return this.usersService.unblock(id);
}

@Delete(':id')
remove(@Param('id') id: string) {
return this.usersService.remove(id);
Expand Down
24 changes: 24 additions & 0 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,30 @@ export class UsersService {
});
}

async block(id: string) {
return this.prisma.user.update({
where: { id },
data: { isBlocked: true },
select: {
id: true,
email: true,
isBlocked: true,
},
});
}

async unblock(id: string) {
return this.prisma.user.update({
where: { id },
data: { isBlocked: false },
select: {
id: true,
email: true,
isBlocked: true,
},
});
}

async remove(id: string) {
return this.prisma.user.delete({
where: { id },
Expand Down
Loading