-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored code related to login and registration
- Loading branch information
1 parent
91d8a12
commit b587364
Showing
4 changed files
with
217 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} |
Oops, something went wrong.