aboutsummaryrefslogtreecommitdiffstats
path: root/examples/ColorPalette/BasicLogin.cs
blob: 50921a6273939b454c363e17e6f3d90ededcf7c2 (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/***************************************************************************************************
 Copyright (C) 2025 The Qt Company Ltd.
 SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
***************************************************************************************************/

using System.ComponentModel;
using System.Text.Json.Serialization;
using Refit;

namespace ColorPalette
{
    public sealed class BasicLogin : AbstractResource, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public string LoginPath { get; set; }
        public string LogoutPath { get; set; }

        private string _User;
        public string User
        {
            get => _User;
            set
            {
                if (_User == value)
                    return;
                _User = value;
                PropertyChanged?.Invoke(this, new(nameof(User)));
                PropertyChanged?.Invoke(this, new(nameof(LoggedIn)));
            }
        }

        public bool LoggedIn => _User != null;

        public void Login(string email, string password)
        {
            if (LoginApi == null) {
                ErrorMsg("Service not ready");
                return;
            }
            try {
                var response = Task.Run(() => LoginApi.LoginAsync(LoginPath, new()
                {
                    Email = email,
                    Password = password,
                }
                )).Result;
                Service.AuthToken = response.Token;
                User = email;
            } catch (Exception e) {
                ErrorMsg($"Login request failed: {e.Message}");
            }
        }

        public void Logout()
        {
            if (LoginApi == null) {
                ErrorMsg("Service not ready");
                return;
            }
            try {
                var response = Task.Run(() => LoginApi.LogoutAsync(LogoutPath, new()
                {
                    Email = User,
                }
                )).Result;
                Service.AuthToken = null;
                User = null;
            } catch (Exception e) {
                ErrorMsg($"Logout request failed: {e.Message}");
            }
        }

        private ILoginApi _LoginApi;
        private ILoginApi LoginApi
        {
            get
            {
                return _LoginApi ??= Service?.Create<ILoginApi>();
            }
        }

        internal override void ResetConnection()
        {
            _LoginApi = null;
        }

        internal interface ILoginApi
        {
            [Post("/{path}")]
            Task<LoginResponse> LoginAsync([AliasAs("path")] string path, [Body] LoginRequest req);

            [Post("/{path}")]
            Task<string> LogoutAsync([AliasAs("path")] string path, [Body] LoginRequest req);
        }

        internal class LoginRequest
        {
            [JsonPropertyName("email")]
            public string Email { get; set; }

            [JsonPropertyName("password")]
            [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
            public string Password { get; set; }
        }

        internal class LoginResponse
        {
            [JsonPropertyName("token")]
            public string Token { get; set; }
        }
    }
}