Can store login tokens.

This commit is contained in:
Pierre HUBERT
2018-11-29 15:53:54 +01:00
parent 99724f845c
commit 1a3169bc16
13 changed files with 269 additions and 6 deletions

View File

@ -1,6 +1,9 @@
#include <QJsonObject>
#include "accounthelper.h"
#include "configurationhelper.h"
#include "../data/apirequest.h"
#include "../data/accountlogintokens.h"
#include "../helpers/apihelper.h"
AccountHelper::AccountHelper(QObject *parent) : QObject(parent)
@ -10,7 +13,7 @@ AccountHelper::AccountHelper(QObject *parent) : QObject(parent)
bool AccountHelper::signedIn()
{
return false; //TODO : implement
return ConfigurationHelper().getAccountTokens().isValid();
}
void AccountHelper::login(const AccountLoginRequest &info)
@ -53,7 +56,20 @@ void AccountHelper::requestLoginResult(const QJsonDocument &document)
//Delete API request
qobject_cast<APIRequest *>(sender())->deleteLater();
//Intend to parse server response
QJsonObject object = document.object().value("tokens").toObject();
if(object.isEmpty()){
qWarning("objects 'tokens' of server response is empty!");
emit loginResult(LoginResult::INVALID_SERVER_RESPONSE);
return;
}
//Intend to parse get login responses
AccountLoginTokens tokens;
tokens.setToken1(object.value("token1").toString());
tokens.setToken2(object.value("token2").toString());
ConfigurationHelper().setAccountTokens(tokens);
//Success
emit loginResult(LoginResult::LOGIN_SUCCESS);

View File

@ -0,0 +1,34 @@
#include <QSettings>
#include "configurationhelper.h"
#include "../config.h"
ConfigurationHelper::ConfigurationHelper()
{
mSettings = new QSettings();
}
ConfigurationHelper::~ConfigurationHelper()
{
mSettings->deleteLater();
}
AccountLoginTokens ConfigurationHelper::getAccountTokens()
{
//Check if we have both of the tokens
if(!mSettings->value(SETTINGS_ACCOUNT_LOGIN_TOKEN_1).isValid() ||
!mSettings->value(SETTINGS_ACCOUNT_LOGIN_TOKEN_1).isValid())
return AccountLoginTokens(); //Return invalid object : not all settings available
//Parse and return account login tokens
AccountLoginTokens tokens;
tokens.setToken1(mSettings->value(SETTINGS_ACCOUNT_LOGIN_TOKEN_1).toString());
tokens.setToken2(mSettings->value(SETTINGS_ACCOUNT_LOGIN_TOKEN_2).toString());
return tokens;
}
void ConfigurationHelper::setAccountTokens(const AccountLoginTokens &tokens)
{
mSettings->setValue(SETTINGS_ACCOUNT_LOGIN_TOKEN_1, tokens.token1());
mSettings->setValue(SETTINGS_ACCOUNT_LOGIN_TOKEN_2, tokens.token2());
}

View File

@ -0,0 +1,41 @@
/**
* Configuration helper
*
* @author Pierre HUBERT
*/
#ifndef CONFIGURATIONHELPER_H
#define CONFIGURATIONHELPER_H
#include <QObject>
#include "../data/accountlogintokens.h"
class QSettings;
class ConfigurationHelper
{
public:
ConfigurationHelper();
~ConfigurationHelper();
/**
* Get the login tokens associated with this account
*
* @return The login tokens associated with this account /
* invalid object if none found
*/
AccountLoginTokens getAccountTokens();
/**
* Set the login tokens associated with the current account
*
* @param tokens The tokens to set
*/
void setAccountTokens(const AccountLoginTokens &tokens);
private:
QSettings *mSettings;
};
#endif // CONFIGURATIONHELPER_H