Initial Commit
This commit is contained in:
129
Clario/ViewModels/AuthViewModel.cs
Normal file
129
Clario/ViewModels/AuthViewModel.cs
Normal file
@@ -0,0 +1,129 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls.ApplicationLifetimes;
|
||||
using Clario.Data;
|
||||
using Clario.Models.GeneralModels;
|
||||
using Clario.Services;
|
||||
using Clario.Views;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using Supabase.Gotrue;
|
||||
|
||||
namespace Clario.ViewModels;
|
||||
|
||||
public partial class AuthViewModel : ViewModelBase
|
||||
{
|
||||
[ObservableProperty] [NotifyCanExecuteChangedFor(nameof(ConfirmCreateAccountCommand))]
|
||||
private string _firstName;
|
||||
|
||||
[ObservableProperty] [NotifyCanExecuteChangedFor(nameof(ConfirmCreateAccountCommand))]
|
||||
private string _lastName;
|
||||
|
||||
[ObservableProperty] [NotifyCanExecuteChangedFor(nameof(ConfirmCreateAccountCommand), nameof(ConfirmLoginCommand))]
|
||||
private string _email;
|
||||
|
||||
[ObservableProperty] [NotifyCanExecuteChangedFor(nameof(ConfirmCreateAccountCommand), nameof(ConfirmLoginCommand))]
|
||||
private string _password;
|
||||
|
||||
[ObservableProperty] [NotifyCanExecuteChangedFor(nameof(ConfirmCreateAccountCommand))]
|
||||
private string _confirmPassword;
|
||||
|
||||
[ObservableProperty]
|
||||
[NotifyPropertyChangedFor(nameof(isSignin), nameof(isCreateAccount))]
|
||||
[NotifyCanExecuteChangedFor(nameof(ConfirmCreateAccountCommand), nameof(ConfirmLoginCommand))]
|
||||
private string _operation = "login";
|
||||
|
||||
public AuthViewModel()
|
||||
{
|
||||
setDefaults();
|
||||
}
|
||||
|
||||
private void setDefaults()
|
||||
{
|
||||
FirstName = "nouredeen";
|
||||
LastName = "ghazal";
|
||||
Email = "nouredeen.ghazal42@gmail.com";
|
||||
Password = "Nour1Clario";
|
||||
ConfirmPassword = "Nour1Clario";
|
||||
ThemeService.SwitchToTheme("system");
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void SetOperation(string operation)
|
||||
{
|
||||
Operation = operation;
|
||||
}
|
||||
|
||||
[RelayCommand(CanExecute = nameof(canSignin))]
|
||||
private async Task ConfirmLogin()
|
||||
{
|
||||
try
|
||||
{
|
||||
await SupabaseService.Client.Auth.SignIn(_email, _password);
|
||||
|
||||
var user = SupabaseService.Client.Auth.CurrentUser;
|
||||
|
||||
if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
||||
{
|
||||
desktop.MainWindow!.DataContext = user is not null ? new MainViewModel() : new AuthViewModel();
|
||||
}
|
||||
else if (Application.Current.ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform)
|
||||
{
|
||||
singleViewPlatform.MainView!.DataContext = user is not null ? new MainViewModel() : new AuthViewModel();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand(CanExecute = nameof(canCreateAccount))]
|
||||
private async Task ConfirmCreateAccount()
|
||||
{
|
||||
try
|
||||
{
|
||||
var session = await SupabaseService.Client.Auth.SignUp(
|
||||
_email,
|
||||
_password,
|
||||
new SignUpOptions
|
||||
{
|
||||
Data = new Dictionary<string, object>
|
||||
{
|
||||
{
|
||||
"display_name", $"{FirstName.Trim()} {LastName.Trim()}"
|
||||
}
|
||||
}
|
||||
});
|
||||
if (session is null) return;
|
||||
await SupabaseService.Client.Auth.SetSession(session.AccessToken, session.RefreshToken);
|
||||
var user = session.User;
|
||||
|
||||
|
||||
if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
||||
{
|
||||
desktop.MainWindow!.DataContext = user is not null ? new MainViewModel() : new AuthViewModel();
|
||||
}
|
||||
else if (Application.Current.ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform)
|
||||
{
|
||||
singleViewPlatform.MainView!.DataContext = user is not null ? new MainViewModel() : new AuthViewModel();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public bool isSignin => Operation == "login";
|
||||
public bool isCreateAccount => Operation == "signup";
|
||||
|
||||
public bool canSignin => isSignin && !string.IsNullOrWhiteSpace(_email) && !string.IsNullOrWhiteSpace(_password);
|
||||
|
||||
public bool canCreateAccount => isCreateAccount && !string.IsNullOrWhiteSpace(_firstName) && !string.IsNullOrWhiteSpace(_lastName) &&
|
||||
!string.IsNullOrWhiteSpace(_email) &&
|
||||
!string.IsNullOrWhiteSpace(_password) && _password == _confirmPassword;
|
||||
}
|
||||
Reference in New Issue
Block a user