From 7d4c9dad87ab4e47c916d904b6ba14d725dfd4be Mon Sep 17 00:00:00 2001 From: mfnefd Date: Thu, 20 Jun 2024 04:18:28 +0400 Subject: [PATCH] add login, sign up, user page and settings --- WebApp/Pages/Login.cshtml | 58 +++++++++++++++-- WebApp/Pages/Login.cshtml.cs | 12 ++-- WebApp/Pages/PageModelExtension.cs | 30 +++++++++ WebApp/Pages/Shared/_Layout.cshtml | 18 +++++- WebApp/Pages/SignUp.cshtml | 93 +++++++++++++++++++++++++++ WebApp/Pages/SignUp.cshtml.cs | 38 +++++++++++ WebApp/Pages/User/Index.cshtml | 23 +++++++ WebApp/Pages/User/Index.cshtml.cs | 32 +++++++++ WebApp/Pages/User/Settings.cshtml | 52 +++++++++++++++ WebApp/Pages/User/Settings.cshtml.cs | 52 ++++++++++++++- WebApp/wwwroot/background-login.jpg | Bin 0 -> 70394 bytes 11 files changed, 393 insertions(+), 15 deletions(-) create mode 100644 WebApp/Pages/PageModelExtension.cs create mode 100644 WebApp/Pages/SignUp.cshtml create mode 100644 WebApp/Pages/SignUp.cshtml.cs create mode 100644 WebApp/Pages/User/Index.cshtml create mode 100644 WebApp/Pages/User/Index.cshtml.cs create mode 100644 WebApp/wwwroot/background-login.jpg diff --git a/WebApp/Pages/Login.cshtml b/WebApp/Pages/Login.cshtml index 597f8be..d914310 100644 --- a/WebApp/Pages/Login.cshtml +++ b/WebApp/Pages/Login.cshtml @@ -1,9 +1,57 @@ @page @model WebApp.Pages.LoginModel -
- +@{ + ViewData["Title"] = "Log In"; +} +
+ +
+
+
+
- - +
+ +

Log in

+ +
+ + +
+ +
+ + +
+ +
+ +
+ +

Don't have an account? Register here

+
+ +
+ +
+
+ Login image +
+
+
+
\ No newline at end of file diff --git a/WebApp/Pages/Login.cshtml.cs b/WebApp/Pages/Login.cshtml.cs index ce8c5b7..4a53a79 100644 --- a/WebApp/Pages/Login.cshtml.cs +++ b/WebApp/Pages/Login.cshtml.cs @@ -11,18 +11,18 @@ namespace WebApp.Pages { } - public void OnPostAsync(string email, string password) + public IActionResult OnPostAsync(string email, string password) { var response = APIClient.PostRequest("user/login", new { email, password }); if (response is null || response is not string) { - return; + throw new Exception("Something wrong LOL!"); } - string token = (string)JsonConvert.DeserializeObject((string)response); - // - Response.Cookies.Append("21gunsthebest", token); - Redirect("/"); + + this.SetJWT((string)response); + + return RedirectToPage("Index"); } } } \ No newline at end of file diff --git a/WebApp/Pages/PageModelExtension.cs b/WebApp/Pages/PageModelExtension.cs new file mode 100644 index 0000000..8d8969d --- /dev/null +++ b/WebApp/Pages/PageModelExtension.cs @@ -0,0 +1,30 @@ +using Microsoft.AspNetCore.Mvc.RazorPages; +using Newtonsoft.Json; + +namespace WebApp.Pages +{ + public static class PageModelExtension + { + public static string? GetUserId(this PageModel pageModel) + { + if (pageModel.User.Identity.IsAuthenticated) + { + var userIdClaim = pageModel.User.Claims.FirstOrDefault(c => c.Type == "userId"); + return userIdClaim?.Value; + } + return null; + } + + public static void SetJWT(this PageModel pageModel, string jwt) + { + string token = (string)JsonConvert.DeserializeObject(jwt); + // Сохраняем в кукис токен + pageModel.Response.Cookies.Append("21gunsthebest", token); + } + + public static void DeleteJWT(this PageModel pageModel) + { + pageModel.Response.Cookies.Delete("21gunsthebest"); + } + } +} \ No newline at end of file diff --git a/WebApp/Pages/Shared/_Layout.cshtml b/WebApp/Pages/Shared/_Layout.cshtml index b103339..e7560e9 100644 --- a/WebApp/Pages/Shared/_Layout.cshtml +++ b/WebApp/Pages/Shared/_Layout.cshtml @@ -3,7 +3,7 @@ - @ViewData["Title"] - WebApp + @ViewData["Title"] - 21 GUNS @@ -12,7 +12,7 @@