Adding auth logs

arthur_logs
Arthur VALIN 2 years ago
parent 3ba2cba88f
commit 9c7c0d044e

@ -24,7 +24,7 @@
<br /> <br />
<label class="text-danger">@error</label> <label class="text-danger">@error</label>
<NavLink href="inscription"> <NavLink href="inscription">
<h6 class="font-weight-normal text-center">Creer un compte</h6> <h6 class="font-weight-normal text-center" style="color: white">Creer un compte</h6>
</NavLink> </NavLink>
</EditForm> </EditForm>
</div> </div>

@ -24,6 +24,9 @@ namespace CraftSharp.Pages
[Inject] [Inject]
public HttpClient httpClient { get; set; } public HttpClient httpClient { get; set; }
[Inject]
public ILogger<Connexion> Logger { get; set; }
private string error { get; set; } private string error { get; set; }
private ConnexionModel loginRequest { get; set; } = new ConnexionModel(); private ConnexionModel loginRequest { get; set; } = new ConnexionModel();
@ -41,6 +44,7 @@ namespace CraftSharp.Pages
try try
{ {
await AuthStateProvider.Login(loginRequest); await AuthStateProvider.Login(loginRequest);
Logger.Log(LogLevel.Information, $"Login : {loginRequest.UserName}");
var stringified = JsonConvert.SerializeObject(loginRequest); var stringified = JsonConvert.SerializeObject(loginRequest);
var response = await httpClient.PostAsJsonAsync($"{NavigationManager.BaseUri}User/SetUser", stringified); var response = await httpClient.PostAsJsonAsync($"{NavigationManager.BaseUri}User/SetUser", stringified);
NavigationManager.NavigateTo("index"); NavigationManager.NavigateTo("index");
@ -48,6 +52,7 @@ namespace CraftSharp.Pages
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Log(LogLevel.Error, $"Login Failure : {ex}");
error = ex.Message; error = ex.Message;
} }
} }

@ -28,7 +28,7 @@
<label class="text-danger">@error</label> <label class="text-danger">@error</label>
<NavLink href="connexion"> <NavLink href="connexion">
<h6 class="font-weight-normal text-center">Vous avez un compte ? Connectez vous</h6> <h6 class="font-weight-normal text-center" style="color: white">Vous avez un compte ? Connectez vous</h6>
</NavLink> </NavLink>
</EditForm> </EditForm>

@ -21,6 +21,9 @@ namespace CraftSharp.Pages
[Inject] [Inject]
public HttpClient httpClient { get; set; } public HttpClient httpClient { get; set; }
[Inject]
public ILogger<Inscription> Logger { get; set; }
private string error { get; set; } private string error { get; set; }
private InscriptionModel registerRequest { get; set; } = new InscriptionModel(); private InscriptionModel registerRequest { get; set; } = new InscriptionModel();
@ -28,6 +31,7 @@ namespace CraftSharp.Pages
{ {
if (AuthStateProvider.GetCurrentUser() != null && AuthStateProvider.GetCurrentUser().IsAuthenticated) if (AuthStateProvider.GetCurrentUser() != null && AuthStateProvider.GetCurrentUser().IsAuthenticated)
{ {
Logger.Log(LogLevel.Information, $"Automatic login : {AuthStateProvider.GetCurrentUser().UserName}");
NavigationManager.NavigateTo("index"); NavigationManager.NavigateTo("index");
} }
} }
@ -36,6 +40,7 @@ namespace CraftSharp.Pages
{ {
await AuthStateProvider.Register(registerRequest); await AuthStateProvider.Register(registerRequest);
Logger.Log(LogLevel.Information, $"Register : {registerRequest.UserName}");
var stringified = JsonConvert.SerializeObject(new ConnexionModel() { var stringified = JsonConvert.SerializeObject(new ConnexionModel() {
Password=registerRequest.Password, Password=registerRequest.Password,
UserName=registerRequest.UserName} UserName=registerRequest.UserName}

@ -10,7 +10,6 @@
@{ @{
Layout = "_Layout"; Layout = "_Layout";
Console.WriteLine("==============START==============");
var response = await httpClient.GetAsync($"https://localhost:7139/User/GetUser"); var response = await httpClient.GetAsync($"https://localhost:7139/User/GetUser");
string jsonUser = await response.Content.ReadAsStringAsync(); string jsonUser = await response.Content.ReadAsStringAsync();
var user = new ConnexionModel(); var user = new ConnexionModel();

@ -41,13 +41,10 @@ namespace CraftSharp.Services
public void Login(ConnexionModel loginRequest) public void Login(ConnexionModel loginRequest)
{ {
Console.WriteLine("LOGIN : " + loginRequest.UserName);
var user = CurrentUser.FirstOrDefault(w => w.UserName == loginRequest.UserName && w.Password == loginRequest.Password); var user = CurrentUser.FirstOrDefault(w => w.UserName == loginRequest.UserName && w.Password == loginRequest.Password);
if (user == null) if (user == null)
{ {
Console.WriteLine("LOGINFAILED");
throw new Exception("User name or password invalid !"); throw new Exception("User name or password invalid !");
} }
} }

@ -73,11 +73,8 @@ namespace CraftSharp.Services
if (_currentUser != null && _currentUser.IsAuthenticated) if (_currentUser != null && _currentUser.IsAuthenticated)
{ {
Console.WriteLine("GETUSER: " + _currentUser.UserName);
return _currentUser; return _currentUser;
} }
Console.WriteLine("GETUSER: FAIL");
return new CurrentUser(); return new CurrentUser();
} }
} }

@ -1,4 +1,5 @@
using CraftSharp.Models; using CraftSharp.Models;
using CraftSharp.Pages;
using CraftSharp.Services; using CraftSharp.Services;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.Authorization;
@ -22,6 +23,9 @@ namespace CraftSharp.Shared
[Inject] [Inject]
public HttpClient httpClient { get; set; } public HttpClient httpClient { get; set; }
[Inject]
public ILogger<HeaderLayout> Logger { get; set; }
[CascadingParameter] [CascadingParameter]
private Task<AuthenticationState> AuthenticationState { get; set; } private Task<AuthenticationState> AuthenticationState { get; set; }
@ -36,16 +40,6 @@ namespace CraftSharp.Shared
isAdmin(); isAdmin();
} }
void goInscription()
{
NavigationManager.NavigateTo("inscription");
}
void goConnexion()
{
NavigationManager.NavigateTo("connexion");
}
async public void isAdmin() async public void isAdmin()
{ {
var roles = AuthStateProvider.GetCurrentUser().Roles; var roles = AuthStateProvider.GetCurrentUser().Roles;
@ -54,6 +48,7 @@ namespace CraftSharp.Shared
private async Task LogoutClick() private async Task LogoutClick()
{ {
await AuthStateProvider.Logout(); await AuthStateProvider.Logout();
Logger.Log(LogLevel.Information, $"Logout : {AuthStateProvider.GetCurrentUser().UserName}");
await httpClient.DeleteAsync($"{NavigationManager.BaseUri}User/DeleteUser"); await httpClient.DeleteAsync($"{NavigationManager.BaseUri}User/DeleteUser");
NavigationManager.NavigateTo("/inscription"); NavigationManager.NavigateTo("/inscription");

Loading…
Cancel
Save