diff --git a/Front.csproj b/Front.csproj index 7227ec8..2548224 100644 --- a/Front.csproj +++ b/Front.csproj @@ -11,6 +11,7 @@ + @@ -26,8 +27,6 @@ - - diff --git a/Program.cs b/Program.cs index 8608b7e..e7ddd79 100644 --- a/Program.cs +++ b/Program.cs @@ -4,7 +4,8 @@ using Microsoft.Extensions.Configuration; using Front; using Front.Program.Services; - +using Front.Program.ViewModels; + var builder = WebAssemblyHostBuilder.CreateDefault(args); builder.RootComponents.Add("#app"); @@ -35,10 +36,12 @@ builder.Services.AddScoped(sp => //new HttpClient builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); // builder.Services.AddRazorPages(); // builder.Services.AddServerSideBlazor(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); await builder.Build().RunAsync(); diff --git a/Program/Layout/MainLayout.razor b/Program/Layout/MainLayout.razor index d99eecd..fe1c403 100644 --- a/Program/Layout/MainLayout.razor +++ b/Program/Layout/MainLayout.razor @@ -1,4 +1,4 @@ -@inherits LayoutComponentBase +@inherits LayoutComponentBase @implements IDisposable
diff --git a/Program/Layout/MainLayout.razor.cs b/Program/Layout/MainLayout.razor.cs index bc7af06..71f7924 100644 --- a/Program/Layout/MainLayout.razor.cs +++ b/Program/Layout/MainLayout.razor.cs @@ -32,40 +32,41 @@ public partial class MainLayout : LayoutComponentBase, IDisposable HandleLocationChanged(this, new LocationChangedEventArgs(Navigation.Uri, false)); } + // 페이지의 URL이 변경될 때마다 실행되는 이벤트 핸들러 private async void HandleLocationChanged(object? sender, LocationChangedEventArgs e) { LoadingService.HideNavigationLoading(); - - var uri = Navigation.ToAbsoluteUri(Navigation.Uri); - Console.WriteLine($"리다이렉트된 URI: {uri}"); - - if (uri.Query.Contains("auth=")) - { - var query = uri.Query.TrimStart('?'); - var parameters = query.Split('&') - .Select(p => p.Split('=')) - .Where(p => p.Length == 2) - .ToDictionary(p => p[0], p => p[1]); - - if (parameters.TryGetValue("auth", out var auth)) - { - Console.WriteLine($"auth 파라미터 값: {auth}"); - if (auth == "true") - { - await StorageService.SetItemAsync("IsLogin", "true"); - Console.WriteLine("로그인 상태를 true로 설정했습니다."); - } - else - { - await StorageService.RemoveItemAsync("IsLogin"); - Console.WriteLine("로그인 상태를 제거했습니다."); - } - - // 파라미터를 제거하고 리다이렉트 - var baseUri = uri.GetLeftPart(UriPartial.Path); - Navigation.NavigateTo(baseUri, forceLoad: false); - } - } + // + // var uri = Navigation.ToAbsoluteUri(Navigation.Uri); + // Console.WriteLine($"리다이렉트된 URI: {uri}"); + // + // if (uri.Query.Contains("auth=")) + // { + // var query = uri.Query.TrimStart('?'); + // var parameters = query.Split('&') + // .Select(p => p.Split('=')) + // .Where(p => p.Length == 2) + // .ToDictionary(p => p[0], p => p[1]); + // + // if (parameters.TryGetValue("auth", out var auth)) + // { + // Console.WriteLine($"auth 파라미터 값: {auth}"); + // if (auth == "true") + // { + // await StorageService.SetItemAsync("IsLogin", "true"); + // Console.WriteLine("로그인 상태를 true로 설정했습니다."); + // } + // else + // { + // await StorageService.RemoveItemAsync("IsLogin"); + // Console.WriteLine("로그인 상태를 제거했습니다."); + // } + // + // // 파라미터를 제거하고 리다이렉트 + // var baseUri = uri.GetLeftPart(UriPartial.Path); + // Navigation.NavigateTo(baseUri, forceLoad: false); + // } + // } } public void Dispose() diff --git a/Program/Models/UserData.cs b/Program/Models/UserData.cs new file mode 100644 index 0000000..532f4fa --- /dev/null +++ b/Program/Models/UserData.cs @@ -0,0 +1,13 @@ +namespace Front.Program.Models; + +public class UserData +{ + public string Uid { get; set; } = string.Empty; + public string Name { get; set; } = string.Empty; + public DateTime? Birth { get; set; } + public string Type { get; set; } = string.Empty; + public string? DeviceId { get; set; } + public bool AutoLoginYn { get; set; } + public DateTime LoginDate { get; set; } + public string? PushToken { get; set; } +} diff --git a/Program/Services/APIService.cs b/Program/Services/APIService.cs index 861d64e..2c11090 100644 --- a/Program/Services/APIService.cs +++ b/Program/Services/APIService.cs @@ -28,6 +28,7 @@ public class APIService var response = await _http.GetFromJsonAsync>($"{url}?{parameter}"); return response; } + } /* diff --git a/Program/Services/QueryParamService.cs b/Program/Services/QueryParamService.cs new file mode 100644 index 0000000..c6693ae --- /dev/null +++ b/Program/Services/QueryParamService.cs @@ -0,0 +1,42 @@ +using Microsoft.AspNetCore.Components; +using Microsoft.EntityFrameworkCore.Metadata.Internal; + +namespace Front.Program.Services; + +public class QueryParamService +{ + public Dictionary ParseQueryParam(System.Uri uri) + { + var result = new Dictionary(); + if (!string.IsNullOrEmpty(uri.Query)) + { + var query = uri.Query.TrimStart('?'); + var parameters = query.Split('&') + .Select(p => p.Split('=')) + .Where(p => p.Length == 2) + .ToDictionary(p => p[0], p => p[1]); + result = parameters; + } + + return result; + } + + public async Task AuthCheck(Dictionary parameters, StorageService storageService) + { + if (parameters.TryGetValue("auth", out var auth)) + { + Console.WriteLine($"auth 파라미터 값: {auth}"); + if (auth == "true") + { + await storageService.SetItemAsync("IsLogin", "true"); + Console.WriteLine("로그인 상태를 true로 설정했습니다."); + } + else + { + await storageService.RemoveItemAsync("IsLogin"); + Console.WriteLine("로그인 상태를 제거했습니다."); + + } + } + } +} \ No newline at end of file diff --git a/Program/Services/UserService.cs b/Program/Services/UserService.cs index 29595fc..6fef32d 100644 --- a/Program/Services/UserService.cs +++ b/Program/Services/UserService.cs @@ -1,4 +1,5 @@ using System.Text.Json; +using Front.Program.Models; using Microsoft.JSInterop; namespace Front.Program.Services; @@ -22,18 +23,7 @@ public class UserService _logger = logger; } - public class UserData - { - public string Uid { get; set; } = string.Empty; - public string Name { get; set; } = string.Empty; - public DateTime? Birth { get; set; } - public string Type { get; set; } = string.Empty; - public string? DeviceId { get; set; } - public bool AutoLoginYn { get; set; } - public DateTime LoginDate { get; set; } - public string? PushToken { get; set; } - } - + public async Task<(bool success, UserData? userData)> GetUserData() { try diff --git a/Program/ViewModels/UserViewModel.cs b/Program/ViewModels/UserViewModel.cs new file mode 100644 index 0000000..91388fd --- /dev/null +++ b/Program/ViewModels/UserViewModel.cs @@ -0,0 +1,158 @@ +using System.Text.Json; +using Front.Program.Services; +using Front.Program.Models; +using Microsoft.JSInterop; + +namespace Front.Program.ViewModels; + +public class UserViewModel(StorageService _storageService,SecureService _secureService, IJSRuntime _js) +{ + public UserData UserData { get; set; } = new UserData(); + + public bool isLogin { get; set; } = false; + + public async Task<(bool success, UserData? userData)> GetUserDataFromStorageAsync() + { + try + { + var encUserData = await _storageService.GetItemAsync("USER_DATA"); + + if (!string.IsNullOrEmpty(encUserData)) + { + + var decUserData = await _secureService.DecryptAsync(encUserData); + + var userData = JsonSerializer.Deserialize(decUserData, + new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); + + Console.WriteLine($"UserData: {userData.Name}, {userData.Type}"); + + + return (true, userData); + } + else + { + return (false, null); + } + } + catch (Exception ex) + { + Console.WriteLine($"Error [GetUserDataFormAsync] : {ex.Message}"); + return (false, null); + } + } + + public async Task<(bool success, UserData? userData)> GetUserDataFromServerAsync() + { + + var headerValue = await _storageService.GetItemAsync("Web_AM_Connect_Key"); + if (string.IsNullOrEmpty(headerValue)) return (false, null); + + var args = new { + url = "/api/v1/in/user", + method = "GET", + headerKey = "Web_AM_Connect_Key", + headerValue = headerValue, + token = "VO00" + }; + + var response = await _js.InvokeAsync( + "fetchWithHeaderAndReturnUrl", + args + ); + + // var response = await _js.InvokeAsync("fetchWithHeaderAndReturnUrl", + // "/api/v1/in/user/", + // "GET", + // "Web_AM_Connect_Key", + // headerValue); + + Console.WriteLine($"JSON 응답: {response.ToString()}"); + + if (response.TryGetProperty("data", out var dataElement)) + { + try + { + // 전체 데이터 암호화 저장 + var userDataJson = dataElement.ToString(); + var userData = JsonSerializer.Deserialize(userDataJson, + new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); + + if (userData != null && !string.IsNullOrEmpty(userData.Name)) + { + var encryptedData = await _secureService.EncryptAsync(userDataJson); + await _storageService.SetItemAsync("USER_DATA", encryptedData); + return (true, userData); + } + else + { + Console.WriteLine("사용자 데이터에 필수 정보가 없습니다"); + } + } + catch (Exception ex) + { + Console.WriteLine($"사용자 데이터 처리 중 오류: {ex.Message}"); + } + } + + Console.WriteLine("사용자 데이터를 찾을 수 없습니다"); + return (false, null); + } + + public async Task GetUserDataAsync() + { + Console.WriteLine("GetUserDataAsync 호출됨"); + // 로그인 상태가 아니라면 애초에 할 필요 없음 + if (await _storageService.GetItemAsync("IsLogin") != "true") + { + isLogin = false; + return false; + } + + var userDataForm = await GetUserDataFromStorageAsync(); + + if (userDataForm.success && userDataForm.userData != null) + { + // 사용자 데이터가 성공적으로 로드되었을 때의 로직 + UserData = userDataForm.userData; + isLogin = true; + return true; + } + else + { + var userDataFromServer = await GetUserDataFromServerAsync(); + if (userDataFromServer.success && userDataFromServer.userData != null) + { + // 서버에서 사용자 데이터를 성공적으로 로드했을 때의 로직 + UserData = userDataFromServer.userData; + isLogin = true; + return true; + } + else + { + // 사용자 데이터를 로드하지 못했을 때의 로직 + Console.WriteLine("사용자 데이터를 로드하지 못했습니다."); + isLogin = false; + return false; + } + } + } + + public async Task ClearUserData() + { + try + { + await _storageService.RemoveItemAsync("USER_DATA"); + await _storageService.RemoveItemAsync("IsLogin"); + Console.WriteLine("사용자 데이터 삭제 성공"); + isLogin = false; + // return true; + } + catch (Exception ex) + { + Console.WriteLine($"사용자 데이터 삭제 중 오류: {ex.Message}"); + // return false; + } + } + +} \ No newline at end of file diff --git a/Program/Views/Academy/TopNavAcademy.razor b/Program/Views/Academy/TopNavAcademy.razor index 75ea618..405f9e3 100644 --- a/Program/Views/Academy/TopNavAcademy.razor +++ b/Program/Views/Academy/TopNavAcademy.razor @@ -1,17 +1,12 @@
-
- Icon -
- - @if (!isLogin) + @if (!UserViewModel.isLogin) {