comunicwatcher/accounthelper.cpp

58 lines
1.3 KiB
C++
Raw Normal View History

2020-06-13 07:49:24 +00:00
#include "accounthelper.h"
#include "apirequest.h"
2020-06-13 08:19:49 +00:00
#include <QSettings>
2020-06-13 07:49:24 +00:00
2020-06-13 08:19:49 +00:00
#define LOGIN_TOKEN_VALUE "login_token"
2020-06-13 07:49:24 +00:00
void AccountHelper::LoginUser(const QString &email, const QString &password, const vLoginCallback cb)
{
auto req = new APIRequest("account/login");
req->addString("userMail", email);
req->addString("userPassword", password);
req->exec();
QObject::connect(req, &APIRequest::done, [=] (APIResponse res) {
QString token;
switch(res.getCode()) {
case 200:
// Save login token
token = res.getObject().value("tokens").toObject().value("token1").toString();
2020-06-13 08:19:49 +00:00
QSettings().setValue(LOGIN_TOKEN_VALUE, token);
2020-06-13 07:49:24 +00:00
cb(LoginResult::SUCCESS);
break;
case 401:
cb(LoginResult::BAD_PASSWORD);
break;
case 429:
cb(LoginResult::TOO_MANY_REQUEST);
break;
default:
cb(LoginResult::ERROR);
break;
}
});
}
2020-06-13 08:19:49 +00:00
bool AccountHelper::SignedIn()
{
return QSettings().value(LOGIN_TOKEN_VALUE).isValid();
}
QString AccountHelper::GetLoginToken()
{
return QSettings().value(LOGIN_TOKEN_VALUE).toString();
}
void AccountHelper::RemoveLoginToken()
{
QSettings().remove(LOGIN_TOKEN_VALUE);
}