@page "/adminstart";
@using TIAMWebApp.Shared.Application.Interfaces
@using TIAMWebApp.Shared.Application.Models
@using TIAMWebApp.Shared.Application.Utility
@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
@inject IUserDataService UserDataService
@inject ISecureStorageHandler SecureStorageHandler
@inject HttpClient http;
<h3>AppLaunch</h3>

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))
        {
            var userBasicDetail = JsonConvert.DeserializeObject<UserBasicDetails>(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");
                    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");
        }

    }
}
