diff --git a/TIAM.Database/DataLayers/Auctions/AuctionDal.cs b/TIAM.Database/DataLayers/Auctions/AuctionDal.cs new file mode 100644 index 00000000..97e17052 --- /dev/null +++ b/TIAM.Database/DataLayers/Auctions/AuctionDal.cs @@ -0,0 +1,54 @@ +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.Auctions; + +namespace TIAM.Database.DataLayers.Users +{ + public class AuctionDal : TiamDalBase + { + + public AuctionDal() : base() + { + } + + public Task> GetBids() + { + return Ctx.AuctionBids.ToListAsync(); + } + + public Task> GetBidsByEmail(string email) + { + Console.WriteLine($"Getting bid from db {email}"); + var emailLower = email.ToLower(); + return Ctx.AuctionBids.Where(x => x.Email.ToLower() == emailLower).ToListAsync(); + } + + public async Task GetBidById(Guid id) + { + Console.WriteLine($"Getting bid from db {id}"); + + return Ctx.AuctionBids.FirstOrDefault(x => x.Id == id); + } + + public Task CreateBidAsync(AuctionBid auctionBid) + { + auctionBid.Created = DateTime.UtcNow; + auctionBid.Modified = DateTime.UtcNow; + Ctx.AuctionBids.Add(auctionBid); + Console.WriteLine($"Saving user to db {auctionBid.Id}, {auctionBid.Email}, {auctionBid.PhoneNumber}"); + return Ctx.SaveChangesAsync().ContinueWith(x => x.Result > 0); + } + + public Task UpdateBidAsync(AuctionBid auctionBid) + { + auctionBid.Modified = DateTime.UtcNow; + Ctx.AuctionBids.Update(auctionBid); + return Ctx.SaveChangesAsync().ContinueWith(x => x.Result > 0); + } + } +} diff --git a/TIAM.Database/DataLayers/Users/UserDal.cs b/TIAM.Database/DataLayers/Users/UserDal.cs index 2b848fc4..7542d9dd 100644 --- a/TIAM.Database/DataLayers/Users/UserDal.cs +++ b/TIAM.Database/DataLayers/Users/UserDal.cs @@ -32,6 +32,27 @@ namespace TIAM.Database.DataLayers.Users return Ctx.Users.SingleOrDefaultAsync(x=>x.Email.ToLower() == emailLower); } + public virtual Task GetUserByPhoneNumberAsync(string phoneNumber) + { + Console.WriteLine($"Getting user from db {phoneNumber}"); + var phoneNumberLower = phoneNumber.ToLower(); + return Ctx.Users.SingleOrDefaultAsync(x=>x.PhoneNumber.ToLower() == phoneNumberLower); + } + + public virtual Task GetUserByEmailOrPhoneNumberAsync(string emailOrPhoneNumber) + { + Console.WriteLine($"Getting user from db {emailOrPhoneNumber}"); + var emailOrPhoneNumberLower = emailOrPhoneNumber.ToLower(); + return Ctx.Users.SingleOrDefaultAsync(x=>x.Email.ToLower() == emailOrPhoneNumberLower || x.PhoneNumber.ToLower() == emailOrPhoneNumberLower); + } + + //get user by Id + public virtual Task GetUserByIdAsync(Guid id) + { + Console.WriteLine($"Getting user from db {id}"); + return Ctx.Users.SingleOrDefaultAsync(x=>x.Id == id); + } + public Task CreateUserAsync(User user) { user.Created = DateTime.UtcNow; @@ -41,11 +62,27 @@ namespace TIAM.Database.DataLayers.Users return Ctx.SaveChangesAsync().ContinueWith(x=>x.Result > 0); } - public Task UpdateUserAsync(User user) + public Task UpdateUserAsyncOld(User user) { user.Modified = DateTime.UtcNow; Ctx.Users.Update(user); return Ctx.SaveChangesAsync().ContinueWith(x=>x.Result > 0); } + + public Task UpdateUserAsync(User user) + { + var existingUser = Ctx.Users.FirstOrDefault(u => u.Email == user.Email); + if (existingUser != null) + { + user.Modified = DateTime.UtcNow; + existingUser = user; + Ctx.Users.Update(existingUser); + return Ctx.SaveChangesAsync().ContinueWith(x => x.Result > 0); + } + else + { + throw new Exception("User not found"); + } + } } } diff --git a/TIAM.Database/DbContexts/AuctionDbContext.cs b/TIAM.Database/DbContexts/AuctionDbContext.cs new file mode 100644 index 00000000..231bd562 --- /dev/null +++ b/TIAM.Database/DbContexts/AuctionDbContext.cs @@ -0,0 +1,43 @@ +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.Auctions; +using TIAM.Entities.TransferDestinations; + +namespace TIAM.Database.DbContexts +{ + public class AuctionDbContext : TiamDbContextBase + { + public virtual DbSet AuctionBids { get; set; } + + public AuctionDbContext() //: this(string.Empty) + { + + } + + public AuctionDbContext(DbContextOptions options) //: this(string.Empty) + { + + } + + public AuctionDbContext(string name) : base(name) + { + } + + public AuctionDbContext(DbContextOptions options, string name) : base(options, name) + { + } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.EnableDetailedErrors(true); + base.OnConfiguring(optionsBuilder); + } + + + } +} diff --git a/TIAM.Entities/Auctions/AuctionBid.cs b/TIAM.Entities/Auctions/AuctionBid.cs new file mode 100644 index 00000000..06103cd7 --- /dev/null +++ b/TIAM.Entities/Auctions/AuctionBid.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AyCode.Entities.Interfaces; +using AyCode.Entities.Users; +using AyCode.Interfaces.TimeStampInfo; + +namespace TIAM.Entities.Auctions +{ + [Table("AuctionBids")] + public class AuctionBid : IEntityGuid, ITimeStampInfo + { + + + public AuctionBid() { } + public AuctionBid(Guid ownerId, int targetProduct, string email) : this(Guid.NewGuid(), ownerId, targetProduct, email) { } + public AuctionBid(Guid id, Guid ownerId, int targetProductId, string email) : this() + { + Id = id; + OwnerId = ownerId; + TargetProductId = targetProductId; + Email = email; + IsValid = false; + } + public AuctionBid(Guid ownerId, int targetProductId, string email, string phoneNumber) : this(Guid.NewGuid(), ownerId, targetProductId, email,phoneNumber) + { + } + + public AuctionBid(Guid id, Guid ownerId, int targetProductId, string email, string? phoneNumber) : this() + { + Id = id; + OwnerId = ownerId; + TargetProductId = targetProductId; + Email = email; + PhoneNumber = phoneNumber; + IsValid = false; + } + + public AuctionBid(Guid ownerId, int targetProductId, string email, string phoneNumber, int bidAmount) : this(Guid.NewGuid(), ownerId, targetProductId, email, phoneNumber, bidAmount) + { + + OwnerId = ownerId; + TargetProductId = targetProductId; + Email = email; + PhoneNumber = phoneNumber; + BidAmount = bidAmount; + IsValid = false; + } + public AuctionBid(Guid id, Guid ownerId, int targetProductId, string email, string? phoneNumber, int bidAmount) : this() + { + Id = id; + OwnerId = ownerId; + TargetProductId = targetProductId; + Email = email; + PhoneNumber = phoneNumber; + BidAmount = bidAmount; + IsValid = false; + } + + + [Key, DatabaseGenerated(DatabaseGeneratedOption.None)] + public Guid Id { get; set; } + public Guid OwnerId { get; set; } + public int TargetProductId { get; set; } + public string Email { get; set; } + public string? PhoneNumber { get; set; } + public int BidAmount { get; set; } + public bool? IsValid { get; set; } + public DateTime Created { get; set; } + public DateTime Modified { get; set; } + } + + public enum TargetProductType + { + Product1 = 1, + Product2 = 2, + } +} diff --git a/TIAMMobileApp/MauiProgram.cs b/TIAMMobileApp/MauiProgram.cs index 821d1efb..47990a92 100644 --- a/TIAMMobileApp/MauiProgram.cs +++ b/TIAMMobileApp/MauiProgram.cs @@ -4,6 +4,7 @@ using TIAMWebApp.Shared.Application.Interfaces; using DevExpress.Blazor; using TIAMMobilApp.Services; using TIAMWebApp.Shared.Application.Utility; +using AyCode.Interfaces.StorageHandlers; namespace TIAMMobileApp { @@ -44,9 +45,9 @@ namespace TIAMMobileApp builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); - builder.Services.AddScoped(); - builder.Services.AddScoped(); - builder.Services.AddScoped(); + builder.Services.AddScoped(); + builder.Services.AddScoped(); + builder.Services.AddScoped(); return builder.Build(); } diff --git a/TIAMMobileApp/Services/SecureStorageHandler.cs b/TIAMMobileApp/Services/SecureStorageHandler.cs index abd615cc..57d5bb47 100644 --- a/TIAMMobileApp/Services/SecureStorageHandler.cs +++ b/TIAMMobileApp/Services/SecureStorageHandler.cs @@ -1,4 +1,5 @@ -using System; +using AyCode.Interfaces.StorageHandlers; +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/TIAMMobileApp/Services/SessionServiceMobile.cs b/TIAMMobileApp/Services/SessionServiceMobile.cs new file mode 100644 index 00000000..1538c95c --- /dev/null +++ b/TIAMMobileApp/Services/SessionServiceMobile.cs @@ -0,0 +1,13 @@ +using System.Net; +using TIAMWebApp.Shared.Application.Interfaces; +using TIAMWebApp.Shared.Application.Models; + +namespace TIAMMobileApp.Services +{ + public class SessionServiceMobile : ISessionService + { + public string? SessionId { get; set; } + public UserSessionModel? User { get; set; } + public IPAddress? IPAddress { get; set; } + } +} diff --git a/TIAMMobileApp/Services/UserDataService.cs b/TIAMMobileApp/Services/UserDataServiceMobile.cs similarity index 79% rename from TIAMMobileApp/Services/UserDataService.cs rename to TIAMMobileApp/Services/UserDataServiceMobile.cs index ab484a85..6fff474c 100644 --- a/TIAMMobileApp/Services/UserDataService.cs +++ b/TIAMMobileApp/Services/UserDataServiceMobile.cs @@ -1,4 +1,5 @@ -using Microsoft.EntityFrameworkCore.Infrastructure; +using AyCode.Interfaces.StorageHandlers; +using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.JSInterop; using Newtonsoft.Json; using System.Net.Http.Json; @@ -12,16 +13,14 @@ using TIAMWebApp.Shared.Application.Utility; namespace TIAMMobilApp.Services { - public class UserDataService : IUserDataService + public class UserDataServiceMobile : IUserDataService { private readonly HttpClient http; 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) + public UserDataServiceMobile(HttpClient http, ISecureStorageHandler secureStorageHandler) { this.http = http; this.secureStorageHandler = secureStorageHandler; @@ -43,35 +42,26 @@ namespace TIAMMobilApp.Services }; - public async Task IsLoggedInAsync() + public async Task IsLoggedInAsync(Guid id) { - if (User == null) - { - User = new UserModel("", "", ""); - User.IsLoggedIn = false; - User.UserType = UserType.User; - return User; - } - - else - { - return User; - } + UserSessionModel User = null; + + var dbUser = await GetUserByIdAsync(id); + if (dbUser != null) + { + + User = new UserSessionModel(dbUser.Id, UserType.User, dbUser.Email, 1); + return User; + } + else + { + + return null; + } + } - //Mock method for now - public async Task AuthorizeUserAsync(int userType) - { - if (User == null) - { - User = new UserModel("", "", ""); - } - //simply return true for now - User.IsLoggedIn = true; - User.UserType = (UserType)userType; - return User; - } public async Task TestUserApi(int Param) { @@ -86,7 +76,7 @@ namespace TIAMMobilApp.Services - var result = string.Empty; + string result = string.Empty; var url = APIUrls.AuthenticateUser; var response = await http.PostAsJsonAsync(url, loginModel); @@ -108,8 +98,9 @@ namespace TIAMMobilApp.Services public async Task<(bool isSuccess, string ErrorMessage)> CreateUser(RegistrationModel regModel) { - var isSuccess = true; - var result = string.Empty; + + bool isSuccess = true; + string result = string.Empty; var url = APIUrls.CreateUser; var response = await http.PostAsJsonAsync(url, regModel); @@ -138,11 +129,14 @@ namespace TIAMMobilApp.Services { return await http.GetFromJsonAsync(APIUrls.GetUserByEmail); } + public async Task GetUserByIdAsync(Guid Id) + { + return await http.GetFromJsonAsync(APIUrls.GetUserById); + } public async Task RefreshToken() { - var isTokenRefreshed = false; - + bool isTokenRefreshed = false; using (var client = new HttpClient()) { var url = APIUrls.RefreshToken; @@ -158,7 +152,7 @@ namespace TIAMMobilApp.Services var response = await client.PostAsync(url, new StringContent(serializedStr, Encoding.UTF8, "application/json")); if (response.IsSuccessStatusCode) { - var contentStr = await response.Content.ReadAsStringAsync(); + string contentStr = await response.Content.ReadAsStringAsync(); var mainResponse = JsonConvert.DeserializeObject(contentStr); if (mainResponse.IsSuccess) { @@ -166,7 +160,7 @@ namespace TIAMMobilApp.Services Setting.UserBasicDetails.AccessToken = tokenDetails.AccessToken; Setting.UserBasicDetails.RefreshToken = tokenDetails.RefreshToken; - var userDetailsStr = JsonConvert.SerializeObject(Setting.UserBasicDetails); + string userDetailsStr = JsonConvert.SerializeObject(Setting.UserBasicDetails); await secureStorageHandler.SaveToSecureStorageAsync(nameof(Setting.UserBasicDetails), userDetailsStr); isTokenRefreshed = true; } @@ -174,7 +168,7 @@ namespace TIAMMobilApp.Services } catch (Exception ex) { - var msg = ex.Message; + string msg = ex.Message; } @@ -185,9 +179,9 @@ namespace TIAMMobilApp.Services public Task> GetUserRolesAsync(UserModel userModel) { - + //TODO Finish this //get the userModel's roles - var role = User.UserRoles; + int role = userModel.UserRoles; foreach (var roleType in roleTypes) { diff --git a/TIAMMobileApp/TIAMMobileApp.csproj b/TIAMMobileApp/TIAMMobileApp.csproj index 36180c33..b0277669 100644 --- a/TIAMMobileApp/TIAMMobileApp.csproj +++ b/TIAMMobileApp/TIAMMobileApp.csproj @@ -54,6 +54,11 @@ + + + + + diff --git a/TIAMSharedUI/Pages/Auction.razor b/TIAMSharedUI/Pages/Auction.razor new file mode 100644 index 00000000..ea315267 --- /dev/null +++ b/TIAMSharedUI/Pages/Auction.razor @@ -0,0 +1,13 @@ +@page "/auction" +@using TIAMSharedUI.Pages.Components +@using TIAMWebApp.Shared.Application.Interfaces; +@using TIAMWebApp.Shared.Application.Models; +@inject ISessionService sessionService + +
+

