MAJ avec l'authentifiation

This commit is contained in:
2025-12-08 12:27:05 +01:00
parent 78e5a4e960
commit 0a8258017a
13 changed files with 325 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PasswordGenerator;
using PyroFetes.DTO.Login.Request;
using PyroFetes.DTO.Login.Response;
namespace PyroFetes.Endpoints.Login;
public class CreateLoginEndpoint(PyroFetesDbContext database) : Endpoint<CreateLoginDto, GetLoginDto>
{
public override void Configure()
{
Post("/logins");
//Roles("Admin");
AllowAnonymous();
}
public override async Task HandleAsync(CreateLoginDto req, CancellationToken ct)
{
bool exists = await database.Users.AnyAsync(x => x.Name == req.Name, ct);
if (exists)
{
AddError("Ce nom d'utilisateur est déjà utilisé.");
await Send.ErrorsAsync(400, ct);
return;
}
string? salt = new Password().IncludeLowercase().IncludeUppercase().IncludeNumeric().LengthRequired(24).Next();
Models.User login = new Models.User()
{
Name = req.Name,
Email = req.Email,
Password = BCrypt.Net.BCrypt.HashPassword(req.Password + salt),
Salt = salt,
Fonction = string.IsNullOrEmpty(req.Fonction) ? "User" : req.Fonction
};
database.Users.Add(login);
await database.SaveChangesAsync(ct);
GetLoginDto responseDto = new()
{
Id = login.Id,
Name = login.Name,
Email = login.Email,
Fonction = login.Fonction
};
await Send.OkAsync(responseDto, ct);
}
}