If no authToken is stored in settings but email and password are on startup the log_in route is triggered to receive a new token. This token will be used to access a protected API.
This commit is contained in:
@ -5,12 +5,14 @@
|
||||
|
||||
// TODO add namespace
|
||||
|
||||
static const QString ROUTE_LOG_IN = "/log_in";
|
||||
static const QString ROUTE_USER_LOG_IN = "/log_in";
|
||||
|
||||
static const QString apiPrefix = "/api/";
|
||||
|
||||
static const QString ROUTE_ITEMS = apiPrefix + "items";
|
||||
|
||||
static const QString ROUTE_ADMIN_LOG_IN = apiPrefix + "log_in";
|
||||
|
||||
static const QString ROUTE_REGISTER_USER = apiPrefix + "users";
|
||||
static const QString ROUTE_MAIL_INVITE = apiPrefix + "invite";
|
||||
|
||||
|
||||
@ -10,6 +10,8 @@
|
||||
#include "../structs.h"
|
||||
#include "apiroutes.h"
|
||||
|
||||
#include "../data/settingshandler.h"
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
|
||||
ServerCommunicator::ServerCommunicator(GenericCore* core)
|
||||
@ -19,6 +21,9 @@ ServerCommunicator::ServerCommunicator(GenericCore* core)
|
||||
m_restManager = std::make_shared<QRestAccessManager>(&m_netManager);
|
||||
m_serviceApi = std::make_shared<QNetworkRequestFactory>();
|
||||
|
||||
connect(core, &GenericCore::loginAndStoreAuthToken, this, &ServerCommunicator::onLoginTriggered);
|
||||
connect(this, &ServerCommunicator::loginSuccessful, core, &GenericCore::onLoginSuccessful);
|
||||
|
||||
connect(m_core, &GenericCore::sendGetRequest, this,
|
||||
&ServerCommunicator::onSendGetRequestTriggered);
|
||||
connect(m_core, &GenericCore::sendPostRequest, this,
|
||||
@ -51,15 +56,21 @@ void ServerCommunicator::setUrl(const QUrl& url) {
|
||||
|
||||
void ServerCommunicator::setServerConfiguration(const QString url,
|
||||
const QString email,
|
||||
const QString password) {
|
||||
const QString password,
|
||||
const QString authToken) {
|
||||
setUrl(url);
|
||||
|
||||
m_email = email;
|
||||
m_password = password;
|
||||
m_email = email;
|
||||
m_password = password;
|
||||
m_authToken = authToken;
|
||||
|
||||
if (!m_authToken.isEmpty()) {
|
||||
m_serviceApi->setBearerToken(authToken.toLatin1());
|
||||
}
|
||||
}
|
||||
|
||||
QString ServerCommunicator::getUserLoginUrl() const {
|
||||
const QString logInUrl = m_serviceApi->baseUrl().toString() + ROUTE_LOG_IN;
|
||||
const QString logInUrl = m_serviceApi->baseUrl().toString() + ROUTE_USER_LOG_IN;
|
||||
return logInUrl;
|
||||
}
|
||||
|
||||
@ -96,6 +107,7 @@ void ServerCommunicator::onSendGetRequestTriggered(const GetRequestTypes type,
|
||||
return;
|
||||
}
|
||||
|
||||
m_serviceApi->setBearerToken(m_authToken.toLatin1());
|
||||
const QNetworkRequest request = m_serviceApi->createRequest(path);
|
||||
|
||||
m_restManager->get(request, this, [this, type](QRestReply& reply) {
|
||||
@ -145,6 +157,9 @@ void ServerCommunicator::onSendPostRequestTriggered(const PostRequestTypes type,
|
||||
const QByteArray data) {
|
||||
QString path;
|
||||
switch (type) {
|
||||
case LogInAdmin:
|
||||
path = ROUTE_ADMIN_LOG_IN;
|
||||
break;
|
||||
case RegisterUser:
|
||||
path = ROUTE_REGISTER_USER;
|
||||
break;
|
||||
@ -162,6 +177,7 @@ void ServerCommunicator::onSendPostRequestTriggered(const PostRequestTypes type,
|
||||
return;
|
||||
}
|
||||
|
||||
m_serviceApi->setBearerToken(m_authToken.toLatin1());
|
||||
const QNetworkRequest request = m_serviceApi->createRequest(path);
|
||||
|
||||
m_restManager->post(request, data, this, [this, type](QRestReply& reply) {
|
||||
@ -188,6 +204,10 @@ void ServerCommunicator::onSendPostRequestTriggered(const PostRequestTypes type,
|
||||
void ServerCommunicator::onPostReplySuccessful(const PostRequestTypes type,
|
||||
const QJsonDocument doc) {
|
||||
switch (type) {
|
||||
case LogInAdmin:
|
||||
qInfo() << "Admin successfully logged in:" << type;
|
||||
handleLogInReply(doc);
|
||||
break;
|
||||
case RegisterUser:
|
||||
qInfo() << "Register user successful:" << type;
|
||||
onlineUserAccountReply(doc);
|
||||
@ -207,7 +227,34 @@ void ServerCommunicator::onPostReplyFailure(const PostRequestTypes type,
|
||||
const QString message =
|
||||
QString("Request of type %1 returned: %2").arg(QString::number(type), errorString);
|
||||
// NEXT improve error message to the UI;
|
||||
m_core->displayStatusMessage(message);
|
||||
emit m_core->displayStatusMessage(message);
|
||||
}
|
||||
|
||||
QByteArray ServerCommunicator::createLoginBody() {
|
||||
QHash<QString, QVariant> values;
|
||||
values.insert("email", m_email);
|
||||
values.insert("password", m_password);
|
||||
return JsonParser::toJsonDoc(values, "admin");
|
||||
}
|
||||
|
||||
void ServerCommunicator::handleLogInReply(const QJsonDocument jsonDoc) {
|
||||
QJsonObject rootObject = jsonDoc.object();
|
||||
bool hasErrors = rootObject.contains(QString("errors"));
|
||||
if (hasErrors) {
|
||||
qCritical() << "Reply has error(s)!";
|
||||
QString errorString = rootObject["errors"].toString();
|
||||
emit m_core->displayStatusMessage(errorString);
|
||||
} else {
|
||||
qInfo() << "Reply has no error(s)!";
|
||||
const QJsonObject dataObject = rootObject["data"].toObject();
|
||||
const QString authToken = dataObject["token"].toString();
|
||||
m_authToken = authToken;
|
||||
|
||||
SettingsHandler::saveSettings({{"token", authToken}}, "Server");
|
||||
|
||||
emit loginSuccessful();
|
||||
emit m_core->displayStatusMessage("Successfully logged in.");
|
||||
}
|
||||
}
|
||||
|
||||
void ServerCommunicator::currentBiddingRoundChangedReply(const QJsonDocument jsonDoc) {
|
||||
@ -245,3 +292,30 @@ void ServerCommunicator::mailInviteSentReply(const QJsonDocument jsonDoc) {
|
||||
qInfo() << "Invitation mail successfully sent.";
|
||||
emit m_core->displayStatusMessage("Invitation mail successfully sent.");
|
||||
}
|
||||
|
||||
void ServerCommunicator::onLoginTriggered() {
|
||||
qInfo() << "Login triggered...";
|
||||
if (m_email.isEmpty() || m_password.isEmpty()) {
|
||||
emit m_core->displayStatusMessage(
|
||||
"Missing email or password in settings! Not trying to log in.");
|
||||
return;
|
||||
}
|
||||
if (m_authToken.isEmpty()) {
|
||||
/// get new authToken
|
||||
qWarning() << "Creating a new authToken!";
|
||||
const QByteArray loginBody = createLoginBody();
|
||||
onSendPostRequestTriggered(LogInAdmin, loginBody);
|
||||
|
||||
} else {
|
||||
/// try authToken
|
||||
qWarning() << "Validity check of token not implemented yet!!!";
|
||||
qInfo() << "Assuming validity of token...";
|
||||
|
||||
emit loginSuccessful();
|
||||
// TODO try validity of token and trigger log_in if not valid
|
||||
// try default route to test access;
|
||||
// if (access denied) {
|
||||
// m_apiClient->sendAPIRequestPost(ROUTE_LOG_IN_ADMIN, );
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,10 +22,15 @@ class ServerCommunicator : public QObject {
|
||||
QUrl url() const;
|
||||
void setUrl(const QUrl& url);
|
||||
|
||||
void setServerConfiguration(const QString url, const QString email, const QString password);
|
||||
void setServerConfiguration(const QString url,
|
||||
const QString email,
|
||||
const QString password,
|
||||
const QString authToken);
|
||||
QString getUserLoginUrl() const;
|
||||
|
||||
public slots:
|
||||
void onLoginTriggered();
|
||||
|
||||
void onSendGetRequestTriggered(const GetRequestTypes type, QVariant data);
|
||||
void onGetReplySuccessful(const GetRequestTypes type, const QJsonDocument doc);
|
||||
void onGetReplyFailure(const GetRequestTypes type, const QString errorString);
|
||||
@ -44,6 +49,8 @@ class ServerCommunicator : public QObject {
|
||||
void deleteRequestSuccessful(const QByteArray responseData);
|
||||
void deleteRequestFailure(const QString errorString);
|
||||
|
||||
void loginSuccessful();
|
||||
|
||||
void currentBiddingRoundChanged(int round, bool isRunning);
|
||||
void biddingsChanged(QList<bidding> biddings);
|
||||
void onlineUserAccountReceived(const QString mailAddress,
|
||||
@ -59,9 +66,13 @@ class ServerCommunicator : public QObject {
|
||||
|
||||
QString m_email;
|
||||
QString m_password;
|
||||
// QString m_authToken;
|
||||
QString m_authToken;
|
||||
|
||||
QByteArray createLoginBody();
|
||||
|
||||
/// reply parser
|
||||
void handleLogInReply(const QJsonDocument jsonDoc);
|
||||
|
||||
void currentBiddingRoundChangedReply(const QJsonDocument jsonDoc);
|
||||
void currentBiddingsReply(const QJsonDocument jsonDoc);
|
||||
void onlineUserAccountReply(const QJsonDocument jsonDoc);
|
||||
|
||||
Reference in New Issue
Block a user