Skip to content

Commit

Permalink
Refactored code related to login and registration
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAJohn committed Nov 30, 2023
1 parent 91d8a12 commit b587364
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 202 deletions.
8 changes: 4 additions & 4 deletions FotoStorio.Client/Pages/Auth/Register.razor
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<div class="mb-4 font-semibold text-sm lg:text-base text-gray-400">
<a href="/login" class="hover:text-blue-800">Already have an account? Click here to log in</a>
</div>
<div class="px-2 md:px-4 py-2">
<div class="px-2 md:px-4">
<EditForm Model="RegisterModel" OnValidSubmit="HandleRegistration">
<FluentValidationValidator />
<div class="mb-3 w-full text-left">
Expand Down Expand Up @@ -59,7 +59,7 @@
{
<div class="mx-2 animate-pulse">
<div class="alert-box" role="alert">
<span class="block sm:inline">@Errors</span>
<span class="block sm:inline">@ErrorMessage</span>
</div>
</div>
}
Expand All @@ -71,7 +71,7 @@
@code {
private RegisterModel RegisterModel = new RegisterModel();
private bool ShowErrors;
private IEnumerable<string> Errors;
private string ErrorMessage;

private async Task HandleRegistration()
{
Expand All @@ -86,7 +86,7 @@
}
else
{
Errors = result.Errors;
ErrorMessage = result.Error;
ShowErrors = true;
}
}
Expand Down
116 changes: 60 additions & 56 deletions FotoStorio.Client/Services/AuthenticationService.cs
Original file line number Diff line number Diff line change
@@ -1,85 +1,89 @@
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;
using System.Threading.Tasks;
using Blazored.LocalStorage;
using FotoStorio.Client.Contracts;
using FotoStorio.Client.Providers;
using FotoStorio.Shared.Auth;
using Microsoft.AspNetCore.Components.Authorization;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;

namespace FotoStorio.Client.Services
namespace FotoStorio.Client.Services;

public class AuthenticationService : IAuthenticationService
{
public class AuthenticationService : IAuthenticationService
private readonly IHttpClientFactory _httpClient;
private readonly ILocalStorageService _localStorage;
private readonly AuthenticationStateProvider _authStateProvider;

public AuthenticationService(IHttpClientFactory httpClient, ILocalStorageService localStorage, AuthenticationStateProvider authStateProvider)
{
private readonly IHttpClientFactory _httpClient;
private readonly ILocalStorageService _localStorage;
private readonly AuthenticationStateProvider _authStateProvider;
public AuthenticationService(IHttpClientFactory httpClient, ILocalStorageService localStorage, AuthenticationStateProvider authStateProvider)
{
_authStateProvider = authStateProvider;
_localStorage = localStorage;
_httpClient = httpClient;
}
_authStateProvider = authStateProvider;
_localStorage = localStorage;
_httpClient = httpClient;
}

public async Task<RegisterResult> Register(RegisterModel registerModel)
{
var client = _httpClient.CreateClient("FotoStorioAPI");
var response = await client.PostAsJsonAsync("accounts/register", registerModel);
public async Task<RegisterResult> Register(RegisterModel registerModel)
{
var client = _httpClient.CreateClient("FotoStorioAPI");
var response = await client.PostAsJsonAsync("accounts/register", registerModel);

if (response.IsSuccessStatusCode)
{
return new RegisterResult
{
Successful = true
};
}
var responseContent = await response.Content.ReadAsStringAsync();
var registerResult = JsonSerializer.Deserialize<RegisterResult>(responseContent, _serializerOptions);

if (response.IsSuccessStatusCode)
{
return new RegisterResult
{
Successful = false,
Errors = new [] {"Registration failed"}
Successful = true
};
}

public async Task<LoginResult> Login(LoginModel loginModel)
return new RegisterResult
{
var client = _httpClient.CreateClient("FotoStorioAPI");
var response = await client.PostAsJsonAsync("accounts/login", loginModel);
Successful = false,
Error = registerResult.Error
};
}

var loginResult = JsonSerializer.Deserialize<LoginResult>(
await response.Content.ReadAsStringAsync(),
new JsonSerializerOptions { PropertyNameCaseInsensitive = true }
);
public async Task<LoginResult> Login(LoginModel loginModel)
{
var client = _httpClient.CreateClient("FotoStorioAPI");
var response = await client.PostAsJsonAsync("accounts/login", loginModel);

if (!response.IsSuccessStatusCode)
{
loginResult.Successful = false;
loginResult.Error = "Invalid username or password";

return loginResult;
}
var responseContent = await response.Content.ReadAsStringAsync();
var loginResult = JsonSerializer.Deserialize<LoginResult>(responseContent, _serializerOptions);

loginResult.Successful = true;
if (!response.IsSuccessStatusCode)
{
loginResult.Successful = false;
loginResult.Error = "Invalid username or password";

return loginResult;
}

await _localStorage.SetItemAsync("authToken", loginResult.Token);
loginResult.Successful = true;

((ApiAuthenticationStateProvider)_authStateProvider).MarkUserAsAuthenticated(loginModel.Email);
await _localStorage.SetItemAsync("authToken", loginResult.Token);

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", loginResult.Token);
((ApiAuthenticationStateProvider)_authStateProvider).MarkUserAsAuthenticated(loginModel.Email);

return loginResult;
}
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", loginResult.Token);

public async Task Logout()
{
await _localStorage.RemoveItemAsync("authToken");
return loginResult;
}

((ApiAuthenticationStateProvider)_authStateProvider).MarkUserAsLoggedOut();
public async Task Logout()
{
await _localStorage.RemoveItemAsync("authToken");

var client = _httpClient.CreateClient("FotoStorioAPI");
client.DefaultRequestHeaders.Authorization = null;
}
((ApiAuthenticationStateProvider)_authStateProvider).MarkUserAsLoggedOut();

var client = _httpClient.CreateClient("FotoStorioAPI");
client.DefaultRequestHeaders.Authorization = null;
}

private static readonly JsonSerializerOptions _serializerOptions = new()
{
PropertyNameCaseInsensitive = true
};
}

0 comments on commit b587364

Please sign in to comment.