forked from sanchezvem/PyroFetes
Ajout de Login et du model réuni avec tout les autres Sujet
This commit is contained in:
44
PyroFetes/Endpoints/Login/CreateLoginEndpoint.cs
Normal file
44
PyroFetes/Endpoints/Login/CreateLoginEndpoint.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user