Auth token is stored in settings and deleted if a request is not authorized (with automatic re-login). Failed request need to be resend.
This commit is contained in:
@ -7,6 +7,8 @@
|
||||
|
||||
static const QString apiPrefix = "/api/";
|
||||
|
||||
static const QString ROUTE_LOG_IN = apiPrefix + "log_in";
|
||||
|
||||
static const QString ROUTE_ITEMS = apiPrefix + "items";
|
||||
|
||||
#endif // APIROUTES_H
|
||||
|
||||
@ -6,6 +6,8 @@
|
||||
#include <QJsonObject>
|
||||
#include <QRestReply>
|
||||
|
||||
#include "../formats/jsonparser.h"
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
|
||||
ServerCommunicator::ServerCommunicator(QObject* parent)
|
||||
@ -46,7 +48,14 @@ void ServerCommunicator::setServerConfiguration(const QString url,
|
||||
m_password = password;
|
||||
m_authToken = authToken;
|
||||
|
||||
if (!authToken.isEmpty()) {
|
||||
if (authToken.isEmpty()) {
|
||||
if (validLoginCredentials()) {
|
||||
const QByteArray userCredentials =
|
||||
JsonParser::userCredentialsToJsonDocument(m_email, m_password);
|
||||
sendPostRequest(ROUTE_LOG_IN, userCredentials);
|
||||
}
|
||||
} else {
|
||||
/// authToken not empty:
|
||||
m_serviceApi->setBearerToken(authToken.toLatin1());
|
||||
}
|
||||
}
|
||||
@ -62,6 +71,19 @@ void ServerCommunicator::deleteItem(const QString& id) {
|
||||
sendDeleteRequest(path);
|
||||
}
|
||||
|
||||
bool ServerCommunicator::validLoginCredentials() {
|
||||
if (url().isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
if (m_email.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
if (m_password.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void ServerCommunicator::sendGetRequest(const QString& path) {
|
||||
// TODO check for valid path, instead of emptiness
|
||||
if (path.isEmpty()) {
|
||||
@ -83,7 +105,11 @@ void ServerCommunicator::sendGetRequest(const QString& path) {
|
||||
} else {
|
||||
int statusCode = reply.httpStatus();
|
||||
qWarning() << "Request not successful:" << statusCode;
|
||||
onGetReplyFailure(path, QString("HTTP status code: %1").arg(statusCode));
|
||||
if (statusCode == 401) {
|
||||
notAuthorized(path);
|
||||
} else {
|
||||
onGetReplyFailure(path, QString("HTTP status code: %1").arg(statusCode));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -123,7 +149,11 @@ void ServerCommunicator::sendPostRequest(const QString& path, const QByteArray d
|
||||
int statusCode = reply.httpStatus();
|
||||
qWarning() << "Request not successful:" << statusCode;
|
||||
qInfo() << "Content:" << reply.readJson();
|
||||
onPostReplyFailure(path, QString("HTTP status code: %1").arg(statusCode));
|
||||
if (statusCode == 401) {
|
||||
notAuthorized(path);
|
||||
} else {
|
||||
onPostReplyFailure(path, QString("HTTP status code: %1").arg(statusCode));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -132,6 +162,9 @@ void ServerCommunicator::sendPostRequest(const QString& path, const QByteArray d
|
||||
void ServerCommunicator::onPostReplySuccessful(const QString& path, const QJsonDocument doc) {
|
||||
if (path == ROUTE_ITEMS) {
|
||||
emit sendItemSuccessful(doc.toJson());
|
||||
} else if (path == ROUTE_LOG_IN) {
|
||||
qCritical() << "Login success:" << doc.toJson(QJsonDocument::Compact);
|
||||
emit loginSuccessful(doc.toJson(QJsonDocument::Compact));
|
||||
} else {
|
||||
qWarning() << "Can't match request path:" << path;
|
||||
}
|
||||
@ -140,6 +173,9 @@ void ServerCommunicator::onPostReplySuccessful(const QString& path, const QJsonD
|
||||
void ServerCommunicator::onPostReplyFailure(const QString& path, const QString errorString) {
|
||||
if (path == ROUTE_ITEMS) {
|
||||
emit sendItemFailure(errorString);
|
||||
} else if (path == ROUTE_LOG_IN) {
|
||||
qCritical() << "Login failure:" << errorString;
|
||||
emit loginFailure(errorString);
|
||||
} else {
|
||||
qWarning() << "Can't match request path:" << path;
|
||||
}
|
||||
@ -160,7 +196,11 @@ void ServerCommunicator::sendDeleteRequest(const QString& path) {
|
||||
} else {
|
||||
int statusCode = reply.httpStatus();
|
||||
qWarning() << "Request not successful:" << statusCode;
|
||||
onDeleteReplyFailure(path, QString("HTTP status code: %1").arg(statusCode));
|
||||
if (statusCode == 401) {
|
||||
notAuthorized(path);
|
||||
} else {
|
||||
onDeleteReplyFailure(path, QString("HTTP status code: %1").arg(statusCode));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@ -30,6 +30,11 @@ class ServerCommunicator : public QObject {
|
||||
signals:
|
||||
void urlChanged();
|
||||
|
||||
void loginSuccessful(const QByteArray responseData);
|
||||
void loginFailure(const QString errorString);
|
||||
|
||||
void notAuthorized(const QString path);
|
||||
|
||||
void itemsFetched(const QByteArray jsonDoc);
|
||||
void itemsFetchFailure(const QString errorString);
|
||||
|
||||
@ -48,6 +53,8 @@ class ServerCommunicator : public QObject {
|
||||
QString m_password;
|
||||
QString m_authToken;
|
||||
|
||||
bool validLoginCredentials();
|
||||
|
||||
void sendGetRequest(const QString& path);
|
||||
void onGetReplySuccessful(const QString& path, const QJsonDocument doc);
|
||||
void onGetReplyFailure(const QString& path, const QString errorString);
|
||||
|
||||
Reference in New Issue
Block a user