Auction

+ +
+@code { + +} diff --git a/TIAMSharedUI/Pages/Components/AppLaunch.razor b/TIAMSharedUI/Pages/Components/AppLaunch.razor index 8c9b90db..b96fdf9e 100644 --- a/TIAMSharedUI/Pages/Components/AppLaunch.razor +++ b/TIAMSharedUI/Pages/Components/AppLaunch.razor @@ -5,6 +5,7 @@ @using Newtonsoft.Json @using System.IdentityModel.Tokens.Jwt @using TIAMWebApp.Shared.Application.Models.ClientSide +@using AyCode.Interfaces.StorageHandlers; @inject NavigationManager NavManager @inject IJSRuntime JSRuntime @inject LogToBrowserConsole logToBrowserConsole diff --git a/TIAMSharedUI/Pages/Components/AuctionComponent.razor b/TIAMSharedUI/Pages/Components/AuctionComponent.razor new file mode 100644 index 00000000..b81a6fbf --- /dev/null +++ b/TIAMSharedUI/Pages/Components/AuctionComponent.razor @@ -0,0 +1,16 @@ + +

Hello @UserId.ToString()

+
+ +
+ +
+
+ +
+
+ +@code { + [Parameter] + public Guid UserId { get; set; } +} diff --git a/TIAMSharedUI/Pages/Components/AuctionItemComponent.cs b/TIAMSharedUI/Pages/Components/AuctionItemComponent.cs new file mode 100644 index 00000000..cdb7ca75 --- /dev/null +++ b/TIAMSharedUI/Pages/Components/AuctionItemComponent.cs @@ -0,0 +1,42 @@ +using Microsoft.AspNetCore.Components; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TIAMWebApp.Shared.Application.Models; + +namespace TIAMSharedUI.Pages.Components +{ + public partial class AuctionItemComponent + { + [Parameter] + public int AuctionItemId { get; set; } + public string IsLoading { get; set; } = ""; + + public int currentStep = 0; + + public AuctionBidModel auctionBidModel = new(); + + public void NextStep() + { + currentStep++; + } + public void PreviousStep() + { + currentStep--; + } + + protected override void OnInitialized() + { + IsLoading = "loaded!"; + base.OnInitialized(); + } + + public void SubmitBid() + { + + } + } + +} diff --git a/TIAMSharedUI/Pages/Components/AuctionItemComponent.razor b/TIAMSharedUI/Pages/Components/AuctionItemComponent.razor new file mode 100644 index 00000000..d8b18b49 --- /dev/null +++ b/TIAMSharedUI/Pages/Components/AuctionItemComponent.razor @@ -0,0 +1,57 @@ +@using TIAMWebApp.Shared.Application.Models; +
+
+ @{ + var url = "_content/TIAMSharedUI/images/auction" + AuctionItemId.ToString() + ".jpg"; + } + ... + +
+
+

