From 2c3a3ed943271847c1fd5b692fa3ef7e4def3094 Mon Sep 17 00:00:00 2001 From: amazing-username Date: Wed, 19 Jun 2019 17:15:00 -0700 Subject: [PATCH] Added functionality to save the user credentials for the purposes of retrieving a new token in the future. --- Mear.sln | 3 +- Mear/Mear/Managers/LoginManager.cs | 32 ++++++- Mear/Mear/Models/Authentication/User.cs | 13 +++ .../Database/DBMusicControlsRepository.cs | 1 - .../Repositories/Database/DBRepository.cs | 38 ++++++++- .../Repositories/Database/DBUserRepository.cs | 83 +++++++++++++++++++ Mear/Mear/Views/LoginPage.xaml.cs | 9 +- Mear/Mear/Views/MearPlayerView.xaml.cs | 4 +- 8 files changed, 167 insertions(+), 16 deletions(-) create mode 100644 Mear/Mear/Repositories/Database/DBUserRepository.cs diff --git a/Mear.sln b/Mear.sln index 397633a..b30c553 100644 --- a/Mear.sln +++ b/Mear.sln @@ -7,7 +7,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mear.Android", "Mear\Mear.A EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mear.iOS", "Mear\Mear.iOS\Mear.iOS.csproj", "{A76BE057-EE1A-4B2B-997C-4D0953DB1E37}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mear", "Mear\Mear\Mear.csproj", "{3A4FB936-3738-4D2B-A116-B9CABEEADDE0}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mear", "Mear\Mear\Mear.csproj", "{3A4FB936-3738-4D2B-A116-B9CABEEADDE0}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -38,7 +38,6 @@ Global {FF6A18DF-D1DF-4D8C-A412-961A3FFF02AE}.Release|iPhoneSimulator.Build.0 = Release|Any CPU {FF6A18DF-D1DF-4D8C-A412-961A3FFF02AE}.Release|iPhoneSimulator.Deploy.0 = Release|Any CPU {A76BE057-EE1A-4B2B-997C-4D0953DB1E37}.Debug|Any CPU.ActiveCfg = Debug|iPhoneSimulator - {A76BE057-EE1A-4B2B-997C-4D0953DB1E37}.Debug|Any CPU.Build.0 = Debug|iPhoneSimulator {A76BE057-EE1A-4B2B-997C-4D0953DB1E37}.Debug|Any CPU.Deploy.0 = Debug|iPhoneSimulator {A76BE057-EE1A-4B2B-997C-4D0953DB1E37}.Debug|iPhone.ActiveCfg = Debug|iPhone {A76BE057-EE1A-4B2B-997C-4D0953DB1E37}.Debug|iPhone.Build.0 = Debug|iPhone diff --git a/Mear/Mear/Managers/LoginManager.cs b/Mear/Mear/Managers/LoginManager.cs index 981ce7b..e696545 100644 --- a/Mear/Mear/Managers/LoginManager.cs +++ b/Mear/Mear/Managers/LoginManager.cs @@ -7,6 +7,7 @@ using RestSharp; using Mear.Constants.API; using Mear.Models.Authentication; +using Mear.Repositories.Database; namespace Mear.Managers { @@ -30,7 +31,23 @@ namespace Mear.Managers #region Methods - public LoginResult Login() + public bool Authenticate() + { + var loginRes = Login(); + + if (loginRes.Expiration > 0 && loginRes != null) + { + DBUserRepository.DeleteUser(); + + SaveToDatabase(loginRes); + + return true; + } + + return false; + } + + private LoginResult Login() { try { @@ -55,6 +72,19 @@ namespace Mear.Managers return null; } + + private async void SaveToDatabase(LoginResult loginRes) + { + DBTokenRepository tokRepo = new DBTokenRepository(); + tokRepo.SaveToken(new Token + { + AccessToken = loginRes.Token, + UserId = loginRes.UserId + }); + + DBUserRepository.SaveUser(_user); + var usr = DBUserRepository.RetrieveUser(); + } #endregion } } diff --git a/Mear/Mear/Models/Authentication/User.cs b/Mear/Mear/Models/Authentication/User.cs index ffa3558..9aa0e63 100644 --- a/Mear/Mear/Models/Authentication/User.cs +++ b/Mear/Mear/Models/Authentication/User.cs @@ -1,33 +1,46 @@ using System; using System.Collections.Generic; +using System.Runtime.Serialization; using System.Text; using Newtonsoft.Json; +using SQLite; namespace Mear.Models.Authentication { + [DataContract] + [Table("User")] public class User { + [PrimaryKey, Column("Id"), AutoIncrement] [JsonProperty("id")] public int Id { get; set; } [JsonProperty("username")] public string Username { get; set; } + [Ignore] [JsonProperty("nickname")] public string Nickname { get; set; } [JsonProperty("password")] public string Password { get; set; } + [Ignore] [JsonProperty("email")] public string Email { get; set; } + [Ignore] [JsonProperty("phone_number")] public string PhoneNumber { get; set; } + [Ignore] [JsonProperty("first_name")] public string Firstname { get; set; } + [Ignore] [JsonProperty("last_name")] public string Lastname { get; set; } + [Ignore] [JsonProperty("email_verified")] public bool EmailVerified { get; set; } + [Ignore] [JsonProperty("date_created")] public DateTime DateCreated { get; set; } + [Ignore] [JsonProperty("last_login")] public DateTime? LastLogin { get; set; } } diff --git a/Mear/Mear/Repositories/Database/DBMusicControlsRepository.cs b/Mear/Mear/Repositories/Database/DBMusicControlsRepository.cs index bacfcd8..02159b7 100644 --- a/Mear/Mear/Repositories/Database/DBMusicControlsRepository.cs +++ b/Mear/Mear/Repositories/Database/DBMusicControlsRepository.cs @@ -8,7 +8,6 @@ using Mear.Utilities; namespace Mear.Repositories.Database { - // TODO: Implement the database functionality #35 public class DBMusicControlsRepository : DBRepository { #region Fields diff --git a/Mear/Mear/Repositories/Database/DBRepository.cs b/Mear/Mear/Repositories/Database/DBRepository.cs index 401b15e..a9de13b 100644 --- a/Mear/Mear/Repositories/Database/DBRepository.cs +++ b/Mear/Mear/Repositories/Database/DBRepository.cs @@ -11,8 +11,10 @@ namespace Mear.Repositories.Database { public class DBRepository { - #region Fields + #region Fields + protected static SQLiteConnection _DbConn = null; protected SQLiteConnection _Db; + protected static string _table; protected string _dbPath; protected string _tableName; #endregion @@ -31,6 +33,24 @@ namespace Mear.Repositories.Database #region Methods + protected static bool TableExists() + { + try + { + var result = _DbConn.GetTableInfo(_table).Count; + + if (result > 0) + { + return true; + } + } + catch (Exception ex) + { + var msg = ex.Message; + } + + return false; + } protected bool DoesTableExist(string tablename) { var result = 0; @@ -51,6 +71,10 @@ namespace Mear.Repositories.Database return false; } + protected static void CloseDbConnection() + { + _DbConn.Close(); + } protected void CloseDb() { _Db.Close(); @@ -63,6 +87,18 @@ namespace Mear.Repositories.Database _Db = new SQLiteConnection(_dbPath); } + protected static void InitializeDatabase(string tablename) + { + if (_DbConn != null) + { + return; + } + + _table = tablename; + var appName = Info.AppName; + _DbConn = new SQLiteConnection(Path.Combine(Environment.GetFolderPath( + Environment.SpecialFolder.Personal), appName)); + } #endregion } } diff --git a/Mear/Mear/Repositories/Database/DBUserRepository.cs b/Mear/Mear/Repositories/Database/DBUserRepository.cs new file mode 100644 index 0000000..6484e4a --- /dev/null +++ b/Mear/Mear/Repositories/Database/DBUserRepository.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +using Mear.Models; +using Mear.Models.Authentication; + +namespace Mear.Repositories.Database +{ + public class DBUserRepository : DBRepository + { + #region Fields + #endregion + + + #region Properties + #endregion + + + #region Constructors + #endregion + + + #region Methods + public static User RetrieveUser() + { + InitializeDatabase("User"); + try + { + if (TableExists()) + { + var user = _DbConn.Table().First(); + + if (user != null) + { + return user; + } + } + } + catch (Exception ex) + { + var msg = ex.Message; + } + + return null; + } + + public static void DeleteUser() + { + InitializeDatabase("User"); + try + { + if (TableExists()) + { + _DbConn.Table().Delete(u => !string.IsNullOrEmpty(u.Username)); + } + } + catch (Exception ex) + { + var msg = ex.Message; + } + } + public static void SaveUser(User user) + { + InitializeDatabase("User"); + try + { + if (!TableExists()) + { + _DbConn.CreateTable(); + } + + _DbConn.Insert(user); + } + catch (Exception ex) + { + var msg = ex.Message; + } + } + #endregion + } +} diff --git a/Mear/Mear/Views/LoginPage.xaml.cs b/Mear/Mear/Views/LoginPage.xaml.cs index 5421ae9..232d3e7 100644 --- a/Mear/Mear/Views/LoginPage.xaml.cs +++ b/Mear/Mear/Views/LoginPage.xaml.cs @@ -64,16 +64,9 @@ namespace Mear.Views if (user != null) { var loginMgr = new LoginManager(user); - var loginRes = loginMgr.Login(); - if (loginRes.Expiration > 0 && loginRes != null) + if (loginMgr.Authenticate()) { - DBTokenRepository tokRepo = new DBTokenRepository(); - tokRepo.SaveToken(new Token - { - AccessToken = loginRes.Token, - UserId = loginRes.UserId - }); App.Current.MainPage = new MusicLibrary(); } else diff --git a/Mear/Mear/Views/MearPlayerView.xaml.cs b/Mear/Mear/Views/MearPlayerView.xaml.cs index 865090d..bf7d9c2 100644 --- a/Mear/Mear/Views/MearPlayerView.xaml.cs +++ b/Mear/Mear/Views/MearPlayerView.xaml.cs @@ -61,8 +61,6 @@ namespace Mear.Views BackgroundSongElasping(); BackgroundSongAttributes(); - //BackgroundSongCoverUpdate(); - //BackgroundControlInit(); InitializeOptions(); } @@ -132,7 +130,7 @@ namespace Mear.Views } }); - await Task.Delay(250); + await Task.Delay(100); } }).Start(); }