36 lines
1.1 KiB
C#
36 lines
1.1 KiB
C#
using BeReadyBackend.DTO.Users;
|
|
using BeReadyBackend.Models;
|
|
using BeReadyBackend.Repositories;
|
|
using BeReadyBackend.Services;
|
|
using BeReadyBackend.Specifications.Users;
|
|
using FastEndpoints;
|
|
using PasswordGenerator;
|
|
|
|
namespace BeReadyBackend.Endpoints.Users;
|
|
|
|
public class PatchUserPasswordEndpoint(UsersRepository usersRepository, UserService userService) : Endpoint<PatchUserPasswordDto>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Patch("/Users/Password");
|
|
}
|
|
|
|
public override async Task HandleAsync(PatchUserPasswordDto req, CancellationToken ct)
|
|
{
|
|
int userId = userService.GetUserIdFromToken();
|
|
|
|
User? user = await usersRepository.SingleOrDefaultAsync(new GetUserByIdSpec(userId), ct);
|
|
|
|
if (user is null)
|
|
{
|
|
await Send.NotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
string salt = new Password().IncludeLowercase().IncludeUppercase().IncludeNumeric().LengthRequired(24).Next();
|
|
user.Password = BCrypt.Net.BCrypt.HashPassword(req.Password + salt);
|
|
|
|
await usersRepository.SaveChangesAsync(ct);
|
|
await Send.OkAsync(ct);
|
|
}
|
|
} |