56 lines
1.2 KiB
C#
56 lines
1.2 KiB
C#
using AutoCat.Utils;
|
|
using AutoCatCore.Model;
|
|
using AutoCatCore.MVVM;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Input;
|
|
|
|
namespace AutoCat.ViewModel
|
|
{
|
|
public class AuthViewModel
|
|
{
|
|
private IDialogService dialogService;
|
|
private AutoCatDbContext dbContext;
|
|
private User currentUser;
|
|
|
|
public string Email { get; private set; }
|
|
|
|
public bool isLoggedIn => currentUser?.Token != null;
|
|
|
|
public AuthViewModel(IDialogService dialogService)
|
|
{
|
|
this.dialogService = dialogService;
|
|
Init();
|
|
}
|
|
|
|
private async void Init()
|
|
{
|
|
dbContext = new AutoCatDbContext();
|
|
await dbContext.Database.EnsureCreatedAsync();
|
|
if (dbContext.Users.Count() > 0)
|
|
{
|
|
currentUser = dbContext.Users.First();
|
|
Email = currentUser.Email;
|
|
Api.SetAccessToken(currentUser.Token);
|
|
}
|
|
}
|
|
|
|
public async Task Login(string login, string password)
|
|
{
|
|
try
|
|
{
|
|
var user = await Api.Login(login, password);
|
|
await dbContext.Users.AddAsync(user);
|
|
await dbContext.SaveChangesAsync();
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
await dialogService.ShowErrorDialogAsync(ex.Message, "Authorization error");
|
|
}
|
|
}
|
|
}
|
|
}
|