mekasu0124

Untitled

Mar 16th, 2024
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.47 KB | None | 0 0
  1. using MakeMyProject.Services;
  2. using MakeMyProject.Models;
  3. using ReactiveUI;
  4. using System.Reactive;
  5. using System.Threading;
  6. using System;
  7. using System.Security.Cryptography.X509Certificates;
  8.  
  9. namespace MakeMyProject.ViewModels;
  10.  
  11. public class LoginViewModel : ViewModelBase
  12. {
  13.     private string _title = "";
  14.     private string _username = "";
  15.     private string _password = "";
  16.     private bool _isError = false;
  17.     private string _errorText = "";
  18.     private bool _success = false;
  19.     private string _successText = "";
  20.     private static readonly string specialCharacters = "!@#$%^&_";
  21.     private UserModel _newUser = new();
  22.  
  23.     public LoginViewModel()
  24.     {
  25.         Title = "Login To Get Started";
  26.  
  27.         IObservable<bool> isEnabled = this.WhenAnyValue(
  28.             x => x.Title,
  29.             x => !string.IsNullOrEmpty(x) && !string.IsNullOrWhiteSpace(x));
  30.  
  31.         Ok = ReactiveCommand.Create(ReturnUser, isEnabled);
  32.         Cancel = ReactiveCommand.Create(ExitProgram, isEnabled);
  33.     }
  34.  
  35.     public void ExitProgram() => Environment.Exit(0);
  36.  
  37.     public UserModel ReturnUser()
  38.     {
  39.         NewUser = new()
  40.         {
  41.             Username = Username,
  42.             Password = Password
  43.         };
  44.  
  45.         bool canReturn = RunChecks();
  46.  
  47.         if (canReturn)
  48.         {
  49.             return NewUser;
  50.         }
  51.  
  52.         return new UserModel();
  53.     }
  54.  
  55.     public bool RunChecks()
  56.     {
  57.         bool usernameValid = Helpers.ValidateUsername(Username);
  58.         bool createPasswordValid = Helpers.ValidatePassword(Password);
  59.         bool userFound = JsonEngine.GetUser(NewUser);
  60.         bool isOk = false;
  61.  
  62.         if (!usernameValid)
  63.         {
  64.             isOk = HandleInvalidInput("Invalid Username: Username Must Be " +
  65.                                             "At least 8 Characters In Length, " +
  66.                                             "Have At least 1 lower case letter, " +
  67.                                             "Have At least 1 upper case letter, " +
  68.                                             "Have At least 1 number", "un");
  69.             if (!isOk)
  70.             {
  71.                 return false;
  72.             }
  73.         }
  74.  
  75.         if (!createPasswordValid)
  76.         {
  77.             isOk = HandleInvalidInput("Invalid Password: Password Must Be " +
  78.                                 "At least 8 Characters In Length And " +
  79.                                 "Have At Least: 1 lower case letter, " +
  80.                                 "1 upper case letter, 1 number, 1 Special " +
  81.                                 "Character: " + string.Join(", ", specialCharacters), "pw");
  82.  
  83.             if (!isOk)
  84.             {
  85.                 return false;
  86.             }
  87.         }
  88.  
  89.         if (!userFound)
  90.         {
  91.             isOk = HandleInvalidInput("Invalid Username/Password. Try again", "nm");
  92.  
  93.             if (!isOk)
  94.             {
  95.                 return false;
  96.             }
  97.         }
  98.  
  99.         Success = true;
  100.         SuccessText = "Login Successful";
  101.         return true;
  102.     }
  103.  
  104.     public bool HandleInvalidInput(string errorText, string trigger)
  105.     {
  106.         IsError = true;
  107.         ErrorText = errorText;
  108.  
  109.         Username = "";
  110.         Password = "";
  111.  
  112.         Thread.Sleep(6000);
  113.  
  114.         IsError = false;
  115.         ErrorText = "";
  116.  
  117.         return false;
  118.     }
  119.  
  120.     public ReactiveCommand<Unit, UserModel> Ok { get; }
  121.     public ReactiveCommand<Unit, Unit> Cancel { get; }
  122.  
  123.     public UserModel NewUser
  124.     {
  125.         get => _newUser;
  126.         set => this.RaiseAndSetIfChanged(ref _newUser, value);
  127.     }
  128.  
  129.     public string Title
  130.     {
  131.         get => _title;
  132.         set => this.RaiseAndSetIfChanged(ref _title, value);
  133.     }
  134.  
  135.     public string Username
  136.     {
  137.         get => _username;
  138.         set => this.RaiseAndSetIfChanged(ref _username, value);
  139.     }
  140.  
  141.     public string Password
  142.     {
  143.         get => _password;
  144.         set => this.RaiseAndSetIfChanged(ref _password, value);
  145.     }
  146.  
  147.     public bool IsError
  148.     {
  149.         get => _isError;
  150.         set => this.RaiseAndSetIfChanged(ref _isError, value);
  151.     }
  152.  
  153.     public string ErrorText
  154.     {
  155.         get => _errorText;
  156.         set => this.RaiseAndSetIfChanged(ref _errorText, value);
  157.     }
  158.  
  159.     public bool Success
  160.     {
  161.         get => _success;
  162.         set => this.RaiseAndSetIfChanged(ref _success, value);
  163.     }
  164.  
  165.     public string SuccessText
  166.     {
  167.         get => _successText;
  168.         set => this.RaiseAndSetIfChanged(ref _successText, value);
  169.     }
  170. }
  171.  
Advertisement
Add Comment
Please, Sign In to add comment