forked from sanchezvem/PyroFetes
MAJ avec l'authentifiation
This commit is contained in:
53
PyroFetes/Endpoints/Login/CreateLoginEndpoint.cs
Normal file
53
PyroFetes/Endpoints/Login/CreateLoginEndpoint.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user