private async Task<User> LoginWithCredentials(UserCredential credential, bool userInitiated)
{
var originalCookie = credential.LoginCookie;
if (!string.IsNullOrWhiteSpace(credential.LoginCookie))
{
var loggedInUser = new User { Username = credential.Username, LoginCookie = credential.LoginCookie, NeedsCaptcha = false };
if (userInitiated)
{
loggedInUser.Me = await _redditService.GetMe(loggedInUser);
}
else
{
ThreadPool.RunAsync(async (o) =>
{
await Task.Delay(5000);
try
{
loggedInUser.Me = await _redditService.GetMe(loggedInUser);
}
catch { }
if (loggedInUser.Me == null)
ServiceLocator.Current.GetInstance<INotificationService>().CreateNotification(string.Format("Failed to login with user {0}", credential.Username));
});
}
return loggedInUser;
}
else
{
//we dont currently posses a valid login cookie, see if windows has a stored credential we can use for this username
var userInfoDb = await GetUserInfoDB();
var passwordCursor = await userInfoDb.SelectAsync(userInfoDb.GetKeys().First(), "passwords", DBReadFlags.NoLock);
if (passwordCursor != null)
{
using (passwordCursor)
{
do
{
try
{
var passwordData = JsonConvert.DeserializeObject<PasswordData>(passwordCursor.GetString());
if (credential.LoginCookie == passwordData.LastCookie)
{
return await _redditService.Login(credential.Username, passwordData.Password);
}
}
catch
{
}
} while (await passwordCursor.MoveNextAsync());
}
}
}
return null;
}