Files
PyroFetes-Sujet1/PyroFetes/Endpoints/Login/CreateLoginEndpoint.cs

44 lines
1.3 KiB
C#

using PyroFetes.DTO.Login.Request;
using PyroFetes.DTO.Login.Response;
using PasswordGenerator;
namespace PyroFetes.Endpoints.Login;
using FastEndpoints;
public class CreateLoginEndpoint(PyroFetesDbContext database) : Endpoint<CreateLoginDto, GetLoginDto>
{
public override void Configure()
{
Post("/api/logins");
AllowAnonymous();
}
public override async Task HandleAsync(CreateLoginDto req, CancellationToken ct)
{
string? salt = new Password().IncludeLowercase().IncludeUppercase().IncludeNumeric().LengthRequired(24).Next();
var login = new Models.Login()
{
Username = req.Username,
FullName = req.FullName,
Password = BCrypt.Net.BCrypt.HashPassword(req.Password + salt),
Salt = salt
};
database.Logins.Add(login);
await database.SaveChangesAsync(ct);
// Pour renvoyer une erreur : Send.StringAsync("Le message d'erreur", 400);
GetLoginDto responseDto = new()
{
Id = login.Id,
Username = login.Username,
FullName = login.FullName,
Password = login.Password,
Salt = login.Salt
};
await Send.OkAsync(responseDto, ct);
}
}