gamenight/FrontendPlatformUno/FrontendPlatformUno/ViewModels/LoginViewModel.cs

60 lines
1.8 KiB
C#

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using FrontendPlatformUno.Services.Api;
using FrontendPlatformUno.Services.Client;
using FrontendPlatformUno.Services.Model;
using FrontendPlatformUno.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace FrontendPlatformUno.ViewModels
{
public class LoginViewModel : ObservableObject
{
private INavigator Navigator { get; }
public string? Title { get; }
public ICommand GoToLogin { get; }
public string? Username { get; set; }
public string? Password { get; set; }
public ICommand LoginCommand { get; internal set; }
private IDefaultApi GamenightApi { get; }
public LoginViewModel(INavigator navigator, IStringLocalizer stringLocalizer, IDefaultApi gamenightApi)
{
Navigator = navigator;
GamenightApi = gamenightApi;
Title = $"{stringLocalizer["ApplicationName"]} - Login";
GoToLogin = new AsyncRelayCommand(GoToLoginView);
LoginCommand = new AsyncRelayCommand(Login);
}
private async Task GoToLoginView()
{
await Navigator.NavigateViewModelAsync<LoginViewModel>(this);
}
private async Task Login()
{
var req = new GetTokenRequest(Username, Password);
GetToken200Response tokenResponse;
try
{
tokenResponse = await GamenightApi.GetTokenAsync(req);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return;
}
((Configuration)GamenightApi.Configuration).AccessToken = tokenResponse.JwtToken;
}
}
}