Added functionality to save the user credentials for the purposes of retrieving a new token in the future.

This commit is contained in:
amazing-username
2019-06-19 17:15:00 -07:00
parent a59bfa67c0
commit 2c3a3ed943
8 changed files with 167 additions and 16 deletions
+1 -2
View File
@@ -7,7 +7,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mear.Android", "Mear\Mear.A
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mear.iOS", "Mear\Mear.iOS\Mear.iOS.csproj", "{A76BE057-EE1A-4B2B-997C-4D0953DB1E37}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mear.iOS", "Mear\Mear.iOS\Mear.iOS.csproj", "{A76BE057-EE1A-4B2B-997C-4D0953DB1E37}"
EndProject 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 EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution 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.Build.0 = Release|Any CPU
{FF6A18DF-D1DF-4D8C-A412-961A3FFF02AE}.Release|iPhoneSimulator.Deploy.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.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|Any CPU.Deploy.0 = Debug|iPhoneSimulator
{A76BE057-EE1A-4B2B-997C-4D0953DB1E37}.Debug|iPhone.ActiveCfg = Debug|iPhone {A76BE057-EE1A-4B2B-997C-4D0953DB1E37}.Debug|iPhone.ActiveCfg = Debug|iPhone
{A76BE057-EE1A-4B2B-997C-4D0953DB1E37}.Debug|iPhone.Build.0 = Debug|iPhone {A76BE057-EE1A-4B2B-997C-4D0953DB1E37}.Debug|iPhone.Build.0 = Debug|iPhone
+31 -1
View File
@@ -7,6 +7,7 @@ using RestSharp;
using Mear.Constants.API; using Mear.Constants.API;
using Mear.Models.Authentication; using Mear.Models.Authentication;
using Mear.Repositories.Database;
namespace Mear.Managers namespace Mear.Managers
{ {
@@ -30,7 +31,23 @@ namespace Mear.Managers
#region Methods #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 try
{ {
@@ -55,6 +72,19 @@ namespace Mear.Managers
return null; 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 #endregion
} }
} }
+13
View File
@@ -1,33 +1,46 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text; using System.Text;
using Newtonsoft.Json; using Newtonsoft.Json;
using SQLite;
namespace Mear.Models.Authentication namespace Mear.Models.Authentication
{ {
[DataContract]
[Table("User")]
public class User public class User
{ {
[PrimaryKey, Column("Id"), AutoIncrement]
[JsonProperty("id")] [JsonProperty("id")]
public int Id { get; set; } public int Id { get; set; }
[JsonProperty("username")] [JsonProperty("username")]
public string Username { get; set; } public string Username { get; set; }
[Ignore]
[JsonProperty("nickname")] [JsonProperty("nickname")]
public string Nickname { get; set; } public string Nickname { get; set; }
[JsonProperty("password")] [JsonProperty("password")]
public string Password { get; set; } public string Password { get; set; }
[Ignore]
[JsonProperty("email")] [JsonProperty("email")]
public string Email { get; set; } public string Email { get; set; }
[Ignore]
[JsonProperty("phone_number")] [JsonProperty("phone_number")]
public string PhoneNumber { get; set; } public string PhoneNumber { get; set; }
[Ignore]
[JsonProperty("first_name")] [JsonProperty("first_name")]
public string Firstname { get; set; } public string Firstname { get; set; }
[Ignore]
[JsonProperty("last_name")] [JsonProperty("last_name")]
public string Lastname { get; set; } public string Lastname { get; set; }
[Ignore]
[JsonProperty("email_verified")] [JsonProperty("email_verified")]
public bool EmailVerified { get; set; } public bool EmailVerified { get; set; }
[Ignore]
[JsonProperty("date_created")] [JsonProperty("date_created")]
public DateTime DateCreated { get; set; } public DateTime DateCreated { get; set; }
[Ignore]
[JsonProperty("last_login")] [JsonProperty("last_login")]
public DateTime? LastLogin { get; set; } public DateTime? LastLogin { get; set; }
} }
@@ -8,7 +8,6 @@ using Mear.Utilities;
namespace Mear.Repositories.Database namespace Mear.Repositories.Database
{ {
// TODO: Implement the database functionality #35
public class DBMusicControlsRepository : DBRepository public class DBMusicControlsRepository : DBRepository
{ {
#region Fields #region Fields
@@ -11,8 +11,10 @@ namespace Mear.Repositories.Database
{ {
public class DBRepository public class DBRepository
{ {
#region Fields #region Fields
protected static SQLiteConnection _DbConn = null;
protected SQLiteConnection _Db; protected SQLiteConnection _Db;
protected static string _table;
protected string _dbPath; protected string _dbPath;
protected string _tableName; protected string _tableName;
#endregion #endregion
@@ -31,6 +33,24 @@ namespace Mear.Repositories.Database
#region Methods #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) protected bool DoesTableExist(string tablename)
{ {
var result = 0; var result = 0;
@@ -51,6 +71,10 @@ namespace Mear.Repositories.Database
return false; return false;
} }
protected static void CloseDbConnection()
{
_DbConn.Close();
}
protected void CloseDb() protected void CloseDb()
{ {
_Db.Close(); _Db.Close();
@@ -63,6 +87,18 @@ namespace Mear.Repositories.Database
_Db = new SQLiteConnection(_dbPath); _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 #endregion
} }
} }
@@ -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<User>().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<User>().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<User>();
}
_DbConn.Insert(user);
}
catch (Exception ex)
{
var msg = ex.Message;
}
}
#endregion
}
}
+1 -8
View File
@@ -64,16 +64,9 @@ namespace Mear.Views
if (user != null) if (user != null)
{ {
var loginMgr = new LoginManager(user); 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(); App.Current.MainPage = new MusicLibrary();
} }
else else
+1 -3
View File
@@ -61,8 +61,6 @@ namespace Mear.Views
BackgroundSongElasping(); BackgroundSongElasping();
BackgroundSongAttributes(); BackgroundSongAttributes();
//BackgroundSongCoverUpdate();
//BackgroundControlInit();
InitializeOptions(); InitializeOptions();
} }
@@ -132,7 +130,7 @@ namespace Mear.Views
} }
}); });
await Task.Delay(250); await Task.Delay(100);
} }
}).Start(); }).Start();
} }