Added functionality to save the user credentials for the purposes of retrieving a new token in the future.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ using Mear.Utilities;
|
||||
|
||||
namespace Mear.Repositories.Database
|
||||
{
|
||||
// TODO: Implement the database functionality #35
|
||||
public class DBMusicControlsRepository : DBRepository
|
||||
{
|
||||
#region Fields
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user