Correction
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
namespace GrossesMitainesAPI.Controllers;
|
||||
|
||||
#region Dependencies
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
@@ -10,44 +11,54 @@ using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using System.Security.Principal;
|
||||
using GrossesMitainesAPI.Models;
|
||||
|
||||
#endregion
|
||||
|
||||
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api"),
|
||||
Authorize(AuthenticationSchemes = "Identity.Application")]
|
||||
public class LoginController : Controller {
|
||||
#region DI Fields
|
||||
private readonly UserManager<InventoryUser> _userMan;
|
||||
private readonly RoleManager<IdentityRole> _roleMan;
|
||||
private readonly SignInManager<InventoryUser> _signInMan;
|
||||
|
||||
public LoginController(SignInManager<InventoryUser> signin, UserManager<InventoryUser> userman, RoleManager<IdentityRole> roleMan) {
|
||||
#endregion
|
||||
|
||||
#region Ctor
|
||||
public LoginController(SignInManager<InventoryUser> signin, UserManager<InventoryUser> userman) {
|
||||
this._signInMan = signin;
|
||||
this._userMan = userman;
|
||||
this._roleMan = roleMan;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Utility Classes
|
||||
public class LoginUser {
|
||||
public string email { get; set; } = "";
|
||||
public string password { get; set; } = "";
|
||||
}
|
||||
|
||||
[HttpGet, Route("Login")]
|
||||
public ReturnUserViewModel WhoAmI() {
|
||||
var user = _userMan.GetUserAsync(_signInMan.Context.User);
|
||||
user.Wait();
|
||||
var roles = _userMan.GetRolesAsync(user.Result);
|
||||
roles.Wait();
|
||||
#endregion
|
||||
|
||||
#region API Methods
|
||||
[HttpGet, Route("WhoAmI")]
|
||||
public async Task<ReturnUserViewModel> WhoAmI() {
|
||||
var user = await _userMan.GetUserAsync(_signInMan.Context.User);
|
||||
var roles = await _userMan.GetRolesAsync(user);
|
||||
string role = "";
|
||||
if (roles.Result.Contains("Administrateur"))
|
||||
if (roles.Contains("Administrateur"))
|
||||
role = "Administrateur";
|
||||
else role = "Client";
|
||||
return new ReturnUserViewModel(user.Result, role);
|
||||
return new ReturnUserViewModel(user, role);
|
||||
}
|
||||
|
||||
[HttpPost, Route("Login"), AllowAnonymous]
|
||||
public async Task<SignInResult> Login(LoginUser user, bool rememberMe = false) {
|
||||
return await _signInMan.PasswordSignInAsync(await _userMan.FindByEmailAsync(user.email), user.password, rememberMe, false);
|
||||
var User = await _userMan.FindByEmailAsync(user.email);
|
||||
return await _signInMan.PasswordSignInAsync(User, user.password, rememberMe, false);
|
||||
}
|
||||
|
||||
[HttpPost, Route("Logout")]
|
||||
public void Logout() => _signInMan.SignOutAsync();
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user