summary refs log tree commit diff
path: root/testFrontend/SafeNSound.Sdk/SafeNSoundAuthentication.cs
blob: 333db6dc365bc269aa7f0d681f1fcf73ffe65615 (plain) (blame)
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
using System.Net.Http.Json;
using System.Text.Json.Serialization;

namespace SafeNSound.Sdk;

public class SafeNSoundAuthentication(SafeNSoundConfiguration config) {
    public async Task Register(RegisterDto registerDto) {
        var hc = new WrappedHttpClient() {
            BaseAddress = new Uri(config.BaseUri)
        };

        var res = await hc.PostAsJsonAsync("/auth/register", registerDto);
        res.EnsureSuccessStatusCode();
    }

    public async Task<SafeNSoundAuthResult> Login(AuthDto authDto) {
        var hc = new WrappedHttpClient() {
            BaseAddress = new Uri(config.BaseUri)
        };

        var res = await hc.PostAsJsonAsync("/auth/login", authDto);
        return (await res.Content.ReadFromJsonAsync<SafeNSoundAuthResult>())!;
    }

    public async Task Delete(AuthDto authDto) {
        var hc = new WrappedHttpClient() {
            BaseAddress = new Uri(config.BaseUri)
        };

        var res = await hc.DeleteAsJsonAsync("/auth/delete", authDto);
        res.EnsureSuccessStatusCode();
    }
}

public class RegisterDto {
    [JsonPropertyName("username")]
    public string Username { get; set; } = string.Empty;

    [JsonPropertyName("password")]
    public string Password { get; set; } = string.Empty;

    [JsonPropertyName("email")]
    public string Email { get; set; } = string.Empty;

    [JsonPropertyName("type")]
    public string UserType { get; set; } = string.Empty;
}

public class AuthDto {
    [JsonPropertyName("username")]
    public string Username { get; set; } = string.Empty;

    [JsonPropertyName("password")]
    public string Password { get; set; } = string.Empty;

    [JsonPropertyName("email")]
    public string Email { get; set; } = string.Empty;
}

public class WhoAmI {
    [JsonPropertyName("userId")]
    public required string UserId { get; set; }

    [JsonPropertyName("deviceId")]
    public required string DeviceId { get; set; }

    [JsonPropertyName("type")]
    public required string UserType { get; set; }
}

public class SafeNSoundAuthResult : WhoAmI {
    [JsonPropertyName("accessToken")]
    public required string AccessToken { get; set; }
}