-
Notifications
You must be signed in to change notification settings - Fork 439
/
Copy pathAuthClient.cs
44 lines (33 loc) · 1.09 KB
/
AuthClient.cs
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
namespace Todo.Web.Server;
public class AuthClient(HttpClient client)
{
public async Task<string?> GetTokenAsync(UserInfo userInfo)
{
var response = await client.PostAsJsonAsync("users/login", userInfo);
if (!response.IsSuccessStatusCode)
{
return null;
}
var token = await response.Content.ReadFromJsonAsync<AuthToken>();
return token?.Token;
}
public async Task<string?> CreateUserAsync(UserInfo userInfo)
{
var response = await client.PostAsJsonAsync("users/register", userInfo);
if (!response.IsSuccessStatusCode)
{
return null;
}
return await GetTokenAsync(userInfo);
}
public async Task<string?> GetOrCreateUserAsync(string provider, ExternalUserInfo userInfo)
{
var response = await client.PostAsJsonAsync($"users/token/{provider}", userInfo);
if (!response.IsSuccessStatusCode)
{
return null;
}
var token = await response.Content.ReadFromJsonAsync<AuthToken>();
return token?.Token;
}
}