Get api/Login -> WhoAmI

This commit is contained in:
MarcEricMartel 2022-10-31 07:12:25 -07:00
parent 63a02da413
commit afc5354d0f
4 changed files with 41 additions and 11 deletions

View File

@ -16,7 +16,7 @@ using Microsoft.AspNetCore.Authorization;
Authorize(AuthenticationSchemes = "Identity.Application")] Authorize(AuthenticationSchemes = "Identity.Application")]
public class InventoryController : Controller { public class InventoryController : Controller {
#region Constants #region Constants
private const int AMOUNT_SCROLL = 5; private const int AMOUNT_SCROLL = 6;
#endregion #endregion
@ -136,7 +136,7 @@ public class InventoryController : Controller {
public ActionResult<int> Delete(int? id) { public ActionResult<int> Delete(int? id) {
int rid = 0; int rid = 0;
if (!id.HasValue) { if (!id.HasValue) {
_logger.LogError(8, "Delete sans Id."); _logger.LogError(8, "Tentative de vente sans Id.");
return BadRequest(); return BadRequest();
} }
try { try {
@ -151,7 +151,7 @@ public class InventoryController : Controller {
Product.States.Discontinued : Product.States.Discontinued :
Product.States.BackOrder; Product.States.BackOrder;
} else { } else {
_logger.LogError(8, "Vente de produit pas en stock."); _logger.LogError(8, $"Vente de produit pas en stock. Id Produit: {prod.Id}");
return BadRequest(); return BadRequest();
} }
_context.Products.Update(prod); _context.Products.Update(prod);

View File

@ -7,28 +7,38 @@ using Microsoft.AspNetCore.Identity;
using GrossesMitainesAPI.Data; using GrossesMitainesAPI.Data;
using SignInResult = Microsoft.AspNetCore.Identity.SignInResult; using SignInResult = Microsoft.AspNetCore.Identity.SignInResult;
using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.Cookies;
using System.Security.Principal;
using GrossesMitainesAPI.Models;
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api"), [EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api"),
Authorize(AuthenticationSchemes = "Identity.Application")] Authorize(AuthenticationSchemes = "Identity.Application")]
public class LoginController : Controller { public class LoginController : Controller {
//private readonly UserManager<InventoryUser> _userMan; private readonly UserManager<InventoryUser> _userMan;
private readonly SignInManager<InventoryUser> _signInMan; private readonly SignInManager<InventoryUser> _signInMan;
public LoginController(SignInManager<InventoryUser> signin) { public LoginController(SignInManager<InventoryUser> signin, UserManager<InventoryUser> userman) {
this._signInMan = signin; this._signInMan = signin;
this._userMan = userman;
} }
public class User { public class LoginUser {
public string username { get; set; } public string username { get; set; } = "";
public string password { get; set; } public string password { get; set; } = "";
}
[HttpGet, Route("Login")]
public ReturnUserViewModel WhoAmI() {
var user = _userMan.GetUserAsync(_signInMan.Context.User);
user.Wait();
return new ReturnUserViewModel(user.Result);
} }
[HttpPost, Route("Login"), AllowAnonymous] [HttpPost, Route("Login"), AllowAnonymous]
public async Task<SignInResult> Login(User user, bool rememberMe = false) { public async Task<SignInResult> Login(LoginUser user, bool rememberMe = false) {
return await _signInMan.PasswordSignInAsync(user.username, user.password, rememberMe, false); return await _signInMan.PasswordSignInAsync(user.username, user.password, rememberMe, false);
} }
[HttpPost, Route("Logout")] [HttpPost, Route("Logout")]
public void Logout() { _signInMan.SignOutAsync(); } public void Logout() => _signInMan.SignOutAsync();
} }

View File

@ -0,0 +1,20 @@
using GrossesMitainesAPI.Data;
namespace GrossesMitainesAPI.Models;
public class ReturnUserViewModel {
public string Username { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public bool EmailConfirmed { get; set; }
public bool PhoneConfirmed { get; set; }
public bool TwoFactorEnable { get; set; }
public ReturnUserViewModel(InventoryUser user) {
Username = user.UserName;
Email = user.Email;
Phone = user.PhoneNumber;
EmailConfirmed = user.EmailConfirmed;
PhoneConfirmed = user.PhoneNumberConfirmed;
TwoFactorEnable = user.TwoFactorEnabled;
}
}

View File

@ -92,7 +92,7 @@ public class DatabaseCacheService {
#region Public Methods #region Public Methods
public bool isOk() { return _ok; } public bool isOk() { return _ok; }
public void askForRefresh() { _needUpd = true; } public void askForRefresh() => _needUpd = true;
public void addHit(uint id) { public void addHit(uint id) {
lock (_hits) { lock (_hits) {
if (_hits.ContainsKey(id)) if (_hits.ContainsKey(id))