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:
2026-05-08 14:44:40 +02:00
parent ceaf6b5fbd
commit 0feaf09d83
9 changed files with 189 additions and 6 deletions

View File

@ -14,6 +14,7 @@
#include "constants.h"
#include "data/filehandler.h"
#include "data/settingshandler.h"
#include "formats/jsonparser.h"
#include "model/generalsortfiltermodel.h"
#include "model/metadata.h"
#include "model/tablemodel.h"
@ -126,6 +127,24 @@ QVariantMap GenericCore::getSettings(QString group) const {
}
void GenericCore::applySettings(QVariantMap settingMap, QString group) {
const QVariantMap changeset = SettingsHandler::getChangeset(settingMap, group);
if (changeset.isEmpty()) {
return;
}
if (group == "Server") {
const bool urlChanged = changeset.contains("url");
const bool emailChanged = changeset.contains("email");
const bool passwordChanged = changeset.contains("password");
if (urlChanged || emailChanged || passwordChanged) {
if (!changeset.contains("authToken")) {
qInfo() << "Account settings changed, but no new token present. Deleting old token...";
SettingsHandler::deleteSettings({"authToken"}, "Server");
}
}
}
SettingsHandler::saveSettings(settingMap, group);
if (group == "Server") {
@ -158,6 +177,30 @@ void GenericCore::saveItems() {
}
}
void GenericCore::onLoginSuccessful(const QByteArray jsonData) {
emit displayStatusMessage("Login successful.");
qInfo() << "Storing auth token...";
QString token = JsonParser::getValueFromJson(jsonData, "token", "user").toString();
SettingsHandler::saveSettings({{"authToken", token}}, "Server");
applyServerConfiguration();
}
void GenericCore::onLoginFailure(const QString errorString) {
emit displayStatusMessage(QString("Error: %1").arg(errorString));
}
void GenericCore::onNotAuthorized(const QString /*path*/) {
const QVariantMap serverSettings = SettingsHandler::getSettings("Server");
const QString tokenValue = serverSettings.value("authToken").toString();
if (!tokenValue.isEmpty()) {
SettingsHandler::deleteSettings({"authToken"}, "Server");
displayStatusMessage("Not authorized! Deleted token. Please retry.");
} else {
displayStatusMessage("Not authorized! But no token was present. Please check your settings.");
}
applyServerConfiguration();
}
void GenericCore::onSendItemTriggered(const QByteArray& jsonData) {
m_serverCommunicator->sendItem(jsonData);
}
@ -258,6 +301,14 @@ void GenericCore::setupServerCommunication() {
&ServerCommunicator::deleteItem);
/// response connections
connect(m_serverCommunicator.get(), &ServerCommunicator::loginSuccessful, this,
&GenericCore::onLoginSuccessful);
connect(m_serverCommunicator.get(), &ServerCommunicator::loginFailure, this,
&GenericCore::onLoginFailure);
connect(m_serverCommunicator.get(), &ServerCommunicator::notAuthorized, this,
&GenericCore::onNotAuthorized);
connect(m_serverCommunicator.get(), &ServerCommunicator::itemsFetched, this,
&GenericCore::onItemsFetched);
connect(m_serverCommunicator.get(), &ServerCommunicator::itemsFetchFailure, this,
@ -277,8 +328,10 @@ void GenericCore::setupServerCommunication() {
void GenericCore::applyServerConfiguration() {
const QVariantMap serverSettings = SettingsHandler::getSettings("Server");
const QString urlValue = serverSettings.value("url").toString();
// NEXT if urlValue is empty -> remove authToken from settings?
if (!urlValue.isEmpty()) {
if (urlValue.isEmpty()) {
SettingsHandler::deleteSettings({"authToken"}, "Server");
} else {
/// urlValue in NOT empty
const QString emailValue = serverSettings.value("email").toString();
const QString passwordValue = serverSettings.value("password").toString();
const QString authTokenValue = serverSettings.value("authToken").toString();