-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add delete user option to user profile
- Loading branch information
Showing
5 changed files
with
98 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { deleteUser, UserWithPasswordHash } from '../../../../database/users'; | ||
|
||
type DeleteUserResponseBody = | ||
| { message: string } | ||
| { message: string; user?: UserWithPasswordHash } | ||
| { message: string; error?: string }; | ||
|
||
export async function DELETE( | ||
request: NextRequest, | ||
): Promise<NextResponse<DeleteUserResponseBody>> { | ||
const { sessionToken } = await request.json(); | ||
|
||
if (!sessionToken) { | ||
return NextResponse.json( | ||
{ message: 'Invalid request', error: 'Session token is missing' }, | ||
{ status: 400 }, | ||
); | ||
} | ||
|
||
try { | ||
const user = await deleteUser(sessionToken); | ||
if (user) { | ||
return NextResponse.json( | ||
{ message: 'User deleted successfully', user }, | ||
{ status: 200 }, | ||
); | ||
} else { | ||
return NextResponse.json( | ||
{ message: 'User not found or session expired' }, | ||
{ status: 404 }, | ||
); | ||
} | ||
} catch (error) { | ||
return NextResponse.json( | ||
{ message: 'Internal server error', error: (error as Error).message }, | ||
{ status: 500 }, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use client'; | ||
|
||
import React from 'react'; | ||
|
||
type DeleteProfileFormProps = { | ||
sessionToken: string; | ||
}; | ||
|
||
export default function DeleteProfileForm({ sessionToken }: DeleteProfileFormProps) { | ||
const handleDelete = async (event: React.FormEvent) => { | ||
event.preventDefault(); | ||
|
||
const response = await fetch('/api/user/delete', { | ||
method: 'DELETE', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ sessionToken }), | ||
}); | ||
|
||
if (response.ok) { | ||
window.location.href = '/login'; // Redirect to login page after deletion | ||
} else { | ||
console.error('Failed to delete user'); | ||
} | ||
}; | ||
|
||
return ( | ||
<form onSubmit={handleDelete}> | ||
<button>Delete Profile</button> | ||
</form> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters