ホテル管理システム
ogi
yesterday 1a1c8e71fcd14858f595029f089b2d4a00202b32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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;
            }
        }
    }
}