diff --git a/Otakians/MainLayout.razor b/Otakians/MainLayout.razor index bbd38b7..9f465e6 100644 --- a/Otakians/MainLayout.razor +++ b/Otakians/MainLayout.razor @@ -3,10 +3,12 @@ @using System.ComponentModel.DataAnnotations @inject HttpClient httpClient @inject Blazored.LocalStorage.ILocalStorageService localStorage +@inject NavigationManager NavManager +@inject ISnackbar Snackbar - Clear cookies + Clear cookies @if (!isLoggedIn) { @@ -14,8 +16,12 @@ Log-in or sign-up here + ? + } + else + { + @(char.ToUpper(Program.LoggedInName[0])) } - ? @@ -29,17 +35,17 @@ @if (isLoggedIn) { - - Store - Library - Community - + + + Logout + + } else { - Log-in + Sign-in Sign-up @@ -55,19 +61,19 @@ - + Sign-up - + + Validation="@(new EmailAddressAttribute() {ErrorMessage = "The email address is invalid"})" + @bind-Text=tempUser.Email /> + RequiredError="Password is required!" + @bind-Text=tempUser.Password /> @@ -75,16 +81,31 @@ I agree with Terms Of Service - Register + Register @*Log-in overlay*@ - - - Login + + + + + Sign-in + Welcome back! + + + + + Login! + + @@ -96,6 +117,7 @@ bool isSigningUp = false; bool isLoggingIn = false; bool isValid = false; + User[]? allUsers; User tempUser = new User(); @@ -105,27 +127,89 @@ openProfileDrawer = !openProfileDrawer; } - void ShowSignUp() - { + void ShowSignUp() + { isSigningUp = true; openContentDrawer = openProfileDrawer = false; } - void ShowLogIn() - { - isLoggingIn = true; + void ShowLogIn() + { + isLoggingIn = true; openContentDrawer = openProfileDrawer = false; } void HideOverlays() => isSigningUp = isLoggingIn = false; -#endregion + #endregion - async void AddUser() + async void RegisterAccount() { - //await httpClient.PostAsJsonAsync("https://globalchat-4a89f-default-rtdb.europe-west1.firebasedatabase.app/Database/Users.json", new User()); - Console.WriteLine(tempUser.Username); - Console.WriteLine(tempUser.Email); - Console.WriteLine(tempUser.Password); + if (await GetUserFromName(tempUser) == null) + { + Console.WriteLine("Creating username"); + await httpClient.PostAsJsonAsync("https://globalchat-4a89f-default-rtdb.europe-west1.firebasedatabase.app/Database/Users.json", tempUser); + LoginAs(tempUser); + } + else + { + Console.WriteLine("Username already exists!"); + Snackbar.Add("Username already exists!", Severity.Warning); + } + } + + async void StartLoggingIn() + { + User? serverSideUser = await GetUserFromName(tempUser); + if (serverSideUser == null) + { + Console.WriteLine("User not found"); + Snackbar.Add("User not found", Severity.Warning); + return; + } + + if (serverSideUser.Password != tempUser.Password) + { + Console.WriteLine("Wrong password!"); + Snackbar.Add("Wrong password! Hackerman!", Severity.Error); + return; + } + + Console.WriteLine("Login success!"); + LoginAs(tempUser); + } + + async Task GetUserFromName(User user) + { + allUsers = await GetAllUsers(); + if (allUsers == null) return null; + + User? selectedUser = allUsers.FirstOrDefault(x => x.Username == user.Username); + if (selectedUser == null) return null; + + return selectedUser; + } + + async void LoginAs(User user) + { + await localStorage.SetItemAsStringAsync("User", user.Username); + RefreshPage(); + } + + async void Logout() + { + await localStorage.RemoveItemAsync("User"); + RefreshPage(); + } + + async Task GetAllUsers() + { + var allUsers = await httpClient.GetFromJsonAsync>("https://globalchat-4a89f-default-rtdb.europe-west1.firebasedatabase.app/Database/Users.json"); + + if (allUsers == null) + return null; + + User[] users = allUsers.Values.ToArray(); + return users; } #region Signup @@ -161,17 +245,19 @@ protected override async Task OnInitializedAsync() { - // Reads "cookie" - var cookieContent = await localStorage.GetItemAsync("cookieName"); + CheckLoggedin(); + } - if (cookieContent == null) - { - Console.WriteLine("Cookie is blank"); - } - else - { - Console.WriteLine("We have a cookie with contents: " + cookieContent); - } + async void CheckLoggedin() + { + string? name = await localStorage.GetItemAsStringAsync("User"); + if (name == null) return; + + Snackbar.Add("Successfully logged in!", Severity.Success); + + Program.LoggedInName = name; + isLoggedIn = true; + StateHasChanged(); } async void ClearAllCookies() @@ -179,6 +265,11 @@ await localStorage.ClearAsync(); } + private void RefreshPage() + { + NavManager.NavigateTo(NavManager.Uri, forceLoad: true); + } + public class User { public string? Username { get; set; } diff --git a/Otakians/Program.cs b/Otakians/Program.cs index cd65a74..476ea71 100644 --- a/Otakians/Program.cs +++ b/Otakians/Program.cs @@ -6,9 +6,12 @@ using Otakians; using System; using System.Security.Cryptography; using System.Text; +using MudBlazor; internal class Program { + public static string? LoggedInName; + private static async Task Main(string[] args) { var builder = WebAssemblyHostBuilder.CreateDefault(args); @@ -16,8 +19,8 @@ internal class Program builder.RootComponents.Add("head::after"); builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); - builder.Services.AddMudServices(); - builder.Services.AddBlazoredLocalStorage(); // local storage + builder.Services.AddMudServices(config => + config.SnackbarConfiguration.PositionClass = Defaults.Classes.Position.TopCenter); builder.Services.AddBlazoredLocalStorage(config => config.JsonSerializerOptions.WriteIndented = true); // local storage diff --git a/Otakians/_Imports.razor b/Otakians/_Imports.razor index 96f9c5f..e040558 100644 --- a/Otakians/_Imports.razor +++ b/Otakians/_Imports.razor @@ -6,4 +6,5 @@ @using Microsoft.AspNetCore.Components.WebAssembly.Http @using Microsoft.JSInterop @using Otakians -@using MudBlazor \ No newline at end of file +@using MudBlazor +@using System.Text.Json;