Item @AuctionItemId.ToString()

+

@IsLoading

+
+
    +
  • An item
  • +
  • A second item
  • +
  • A third item
  • +
+
+
+
+ @switch (currentStep) + { + case 0: + + ; + break; + case 1: + + ; + break; + case 2: + + ; + break; + case 3: + + ; + break; + } +
+ + +
+
+ +
+ + +@code { + +} diff --git a/TIAMSharedUI/Pages/Components/AuctionStep0.razor b/TIAMSharedUI/Pages/Components/AuctionStep0.razor new file mode 100644 index 00000000..ec5f3369 --- /dev/null +++ b/TIAMSharedUI/Pages/Components/AuctionStep0.razor @@ -0,0 +1,38 @@ +@using TIAMWebApp.Shared.Application.Models.PageModels; + + + + +
+ + + + Licitálok! + + + +
+ + + + + + + +@code { + + [Parameter] + public EventCallback onNext { get; set; } + + + private string spinnerClass = ""; + + private async Task GoToNextStep() + { + spinnerClass = "spinner-border spinner-border-sm"; + await Task.Delay(500); + spinnerClass = ""; + await onNext.InvokeAsync(); + } + +} \ No newline at end of file diff --git a/TIAMSharedUI/Pages/Components/AuctionStep1.razor b/TIAMSharedUI/Pages/Components/AuctionStep1.razor new file mode 100644 index 00000000..a73fef6c --- /dev/null +++ b/TIAMSharedUI/Pages/Components/AuctionStep1.razor @@ -0,0 +1,65 @@ +@using TIAMWebApp.Shared.Application.Models.PageModels; +@using TIAMWebApp.Shared.Application.Models; + + + +

