43 lines
1.2 KiB
C#
43 lines
1.2 KiB
C#
using ApiEfCoreLibrary.DTO.Login.Request;
|
|
using ApiEfCoreLibrary.DTO.Login.Response;
|
|
|
|
namespace ApiEfCoreLibrary.Endpoints.Login;
|
|
using FastEndpoints;
|
|
|
|
public class CreateLoginEndpoint(LibraryDbContext database) : Endpoint<CreateLoginDto, GetLoginDto>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Post("/api/logins");
|
|
AllowAnonymous();
|
|
}
|
|
|
|
public override async Task HandleAsync(CreateLoginDto req, CancellationToken ct)
|
|
{
|
|
string? salt = BCrypt.Net.BCrypt.GenerateSalt(24);
|
|
|
|
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);
|
|
}
|
|
} |