fucking DELETE method for account is now working and existing

This commit is contained in:
cherif
2023-01-17 15:57:43 +01:00
parent 44f9f8e21b
commit c2812c0534
4 changed files with 32 additions and 7 deletions

View File

@@ -146,6 +146,7 @@ export class ChatService {
const user_db = await this.getUserByName(username); const user_db = await this.getUserByName(username);
printCaller("-- out "); printCaller("-- out ");
if (user_db)
return user_db.currentRoom; return user_db.currentRoom;
} }

View File

@@ -43,14 +43,14 @@ export class User {
@Column({ nullable: true }) @Column({ nullable: true })
secretTwoFactorAuth: string; secretTwoFactorAuth: string;
@OneToMany(type => Friendship , (friendship) => friendship.sender) @OneToMany(type => Friendship , (friendship) => friendship.sender, {onDelete: 'CASCADE'})
sentFriendRequest: Friendship[]; sentFriendRequest: Friendship[];
@OneToMany(type => Friendship , (friendship) => friendship.receiver) @OneToMany(type => Friendship , (friendship) => friendship.receiver, {onDelete: 'CASCADE'})
receivedFriendRequest: Friendship[]; receivedFriendRequest: Friendship[];
@JoinColumn() @JoinColumn()
@OneToOne(() => UserStats, { cascade: true }) @OneToOne(() => UserStats, { cascade: true, onDelete: 'CASCADE' })
stats: UserStats; stats: UserStats;
// ROOMS : // ROOMS :

View File

@@ -1,5 +1,5 @@
import { import {
Body, Controller, Delete, Get, NotFoundException,HttpStatus, Param, Patch, Post, Query, Redirect, Req, Res, UploadedFile, UseGuards, UseInterceptors Body, Controller, Delete, Get, NotFoundException,HttpStatus, Next, Patch, Post, Query, Redirect, Req, Res, UploadedFile, UseGuards, UseInterceptors
} from '@nestjs/common'; } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express'; import { FileInterceptor } from '@nestjs/platform-express';
import { Response } from 'express'; import { Response } from 'express';
@@ -73,8 +73,14 @@ export class UsersController {
@UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard) @UseGuards(TwoFactorGuard)
@Delete() @Delete()
remove(@Req() req) { remove(@Req() req, @Res() response, @Next() next) {
return this.usersService.remove(req.user.id); this.usersService.remove(req.user.id);
req.logout(function(err) {
if (err) { return next(err); }
response.redirect('/');
});
req.session.cookie.maxAge = 0;
return {msg : 'Your account has been deleted'};
} }

View File

@@ -108,6 +108,23 @@
avatar = await fetchAvatar(user.username); avatar = await fetchAvatar(user.username);
} }
const deleteAccount = async() => {
console.log("deleting account")
await fetch(`http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}/api/v2/user`, {
method: "DELETE",
})
.then((response) => {
if (!response.ok) {
throw new Error("HTTP " + response.status);
}
console.log("account deleted")
push('/');
})
.catch((error) => {
console.log("catch unable to delete: ", error);
});
}
</script> </script>
@@ -152,6 +169,7 @@
</form> </form>
</Card> </Card>
</div> </div>
<Button type="primary" on:click={() => deleteAccount()}>Delete Account</Button>
</div> </div>
</main> </main>