Step 1

+
+ + + + + + + +
+ + + + +
+ + + +@code { + [Parameter] + public AuctionBidModel auctionBidModel { get; set; } + + //[Parameter] + //public string Email { get; set; } + + [Parameter] + public EventCallback onNext { get; set; } + + [Parameter] + public EventCallback auctionBidModelChanged { get; set; } + + IEnumerable PredefinedPlaceholders { get; set; } = new List() { '_', '#' }; + + string EmailMask { get; set; } = @"(\w|[.-])+@(\w|-)+\.(\w|-){2,4}"; + MaskAutoCompleteMode AutoCompleteMode { get; set; } = MaskAutoCompleteMode.Strong; + char Placeholder { get; set; } = '_'; + bool PlaceholderVisible { get; set; } = false; + + private string spinnerClass = ""; + + private async Task GoToNextStep() + { + spinnerClass = "spinner-border spinner-border-sm"; + await Task.Delay(500); + spinnerClass = ""; + auctionBidModel.Id = Guid.NewGuid(); + await auctionBidModelChanged.InvokeAsync(auctionBidModel); + await onNext.InvokeAsync(); + } + +} \ No newline at end of file diff --git a/TIAMSharedUI/Pages/Components/AuctionStep2.razor b/TIAMSharedUI/Pages/Components/AuctionStep2.razor new file mode 100644 index 00000000..61f75004 --- /dev/null +++ b/TIAMSharedUI/Pages/Components/AuctionStep2.razor @@ -0,0 +1,68 @@ +@using TIAMWebApp.Shared.Application.Models.PageModels; +

Step 2: Phone Number

+ + + + + + + +
+ + + + + + + + +
+ + + Previous + + +
+ + + +@code { + [Parameter] + public RegistrationModel regModel { get; set; } + + [Parameter] + public EventCallback onNext { get; set; } + + [Parameter] + public EventCallback onPrev { get; set; } + + [Parameter] + public EventCallback RegModelChanged { get; set; } + + char Placeholder = '_'; + + private string spinnerClass = ""; + + private async Task GoToNextStep() + { + spinnerClass = "spinner-border spinner-border-sm"; + await Task.Delay(500); + spinnerClass = ""; + await RegModelChanged.InvokeAsync(regModel); + await onNext.InvokeAsync(); + } + + private async Task GoToPreviousStep() + { + + await onPrev.InvokeAsync(); + } + +} \ No newline at end of file diff --git a/TIAMSharedUI/Pages/Components/AuctionStep3.razor b/TIAMSharedUI/Pages/Components/AuctionStep3.razor new file mode 100644 index 00000000..04ea487c --- /dev/null +++ b/TIAMSharedUI/Pages/Components/AuctionStep3.razor @@ -0,0 +1,64 @@ +@using TIAMWebApp.Shared.Application.Models.PageModels; +

Step 3: Password

+ + + + + + + +
+ + + + + + +
+ + + Previous + + +
+ + +@code { + [Parameter] + public RegistrationModel regModel { get; set; } + + [Parameter] + public EventCallback RegModelChanged { get; set; } + + [Parameter] + public EventCallback onPrev { get; set; } + + [Parameter] + public EventCallback onSubmit { get; set; } + + private string spinnerClass = ""; + + public async Task SubmitRegistration() + { + + spinnerClass = "spinner-border spinner-border-sm"; + await Task.Delay(500); + spinnerClass = ""; + + await RegModelChanged.InvokeAsync(regModel); + await onSubmit.InvokeAsync(); + } + + private async Task GoToPreviousStep() + { + + await onPrev.InvokeAsync(); + } +} + diff --git a/TIAMSharedUI/Pages/Index.razor b/TIAMSharedUI/Pages/Index.razor index b5560892..b103267e 100644 --- a/TIAMSharedUI/Pages/Index.razor +++ b/TIAMSharedUI/Pages/Index.razor @@ -1,4 +1,17 @@ @page "/" +@using AyCode.Interfaces.StorageHandlers; +@using Newtonsoft.Json; +@using TIAMWebApp.Shared.Application.Interfaces +@using TIAMWebApp.Shared.Application.Models.ClientSide; +@using AyCode.Blazor.Components; +@using TIAMWebApp.Shared.Application.Models; +@using TIAMWebApp.Shared.Application.Utility; +@using System.IdentityModel.Tokens.Jwt; +@inject NavigationManager NavManager +@inject IUserDataService UserDataService; +@inject IJSRuntime jsRuntime; +@inject ISecureStorageHandler SecureStorageHandler +@inject ISessionService sessionService; @using TIAMSharedUI.Shared Index @@ -57,6 +70,76 @@ +@code { + + bool isUserLoggedIn; + int userType = 0; + int currentUserRole = 249; + public UserSessionModel MyUser; + //add a new dictionary for the role types + + protected async override Task OnInitializedAsync() + { + //old + + + var 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)) + { + var userBasicDetail = JsonConvert.DeserializeObject(userDetailsStr); + + var handler = new JwtSecurityTokenHandler(); + var jsontoken = handler.ReadToken(userBasicDetail?.AccessToken) as JwtSecurityToken; + + if (userBasicDetail != null) + Setting.UserBasicDetails = userBasicDetail; + + if (jsontoken?.ValidTo < DateTime.UtcNow) + { + logToBrowserConsole.LogToBC("Token needs to be refreshed"); + bool isTokenRefreshed = await UserDataService.RefreshToken(); + + if (isTokenRefreshed) + { + logToBrowserConsole.LogToBC("Token refreshed"); + var myId = Guid.Parse(jsontoken.Claims.First(claim => claim.Type == JwtRegisteredClaimNames.NameId).Value); + //UserDataService.User.Email = jsontoken.Claims.First(claim => claim.Type == JwtRegisteredClaimNames.Email).Value; + MyUser = await UserDataService.IsLoggedInAsync(myId); + logToBrowserConsole.LogToBC(MyUser.UserId.ToString()); + } + else + { + logToBrowserConsole.LogToBC("Couldn't refresh token"); + } + + } + else + { + logToBrowserConsole.LogToBC("Valid token found"); + + var myId = Guid.Parse(jsontoken.Claims.First(claim => claim.Type == JwtRegisteredClaimNames.NameId).Value); + logToBrowserConsole.LogToBC(myId.ToString()); + //UserDataService.User.Email = jsontoken.Claims.First(claim => claim.Type == JwtRegisteredClaimNames.Email).Value; + MyUser = await UserDataService.IsLoggedInAsync(myId); + logToBrowserConsole.LogToBC(MyUser.UserId.ToString()); + sessionService.User = MyUser; + logToBrowserConsole.LogToBC($"{sessionService.User.UserId.ToString()}, {sessionService.User.Email}."); + } + } + else + { + logToBrowserConsole.LogToBC("No token stored yet"); + } + + } + +} + diff --git a/TIAMSharedUI/Pages/Login.razor b/TIAMSharedUI/Pages/Login.razor index 4093d6c5..ed89d9ad 100644 --- a/TIAMSharedUI/Pages/Login.razor +++ b/TIAMSharedUI/Pages/Login.razor @@ -58,11 +58,11 @@ public async Task next() { - var user = await userDataService.IsLoggedInAsync(); - user.IsLoggedIn = true; - isUserLoggedIn = user.IsLoggedIn; - user.UserType = (UserType)CurrentValue; - navManager.NavigateTo("home"); + //var user = await userDataService.IsLoggedInAsync(); + //user.IsLoggedIn = true; + //isUserLoggedIn = user.IsLoggedIn; + //user.UserType = (UserType)CurrentValue; + //navManager.NavigateTo("home"); } diff --git a/TIAMSharedUI/Pages/Login2.razor b/TIAMSharedUI/Pages/Login2.razor index f7e43727..edb05e42 100644 --- a/TIAMSharedUI/Pages/Login2.razor +++ b/TIAMSharedUI/Pages/Login2.razor @@ -10,9 +10,10 @@ @using TIAMWebApp.Shared.Application.Models.ClientSide; @using TIAMWebApp.Shared.Application.Models; @using TIAMWebApp.Shared.Application.Utility; +@using AyCode.Interfaces.StorageHandlers; @inject NavigationManager navManager @inject LogToBrowserConsole logToBrowserConsole -@inject IUserDataService UserDataservice +@inject IUserDataService userDataService @inject IJSRuntime jsRuntime @inject ISecureStorageHandler SecureStorageHandler @@ -86,7 +87,7 @@ currentStep = 1; logToBrowserConsole.LogToBC("Login started: " + "Email: " + loginModel.Email + ", Password: " + loginModel.Password); - var response = await UserDataservice.AuthenticateUser(loginModel); + var response = await userDataService.AuthenticateUser(loginModel); //var response = await UserDataservice.TestUserApi(30); logToBrowserConsole.LogToBC("Login started"); logToBrowserConsole.LogToBC(response); @@ -116,6 +117,9 @@ string _userId = token.Claims.First(claim => claim.Type == JwtRegisteredClaimNames.NameId).Value; string _email = token.Claims.First(claim => claim.Type == JwtRegisteredClaimNames.Email).Value; + var myId = Guid.Parse(_userId); + //userDataService.User.Email = _email; + var userBasicDetails = new UserBasicDetails(_userId, _email, AuthResponse.AccessToken, AuthResponse.RefreshToken); string userBasicDetailsJson = JsonSerializer.Serialize(userBasicDetails); @@ -137,8 +141,7 @@ //await App.Current.MainPage.DisplayAlert("Success", "Successful login", "Ok"); //display success message via jsinterop logToBrowserConsole.LogToBC("Successful login"); - var user = await UserDataservice.IsLoggedInAsync(); - user.IsLoggedIn = true; + var user = await userDataService.IsLoggedInAsync(myId); user.UserType = UserType.Admin; navManager.NavigateTo("home"); diff --git a/TIAMSharedUI/Pages/Register.razor b/TIAMSharedUI/Pages/Register.razor index 7466ddaa..a2723dfb 100644 --- a/TIAMSharedUI/Pages/Register.razor +++ b/TIAMSharedUI/Pages/Register.razor @@ -57,12 +57,7 @@ /*IEnumerable PredefinedPlaceholders = new List() { '_', '#' }; string Telephone { get; set; } = "5625595830"; char Placeholder { get; set; } = '_'; - bool SaveLiterals { get; set; } = true;*/ - - private void next() - { - navManager.NavigateTo("register2"); - } + bool SaveLiterals { get; set; } = true;*/ private int currentStep = 1; diff --git a/TIAMSharedUI/Pages/User/Home.razor b/TIAMSharedUI/Pages/User/Home.razor index 6f96239d..c99b9b73 100644 --- a/TIAMSharedUI/Pages/User/Home.razor +++ b/TIAMSharedUI/Pages/User/Home.razor @@ -758,11 +758,7 @@ protected override async Task OnInitializedAsync() { - base.OnInitialized(); - - var user = await UserDataService.IsLoggedInAsync(); - isUserLoggedIn = user.IsLoggedIn; - userType = (int)user.UserType; + base.OnInitialized(); OrderData = new object[] { diff --git a/TIAMSharedUI/Shared/MainLayout.razor b/TIAMSharedUI/Shared/MainLayout.razor index 604d917e..d256323f 100644 --- a/TIAMSharedUI/Shared/MainLayout.razor +++ b/TIAMSharedUI/Shared/MainLayout.razor @@ -1,8 +1,17 @@ @inherits LayoutComponentBase +@using AyCode.Interfaces.StorageHandlers; +@using Newtonsoft.Json; @using TIAMWebApp.Shared.Application.Interfaces @using TIAMWebApp.Shared.Application.Models.ClientSide; +@using AyCode.Blazor.Components; +@using TIAMWebApp.Shared.Application.Models; +@using TIAMWebApp.Shared.Application.Utility; +@using System.IdentityModel.Tokens.Jwt; +@inject NavigationManager NavManager @inject IUserDataService UserDataService; -@inject IJSRuntime jsRuntime +@inject IJSRuntime jsRuntime; +@inject ISecureStorageHandler SecureStorageHandler +@inject ISessionService sessionService;
@@ -12,43 +21,18 @@
- @{ - if(isUserLoggedIn) - { - - } - } + @if (Setting.UserBasicDetails != null) + { + + } @Body
+
+ @code { - bool isUserLoggedIn; - int userType = 0; - int currentUserRole = 249; - //add a new dictionary for the role types - - - - - protected override async Task OnInitializedAsync() - { - var user = await UserDataService.IsLoggedInAsync(); - isUserLoggedIn = user.IsLoggedIn; - - } - - protected override void OnAfterRender(bool isFirst) - { - - - } - - - - - - } diff --git a/TIAMSharedUI/Shared/NavMenu.razor b/TIAMSharedUI/Shared/NavMenu.razor index 8c558b42..af3a9f19 100644 --- a/TIAMSharedUI/Shared/NavMenu.razor +++ b/TIAMSharedUI/Shared/NavMenu.razor @@ -1,4 +1,5 @@ @using TIAMWebApp.Shared.Application.Interfaces +@using AyCode.Interfaces.StorageHandlers; @inject ISecureStorageHandler SecureStorageHandler + +