Auth and error handling components
This commit is contained in:
		| @@ -0,0 +1,67 @@ | ||||
| @using System.Net | ||||
| @using BlazorWebAssemblyVisaApiClient.Components.Auth.Exceptions | ||||
| @using BlazorWebAssemblyVisaApiClient.ErrorHandling | ||||
| @using VisaApiClient | ||||
|  | ||||
| @code { | ||||
|     public static bool LoggedIn; | ||||
|     private static AuthData savedData = null!; | ||||
|  | ||||
|     [CascadingParameter] private GlobalErrorHandler ErrorHandler { get; set; } = null!; | ||||
|  | ||||
|     [CascadingParameter] private Status? Status { get; set; } | ||||
|  | ||||
|     [Inject] private Client Client { get; set; } = null!; | ||||
|  | ||||
|     [Inject] private NavigationManager Nav { get; set; } = null!; | ||||
|  | ||||
|     ///Authorize with email and password | ||||
|     /// <returns>Message to user</returns> | ||||
|     public async Task TryAuthorize(AuthData authData) | ||||
|     { | ||||
|         Status?.SetMessage("Wait..."); | ||||
|         try | ||||
|         { | ||||
|             var token = await Client.LoginAsync(authData.Email, authData.Password); | ||||
|             Client.SetAuthToken(token); | ||||
|             savedData = authData; | ||||
|  | ||||
|             Status?.SetSucces("Logged in successfully."); | ||||
|         } | ||||
|         catch (ApiException<ProblemDetails> e) | ||||
|         { | ||||
|             if (e.Result.Status == (int)HttpStatusCode.Forbidden) | ||||
|             { | ||||
|                 Status?.SetError(e.Result.Detail!); | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 Status?.SetError("Error occured"); | ||||
|                 ErrorHandler.Handle(e); | ||||
|             } | ||||
|         } | ||||
|         catch (Exception e) | ||||
|         { | ||||
|             Status?.SetError("Error occured"); | ||||
|             ErrorHandler.Handle(e); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     ///Re-auth if token expired or something | ||||
|     public async Task ReAuthenticate(bool redirectOnFailure = true) | ||||
|     { | ||||
|         if (!LoggedIn) | ||||
|         { | ||||
|             if (redirectOnFailure) | ||||
|             { | ||||
|                 Nav.NavigateTo("/"); | ||||
|                 return; | ||||
|             } | ||||
|  | ||||
|             throw new NotLoggedInException(); | ||||
|         } | ||||
|  | ||||
|         await TryAuthorize(savedData); | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,7 @@ | ||||
| using BlazorWebAssemblyVisaApiClient.Common; | ||||
| using VisaApiClient; | ||||
|  | ||||
| namespace BlazorWebAssemblyVisaApiClient.Components.Auth.Exceptions | ||||
| { | ||||
|     public class NotLoggedInException() : BlazorClientException("User is not logged in"); | ||||
| } | ||||
| @@ -0,0 +1,9 @@ | ||||
| @using BlazorWebAssemblyVisaApiClient.ErrorHandling | ||||
| @using VisaApiClient | ||||
|  | ||||
| @code | ||||
| { | ||||
|     [CascadingParameter] protected GlobalErrorHandler ErrorHandler { get; set; } = null!; | ||||
|  | ||||
|     [Inject] protected Client Client { get; set; } = null!; | ||||
| } | ||||
| @@ -0,0 +1,34 @@ | ||||
| <p class="@statusClass">@((MarkupString)StatusText)</p> | ||||
| <CascadingValue Value="this"> | ||||
|     @ChildContent | ||||
| </CascadingValue> | ||||
|  | ||||
| @code { | ||||
|     private string statusClass = string.Empty; | ||||
|  | ||||
|     [Parameter] | ||||
|     public RenderFragment? ChildContent { get; set; } | ||||
|  | ||||
|     public string StatusText { get; set; } = string.Empty; | ||||
|  | ||||
|     public void SetMessage(string message) | ||||
|     { | ||||
|         statusClass = string.Empty; | ||||
|         StatusText = message; | ||||
|         StateHasChanged(); | ||||
|     } | ||||
|  | ||||
|     public void SetError(string message) | ||||
|     { | ||||
|         statusClass = "validation-message"; | ||||
|         StatusText = message; | ||||
|         StateHasChanged(); | ||||
|     } | ||||
|  | ||||
|     public void SetSucces(string message) | ||||
|     { | ||||
|         statusClass = "text-success"; | ||||
|         StatusText = message; | ||||
|         StateHasChanged(); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user