From 7096c9063721b3621a97d6949e2289d42d0685bf Mon Sep 17 00:00:00 2001 From: Adam Date: Sat, 25 Nov 2023 03:17:41 +0100 Subject: [PATCH] AdminDal (?), Registration and login almost finished (works from db already), login approvements, new api endpoints --- TIAM.Database/DataLayers/Admins/AdminDal.cs | 47 +++++ .../TransferDestinationDal.cs | 9 + TIAM.Database/DataLayers/Users/UserDal.cs | 17 ++ TIAM.Database/DbContexts/AdminDbContext.cs | 39 +++++ TIAM.Database/DbContexts/UserDbContext.cs | 3 + .../TransferDestination.cs | 46 ++--- TIAM.Entities/Users/User.cs | 5 + .../Services/SecureStorageHandler.cs | 11 ++ TIAMMobileApp/Services/UserDataService.cs | 19 ++- TIAMSharedUI/Pages/Components/AppLaunch.razor | 24 ++- TIAMSharedUI/Pages/Index.razor | 2 +- TIAMSharedUI/Pages/Login2.razor | 6 +- TIAMSharedUI/Shared/MainLayout.razor | 10 +- TIAMSharedUI/Shared/NavMenu.razor | 16 +- TIAMSharedUI/Shared/Users/AdminNavMenu.razor | 16 +- .../Client/Services/SecureStorageHandler.cs | 12 ++ TIAMWebApp/Client/Services/UserDataService.cs | 20 ++- .../Controllers/GoogleAPIController .cs | 2 +- .../Controllers/TransferDataAPIController.cs | 78 ++++++++- .../Server/Controllers/UserAPIController.cs | 161 ++++++++++++------ .../WeatherForecastAPIController.cs | 2 +- TIAMWebApp/Server/Program.cs | 2 +- TIAMWebApp/Server/TIAMWebApp.Server.csproj | 3 + .../Interfaces/ISecureStorageHandler.cs | 3 + TIAMWebApp/Shared/Models/APIUrls.cs | 5 +- .../Shared/Models/ClientSide/Setting.cs | 1 + .../Shared/Utility/LogToBrowserConsole.cs | 7 +- TIAMWebApp/Shared/Utility/PasswordHasher.cs | 48 ------ 28 files changed, 438 insertions(+), 176 deletions(-) create mode 100644 TIAM.Database/DataLayers/Admins/AdminDal.cs create mode 100644 TIAM.Database/DbContexts/AdminDbContext.cs delete mode 100644 TIAMWebApp/Shared/Utility/PasswordHasher.cs diff --git a/TIAM.Database/DataLayers/Admins/AdminDal.cs b/TIAM.Database/DataLayers/Admins/AdminDal.cs new file mode 100644 index 00000000..68cc0bf6 --- /dev/null +++ b/TIAM.Database/DataLayers/Admins/AdminDal.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using TIAM.Database.DbContexts; +using TIAM.Entities.Users; + +namespace TIAM.Database.DataLayers.Users +{ + public class AdminDal : TiamDalBase + { + + public AdminDal() : base() + { + } + + public Task> GetUsersAsync() + { + return Ctx.Users.ToListAsync(); + } + + public Task GetUserByEmailAsync(string email) + { + Console.WriteLine($"Getting user from db {email}"); + var emailLower = email.ToLower(); + return Ctx.Users.SingleOrDefaultAsync(x=>x.Email.ToLower() == emailLower); + } + + public Task CreateUserAsync(User user) + { + user.Created = DateTime.UtcNow; + user.Modified = DateTime.UtcNow; + Ctx.Users.Add(user); + Console.WriteLine($"Saving user to db {user.Id}, {user.Email}, {user.PhoneNumber}, {user.Password}"); + return Ctx.SaveChangesAsync().ContinueWith(x=>x.Result > 0); + } + + public Task UpdateUserAsync(User user) + { + user.Modified = DateTime.UtcNow; + Ctx.Users.Update(user); + return Ctx.SaveChangesAsync().ContinueWith(x=>x.Result > 0); + } + } +} diff --git a/TIAM.Database/DataLayers/TransferDestinations/TransferDestinationDal.cs b/TIAM.Database/DataLayers/TransferDestinations/TransferDestinationDal.cs index 62b2be75..effc716e 100644 --- a/TIAM.Database/DataLayers/TransferDestinations/TransferDestinationDal.cs +++ b/TIAM.Database/DataLayers/TransferDestinations/TransferDestinationDal.cs @@ -7,6 +7,7 @@ using AyCode.Database; using Microsoft.Identity.Client; using TIAM.Database.DbContexts; using TIAM.Entities.TransferDestinations; +using TIAM.Entities.Users; namespace TIAM.Database.DataLayers.TransferDestinations; @@ -15,4 +16,12 @@ public class TransferDestinationDal : TiamDalBase public TransferDestinationDal() : base() { } + + public Task CreateTransferDestinationAsync(TransferDestination transferDestination) + { + transferDestination.Created = DateTime.UtcNow; + transferDestination.Modified = DateTime.UtcNow; + Ctx.TransferDestinations.Add(transferDestination); + return Ctx.SaveChangesAsync().ContinueWith(x => x.Result > 0); + } } \ No newline at end of file diff --git a/TIAM.Database/DataLayers/Users/UserDal.cs b/TIAM.Database/DataLayers/Users/UserDal.cs index 483b2d26..aebe62e5 100644 --- a/TIAM.Database/DataLayers/Users/UserDal.cs +++ b/TIAM.Database/DataLayers/Users/UserDal.cs @@ -23,8 +23,25 @@ namespace TIAM.Database.DataLayers.Users public Task GetUserByEmailAsync(string email) { + Console.WriteLine($"Getting user from db {email}"); var emailLower = email.ToLower(); return Ctx.Users.SingleOrDefaultAsync(x=>x.Email.ToLower() == emailLower); } + + public Task CreateUserAsync(User user) + { + user.Created = DateTime.UtcNow; + user.Modified = DateTime.UtcNow; + Ctx.Users.Add(user); + Console.WriteLine($"Saving user to db {user.Id}, {user.Email}, {user.PhoneNumber}, {user.Password}"); + return Ctx.SaveChangesAsync().ContinueWith(x=>x.Result > 0); + } + + public Task UpdateUserAsync(User user) + { + user.Modified = DateTime.UtcNow; + Ctx.Users.Update(user); + return Ctx.SaveChangesAsync().ContinueWith(x=>x.Result > 0); + } } } diff --git a/TIAM.Database/DbContexts/AdminDbContext.cs b/TIAM.Database/DbContexts/AdminDbContext.cs new file mode 100644 index 00000000..5629f9db --- /dev/null +++ b/TIAM.Database/DbContexts/AdminDbContext.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AyCode.Database.DbContexts; +using Microsoft.EntityFrameworkCore; +using TIAM.Entities.TransferDestinations; +using TIAM.Entities.Users; + +namespace TIAM.Database.DbContexts +{ + public class AdminDbContext : TiamDbContextBase + { + public DbSet Users { get; set; } + public DbSet TransferDestinations { get; set; } + + public AdminDbContext() //: this(string.Empty) + { + + } + + public AdminDbContext(string name) : base(name) + { + } + + public AdminDbContext(DbContextOptions options, string name) : base(options, name) + { + } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.EnableDetailedErrors(true); + base.OnConfiguring(optionsBuilder); + } + + + } +} diff --git a/TIAM.Database/DbContexts/UserDbContext.cs b/TIAM.Database/DbContexts/UserDbContext.cs index 7ea71de0..23ee41de 100644 --- a/TIAM.Database/DbContexts/UserDbContext.cs +++ b/TIAM.Database/DbContexts/UserDbContext.cs @@ -29,7 +29,10 @@ namespace TIAM.Database.DbContexts protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { + optionsBuilder.EnableDetailedErrors(true); base.OnConfiguring(optionsBuilder); } + + } } diff --git a/TIAM.Entities/TransferDestinations/TransferDestination.cs b/TIAM.Entities/TransferDestinations/TransferDestination.cs index 39b3dc9a..50d2fbc0 100644 --- a/TIAM.Entities/TransferDestinations/TransferDestination.cs +++ b/TIAM.Entities/TransferDestinations/TransferDestination.cs @@ -1,44 +1,30 @@ using System.ComponentModel.DataAnnotations.Schema; using AyCode.Entities.Interfaces; +using AyCode.Entities.Locations; using AyCode.Interfaces.TimeStampInfo; namespace TIAM.Entities.TransferDestinations { [Table("TransferDestination")] - public class TransferDestination : IEntityGuid, ITimeStampInfo + public class TransferDestination : LocationBase, ITimeStampInfo { - [DatabaseGenerated(DatabaseGeneratedOption.None)] - public Guid Id - { - get; - set; - } - public string Name - { - get; - set; - } - - public string Description - { - get; - set; - } - - public decimal Longitude - { - get; - set; - } - - public decimal Latitude - { - get; - set; - } + public Guid Id { get; set; } + public double Longitude { get; set; } + public double Latitude { get; set; } + public string? Address { get; set; } + public string? Name { get; set; } public DateTime Created { get; set; } public DateTime Modified { get; set; } + + public TransferDestination() { } + + public TransferDestination(double longitude, double latitude, string address) : this(Guid.NewGuid(), longitude, latitude, address) { } + + public TransferDestination(Guid id, double longitude, double latitude, string address) : base(Guid.NewGuid(), longitude, latitude, address) { } + + + public TransferDestination(Guid id, string name, double longitude, double latitude, string address) : base(id, longitude, latitude, address) { } } } diff --git a/TIAM.Entities/Users/User.cs b/TIAM.Entities/Users/User.cs index 71bdac29..10a6ebbc 100644 --- a/TIAM.Entities/Users/User.cs +++ b/TIAM.Entities/Users/User.cs @@ -13,5 +13,10 @@ namespace TIAM.Entities.Users public User(string email, string password) : this(Guid.NewGuid(), email, password) { } public User(Guid id, string email, string password) : base(id, email, password) { } + public User(Guid id, string email, string phoneNumber, string password) : base(id, email, phoneNumber, password) + { } + public User(Guid id, string email, string phoneNumber, string password, string refreshToken) : base(id, email, phoneNumber, password, refreshToken) + { } + } } diff --git a/TIAMMobileApp/Services/SecureStorageHandler.cs b/TIAMMobileApp/Services/SecureStorageHandler.cs index ae3afd53..abd615cc 100644 --- a/TIAMMobileApp/Services/SecureStorageHandler.cs +++ b/TIAMMobileApp/Services/SecureStorageHandler.cs @@ -21,5 +21,16 @@ namespace TIAMMobileApp.Services { return await SecureStorage.GetAsync(key); } + + public async Task DeleteFromSecureStorageAsync(string key) + { + SecureStorage.Remove(key); + } + + public async Task ClearAllSecureStorageAsync() + { + SecureStorage.RemoveAll(); + } + } } diff --git a/TIAMMobileApp/Services/UserDataService.cs b/TIAMMobileApp/Services/UserDataService.cs index d2d11669..5c24c362 100644 --- a/TIAMMobileApp/Services/UserDataService.cs +++ b/TIAMMobileApp/Services/UserDataService.cs @@ -1,4 +1,5 @@ using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.JSInterop; using Newtonsoft.Json; using System.Net.Http.Json; using System.Text; @@ -7,21 +8,23 @@ using TIAMWebApp.Shared.Application.Interfaces; using TIAMWebApp.Shared.Application.Models; using TIAMWebApp.Shared.Application.Models.ClientSide; using TIAMWebApp.Shared.Application.Models.PageModels; - +using TIAMWebApp.Shared.Application.Utility; namespace TIAMMobilApp.Services { public class UserDataService : IUserDataService { private readonly HttpClient http; - private readonly ISecureStorageHandler secureStorageHandler; + private readonly ISecureStorageHandler secureStorageHandler; + + public UserModel? User { get; set; } = new UserModel("", "", ""); public Dictionary userRoleTypes { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } public UserDataService(HttpClient http, ISecureStorageHandler secureStorageHandler) { this.http = http; - this.secureStorageHandler = secureStorageHandler; + this.secureStorageHandler = secureStorageHandler; } @@ -127,18 +130,18 @@ namespace TIAMMobilApp.Services return (isSuccess, result); } - public Task> GetUsersAsync() + public async Task?> GetUsersAsync() { - throw new NotImplementedException(); + return await http.GetFromJsonAsync>(APIUrls.GetUsers); } - public Task GetUserByEmailAsync(string email) + public async Task GetUserByEmailAsync(string email) { - throw new NotImplementedException(); + return await http.GetFromJsonAsync(APIUrls.GetUserByEmail); } public async Task RefreshToken() - { + { bool isTokenRefreshed = false; using (var client = new HttpClient()) { diff --git a/TIAMSharedUI/Pages/Components/AppLaunch.razor b/TIAMSharedUI/Pages/Components/AppLaunch.razor index ec1d3b5d..8c9b90db 100644 --- a/TIAMSharedUI/Pages/Components/AppLaunch.razor +++ b/TIAMSharedUI/Pages/Components/AppLaunch.razor @@ -1,4 +1,4 @@ -@page "/"; +@page "/admin"; @using TIAMWebApp.Shared.Application.Interfaces @using TIAMWebApp.Shared.Application.Models @using TIAMWebApp.Shared.Application.Utility @@ -6,18 +6,25 @@ @using System.IdentityModel.Tokens.Jwt @using TIAMWebApp.Shared.Application.Models.ClientSide @inject NavigationManager NavManager +@inject IJSRuntime JSRuntime @inject LogToBrowserConsole logToBrowserConsole @inject IUserDataService UserDataService @inject ISecureStorageHandler SecureStorageHandler +@inject HttpClient http;

AppLaunch

Loading.... @code { - + protected async override Task OnInitializedAsync() { + + logToBrowserConsole = new LogToBrowserConsole(JSRuntime); + //wait for 5 seconds + await Task.Delay(5000); + string userDetailsStr = await SecureStorageHandler.GetFromSecureStorageAsync(nameof(Setting.UserBasicDetails)); logToBrowserConsole.LogToBC(userDetailsStr); if (!string.IsNullOrWhiteSpace(userDetailsStr)) @@ -25,30 +32,37 @@ Loading.... var userBasicDetail = JsonConvert.DeserializeObject(userDetailsStr); var handler = new JwtSecurityTokenHandler(); - var jsontoken = handler.ReadToken(userBasicDetail.AccessToken) as JwtSecurityToken; - Setting.UserBasicDetails = userBasicDetail; + var jsontoken = handler.ReadToken(userBasicDetail?.AccessToken) as JwtSecurityToken; + + if(userBasicDetail!= null) + Setting.UserBasicDetails = userBasicDetail; - if (jsontoken.ValidTo < DateTime.UtcNow) + if (jsontoken?.ValidTo < DateTime.UtcNow) { + logToBrowserConsole.LogToBC("Token needs to be refreshed"); bool isTokenRefreshed = await UserDataService.RefreshToken(); if (isTokenRefreshed) { + logToBrowserConsole.LogToBC("Token refreshed"); NavManager.NavigateTo("/home"); } else { + logToBrowserConsole.LogToBC("Couldn't refresh token"); NavManager.NavigateTo("/login"); } } else { + logToBrowserConsole.LogToBC("Valid token found"); NavManager.NavigateTo("/home"); } } else { + logToBrowserConsole.LogToBC("No token stored yet"); NavManager.NavigateTo("/login"); } diff --git a/TIAMSharedUI/Pages/Index.razor b/TIAMSharedUI/Pages/Index.razor index 8201f41f..b5560892 100644 --- a/TIAMSharedUI/Pages/Index.razor +++ b/TIAMSharedUI/Pages/Index.razor @@ -1,4 +1,4 @@ -@page "/index" +@page "/" @using TIAMSharedUI.Shared Index diff --git a/TIAMSharedUI/Pages/Login2.razor b/TIAMSharedUI/Pages/Login2.razor index a1ee0510..f7e43727 100644 --- a/TIAMSharedUI/Pages/Login2.razor +++ b/TIAMSharedUI/Pages/Login2.razor @@ -57,7 +57,7 @@ } }
- Already have an account? Sign in here! + No account yet? Sign up here!
@@ -130,7 +130,7 @@ //await App.Current.MainPage.DisplayAlert("Error", "Invalid credentials", "Ok"); //display error message via jsinterop logToBrowserConsole.LogToBC("Invalid credentials"); - navManager.NavigateTo("login2"); + navManager.NavigateTo("login"); } else { @@ -153,7 +153,7 @@ //await App.Current.MainPage.DisplayAlert("Error", "An error occured while trying to login", "Ok"); //display error message via jsinterop logToBrowserConsole.LogToBC("An error occured while trying to login"); - navManager.NavigateTo("login2"); + navManager.NavigateTo("login"); } } diff --git a/TIAMSharedUI/Shared/MainLayout.razor b/TIAMSharedUI/Shared/MainLayout.razor index f5d6d31f..604d917e 100644 --- a/TIAMSharedUI/Shared/MainLayout.razor +++ b/TIAMSharedUI/Shared/MainLayout.razor @@ -5,14 +5,11 @@ @inject IJSRuntime jsRuntime
- @if (Setting.UserBasicDetails != null) - { +
- } - - +
@{ @@ -20,8 +17,7 @@ { } - } - + } @Body
diff --git a/TIAMSharedUI/Shared/NavMenu.razor b/TIAMSharedUI/Shared/NavMenu.razor index b9d88571..8c558b42 100644 --- a/TIAMSharedUI/Shared/NavMenu.razor +++ b/TIAMSharedUI/Shared/NavMenu.razor @@ -1,4 +1,7 @@ -