Skip to content
Snippets Groups Projects
Unverified Commit e7c0dd50 authored by Marius Friess's avatar Marius Friess Committed by GitHub
Browse files

Add delete account functionality (#2)


* Add delete account functionality

* Add delete constraint to category

---------

Co-authored-by: default avatarFlorian Raith <florianraith00@gmail.com>
parent 5dbbf042
No related branches found
No related tags found
No related merge requests found
......@@ -177,6 +177,7 @@
"id"
],
"referencedTableName": "users",
"deleteRule": "cascade",
"updateRule": "cascade"
}
}
......
import { Migration } from '@mikro-orm/migrations';
export class Migration20230608100226 extends Migration {
async up(): Promise<void> {
this.addSql('alter table `categories` drop foreign key `categories_owner_id_foreign`;');
this.addSql('alter table `categories` add constraint `categories_owner_id_foreign` foreign key (`owner_id`) references `users` (`id`) on update cascade on delete cascade;');
}
async down(): Promise<void> {
this.addSql('alter table `categories` drop foreign key `categories_owner_id_foreign`;');
this.addSql('alter table `categories` add constraint `categories_owner_id_foreign` foreign key (`owner_id`) references `users` (`id`) on update cascade;');
}
}
......@@ -5,6 +5,7 @@ import {
HttpCode,
HttpStatus,
Post,
Delete,
Res,
UseGuards,
} from '@nestjs/common';
......@@ -59,4 +60,16 @@ export class AuthController {
return { message: 'success' };
}
@UseGuards(AuthGuard)
@Delete('delete')
public async delete(@Res({ passthrough: true }) response: Response) {
const user = await this.auth.user();
const id = user.id;
await this.auth.delete(id);
response.clearCookie('jwt');
return { message: 'success' };
}
}
......@@ -34,6 +34,10 @@ export class AuthService {
return this.createToken(user);
}
public async delete(id: number): Promise<void> {
return await this.users.delete(id);
}
private createToken(user: User): AuthPayload {
const payload = { sub: user.id };
const token = this.jwtService.sign(payload);
......
......@@ -17,7 +17,7 @@ export class Category {
@Property()
name: string;
@ManyToOne({ hidden: true })
@ManyToOne({ hidden: true, onDelete: 'cascade' })
owner: User;
@OneToMany(() => Room, (room) => room.category)
......
......@@ -33,4 +33,7 @@ export class UserService {
return user;
}
public async delete(id: number): Promise<void> {
await this.repository.nativeDelete({ id });
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment