From 1a1c8e71fcd14858f595029f089b2d4a00202b32 Mon Sep 17 00:00:00 2001
From: ogi <Administrator@S-OGI-PC>
Date: Fri, 05 Dec 2025 09:24:16 +0900
Subject: [PATCH] プロジェクトファイルを追加。

---
 HotelPms.Client.Blazor/Services/AuthService.cs |   78 +++++++++++++++++++++++++++++++++++++++
 1 files changed, 78 insertions(+), 0 deletions(-)

diff --git a/HotelPms.Client.Blazor/Services/AuthService.cs b/HotelPms.Client.Blazor/Services/AuthService.cs
new file mode 100644
index 0000000..6aa89a5
--- /dev/null
+++ b/HotelPms.Client.Blazor/Services/AuthService.cs
@@ -0,0 +1,78 @@
+using Blazored.LocalStorage;
+using HotelPms.Client.Blazor.Util;
+using HotelPms.Data;
+using Grpc.Net.Client;
+using Microsoft.AspNetCore.Components;
+using Microsoft.AspNetCore.Components.Authorization;
+using System.Net.Http;
+using System.Net.Http.Headers;
+using System.Text;
+using System.Text.Json;
+using System.Threading.Channels;
+using System.Threading.Tasks;
+using HotelPms.DataAccessGrpc.Client;
+
+namespace HotelPms.Client.Blazor.Services
+{
+    internal class AuthService : IAuthService
+    {
+        private readonly GrpcChannel Channel;
+        private readonly AuthenticationStateProvider _authenticationStateProvider;
+        private readonly ILocalStorageService _localStorage;
+
+        public AuthService(GrpcChannel channel,
+            AuthenticationStateProvider authenticationStateProvider,
+            ILocalStorageService localStorage)
+        {
+            Channel = channel;
+            _authenticationStateProvider = authenticationStateProvider;
+            _localStorage = localStorage;
+        }
+
+        public async Task<LoginResult> Login(string loginID, string passwordl)
+        {
+            using (AuthAccess access = new AuthAccess(Channel))
+            {
+                LoginResult result = await access.LoginAsync(loginID, passwordl);
+                var options = new JsonSerializerOptions
+                {
+                    WriteIndented = true
+                };
+
+                if (result.ErrNo != 0)
+                {
+                    return result;
+                }
+
+
+                await _localStorage.SetItemAsync("authToken", result.AccessToKen);
+                ((ApiAuthenticationStateProvider)_authenticationStateProvider).MarkUserAsAuthenticated(result.AccessToKen);
+                ApiAuthenticationStateProvider.AccessToken = result.AccessToKen;
+
+                return result;
+            }
+        }
+
+        public async Task<LoginResult> Logout(string loginID)
+        {
+            using (AuthAccess access = new AuthAccess(Channel))
+            {
+                LoginResult result = await access.LogoutAsync(loginID);
+                var options = new JsonSerializerOptions
+                {
+                    WriteIndented = true
+                };
+
+                if (result.ErrNo != 0)
+                {
+                    return result;
+                }
+
+                await _localStorage.RemoveItemAsync("authToken");
+                ((ApiAuthenticationStateProvider)_authenticationStateProvider).MarkUserAsLoggedOut();
+                ApiAuthenticationStateProvider.AccessToken = null;
+                return result;
+            }
+        }
+    }
+}

--
Gitblit v1.10.0