Skip to content

Commit

Permalink
feat: add deleteAccount endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwwinter committed Aug 15, 2024
1 parent ba375f7 commit d0e66cc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/account/account.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Body,
Controller,
Delete,
Get,
Headers,
Param,
Expand Down Expand Up @@ -149,6 +150,28 @@ export class AccountController {
);
}

@ApiOperation({
summary: 'delete an user account',
description:
'Looks if an account with given id exist in realm and deletes it',
})
@ApiBearerAuth()
@ApiHeader({ name: 'Accept-Language', required: false })
@UseGuards(BearerGuard, RolesGuard)
@Roles(AccountController.ACCOUNT_MANAGEMENT_ROLE)
@Delete('/:userId')
deleteAccount(
@Req() req,
@Param('userId') userId: string,
) {
const user = req.user as User;

return this.keycloak.deleteUser(
user.realm,
userId,
)
}

@ApiOperation({
summary: 'get account details',
description:
Expand Down
20 changes: 19 additions & 1 deletion src/account/keycloak.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, NotFoundException } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { ConfigService } from '@nestjs/config';
import { KeycloakUser } from './keycloak-user.dto';
import { map, Observable } from 'rxjs';
import { catchError, map, Observable, of } from 'rxjs';

/**
* This service provides endpoints for interacting with Keycloak.
Expand All @@ -17,6 +17,7 @@ export class KeycloakService {
];

private readonly keycloakUrl: string;

constructor(private http: HttpService, configService: ConfigService) {
this.keycloakUrl = configService.get('KEYCLOAK_URL');
}
Expand All @@ -35,6 +36,23 @@ export class KeycloakService {
);
}

/**
* Delete a user in the realm with the given id
* @param realm
* @param id
*/
deleteUser(realm: string, id: string) {
return this.perform(
this.http.delete,
`${realm}/users/${id}`,
{}
).pipe(
catchError(() => {
return of("")
}),
);
}

/**
* Update the user with the given id in the realm.
* @param realm
Expand Down

0 comments on commit d0e66cc

Please sign in to comment.