-
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 file upload and delete hooks to profilePictures bucket, implement…
… deleteUser function, and add password update route
- Loading branch information
1 parent
d259c34
commit 9b097f2
Showing
7 changed files
with
339 additions
and
76 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
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,63 @@ | ||
import { NextResponse } from "next/server"; | ||
import bcrypt from "bcrypt"; | ||
import { prisma } from "@/prisma/client"; | ||
|
||
type Props = { | ||
currentPassword: string; | ||
newPassword: string; | ||
userId: number; | ||
}; | ||
|
||
export async function PATCH(req: Request) { | ||
const body = await req.json(); | ||
|
||
const { currentPassword, newPassword, userId } = body as Props; | ||
|
||
try { | ||
const user = await prisma.user.findUnique({ | ||
where: { id: userId }, | ||
}); | ||
|
||
if (user === null) { | ||
return new NextResponse( | ||
JSON.stringify({ | ||
error: "Nie znaleziono użytkownika.", | ||
}), | ||
{ status: 404 } | ||
); | ||
} | ||
|
||
const passwordMatch = await bcrypt.compare( | ||
currentPassword, | ||
user.password | ||
); | ||
|
||
if (!passwordMatch) { | ||
return new NextResponse( | ||
JSON.stringify({ | ||
error: "Niepoprawne hasło.", | ||
}), | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
const salt = await bcrypt.genSalt(10); | ||
const hash = await bcrypt.hash(newPassword, salt); | ||
|
||
await prisma.user.update({ | ||
where: { id: userId }, | ||
data: { | ||
password: hash, | ||
}, | ||
}); | ||
|
||
return new NextResponse(null, { status: 200 }); | ||
} catch (e: any) { | ||
return new NextResponse( | ||
JSON.stringify({ | ||
error: e, | ||
}), | ||
{ 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
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
Oops, something went wrong.