You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
2.0 KiB
62 lines
2.0 KiB
using HeartTrack.Models;
|
|
using HeartTrack.Models.Authentification;
|
|
using Microsoft.AspNetCore.Components;
|
|
using System.Security.Claims;
|
|
|
|
namespace HeartTrack.Services.AuthentificationService
|
|
{
|
|
public class AuthService : IAuthService
|
|
{
|
|
private readonly List<AppUser> users;
|
|
|
|
[Inject]
|
|
private HttpClient Http { get; set; }
|
|
[Inject]
|
|
private NavigationManager NavigationManager { get; set; }
|
|
|
|
private readonly ILogger<AuthService> _logger;
|
|
|
|
public AuthService(IDataService dataService, ILogger<AuthService> logger)
|
|
{
|
|
users = new List<AppUser>
|
|
{
|
|
new AppUser { UserName = "Admin", Password = "123456", Roles = new List<string> { "admin" } }
|
|
};
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task<CurrentUser> GetUser(string username, string password)
|
|
{
|
|
var user = users.FirstOrDefault(w => w.UserName == username && BCrypt.Net.BCrypt.Verify(password, w.Password));
|
|
|
|
if (user == null)
|
|
{
|
|
throw new Exception("User name or password invalid !");
|
|
}
|
|
|
|
var claims = new List<Claim>();
|
|
claims.AddRange(user.Roles.Select(s => new Claim(ClaimTypes.Role, s)));
|
|
|
|
return await Task.FromResult(new CurrentUser
|
|
{
|
|
IsAuthenticated = true,
|
|
UserName = user.UserName,
|
|
Claims = claims.ToDictionary(c => c.Type, c => c.Value)
|
|
});
|
|
}
|
|
|
|
public async Task Login(LoginRequest loginRequest)
|
|
{
|
|
var user = users.FirstOrDefault(w => w.UserName == loginRequest.UserName && BCrypt.Net.BCrypt.Verify(loginRequest.Password, w.Password));
|
|
|
|
|
|
if (user == null)
|
|
{
|
|
_logger.LogError($"�chec de connexion pour l'utilisateur : {loginRequest.UserName}");
|
|
throw new Exception("User name or password invalid !");
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|