From b523e6b98973103c3a79d5efb1d78c42f20da31b Mon Sep 17 00:00:00 2001 From: AndriiSyrotenko Date: Wed, 20 Dec 2023 10:02:28 +0000 Subject: [PATCH] Add razor page for login --- .../Pages/Login.cshtml | 56 +++++++++++++++++++ .../Pages/Login.cshtml.cs | 47 ++++++++++++++++ .../Pages/Settings.razor | 2 +- 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 ShoppingAssistantWebClient.Web/Pages/Login.cshtml create mode 100644 ShoppingAssistantWebClient.Web/Pages/Login.cshtml.cs diff --git a/ShoppingAssistantWebClient.Web/Pages/Login.cshtml b/ShoppingAssistantWebClient.Web/Pages/Login.cshtml new file mode 100644 index 0000000..bfee3d5 --- /dev/null +++ b/ShoppingAssistantWebClient.Web/Pages/Login.cshtml @@ -0,0 +1,56 @@ +@page "/loginrazor" +@model ShoppingAssistantWebClient.Web.Pages.LoginModel + + + + + +
+ +
+ +@section Scripts { + +} diff --git a/ShoppingAssistantWebClient.Web/Pages/Login.cshtml.cs b/ShoppingAssistantWebClient.Web/Pages/Login.cshtml.cs new file mode 100644 index 0000000..58d2a3d --- /dev/null +++ b/ShoppingAssistantWebClient.Web/Pages/Login.cshtml.cs @@ -0,0 +1,47 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; +using ShoppingAssistantWebClient.Web.Models.Input; +using ShoppingAssistantWebClient.Web.Network; + +namespace ShoppingAssistantWebClient.Web.Pages; + +public class LoginModel : PageModel +{ + [BindProperty] + public LoginInputModel Input { get; set; } + + public string ErrorMessage { get; set; } + + public AuthenticationService AuthenticationService { get; set; } + + public void OnGet() + { + } + + public async Task OnPostAsync() + { + if (!ModelState.IsValid) + { + return Page(); + } + + if (Input.IsEmailOrPhoneProvided) + { + try + { + await AuthenticationService.LoginAsync(Input); + return RedirectToPage("/"); + } + catch (Exception ex) + { + ErrorMessage = "Login failed. Please try again."; + return Page(); + } + } + else + { + ErrorMessage = "Please provide an email or phone number."; + return Page(); + } + } +} diff --git a/ShoppingAssistantWebClient.Web/Pages/Settings.razor b/ShoppingAssistantWebClient.Web/Pages/Settings.razor index ecfacaa..dca3f1d 100644 --- a/ShoppingAssistantWebClient.Web/Pages/Settings.razor +++ b/ShoppingAssistantWebClient.Web/Pages/Settings.razor @@ -143,7 +143,7 @@ } private void RedirectToLogin() { - var url = $"/login"; + var url = $"/loginrazor"; NavigationManager.NavigateTo(url); }