Sending register, handling register result, sending login
This commit is contained in:
parent
89aa830bfa
commit
73e91e658f
15 changed files with 295 additions and 90 deletions
|
@ -1,9 +1,11 @@
|
|||
set(HEADERS
|
||||
api.h
|
||||
codes.h
|
||||
)
|
||||
|
||||
set(SOURCES
|
||||
api.cpp
|
||||
codes.cpp
|
||||
)
|
||||
|
||||
target_sources(magpie PRIVATE ${SOURCES})
|
||||
|
|
82
API/api.cpp
82
API/api.cpp
|
@ -4,6 +4,8 @@
|
|||
#include <QJsonObject>
|
||||
#include <QUrlQuery>
|
||||
|
||||
#include "codes.h"
|
||||
|
||||
constexpr const char* json = "application/json";
|
||||
constexpr const char* urlEncoded = "application/x-www-form-urlencoded";
|
||||
|
||||
|
@ -93,18 +95,21 @@ void API::onTestFinished(QNetworkReply* reply, const QUrl& addr, const QJSValue&
|
|||
setAddress(addr);
|
||||
}
|
||||
|
||||
void API::sendRegister(const QString& login, const QString& password, const QJSValue &finished) {
|
||||
void API::sendRegister(const QString& login, const QString& password, const QJSValue& finished) {
|
||||
qDebug() << "Registering...";
|
||||
if (state != NotAuthenticated)
|
||||
callCallback(finished, "Can not register in current state");
|
||||
return;
|
||||
return callCallback(finished, "Can not register in current state");
|
||||
|
||||
QUrlQuery params({
|
||||
{"login", login},
|
||||
{"password", password}
|
||||
});
|
||||
|
||||
QNetworkRequest request(address.path() + "/register");
|
||||
QString path = address.path();
|
||||
QUrl regUrl = address;
|
||||
regUrl.setPath(path + "/register");
|
||||
|
||||
QNetworkRequest request(regUrl);
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, urlEncoded);
|
||||
|
||||
QNetworkReply* reply = network.post(request, params.toString(QUrl::FullyEncoded).toUtf8());
|
||||
|
@ -113,7 +118,30 @@ void API::sendRegister(const QString& login, const QString& password, const QJSV
|
|||
);
|
||||
}
|
||||
|
||||
void API::onRegisterFinished(QNetworkReply *reply, const QJSValue &finished) const {
|
||||
void API::sendLogin(const QString& login, const QString& password, const QJSValue& finished) {
|
||||
qDebug() << "Logging in...";
|
||||
if (state != NotAuthenticated)
|
||||
return callCallback(finished, "Can not register in current state");
|
||||
|
||||
QUrlQuery params({
|
||||
{"login", login},
|
||||
{"password", password}
|
||||
});
|
||||
|
||||
QString path = address.path();
|
||||
QUrl url = address;
|
||||
url.setPath(path + "/login");
|
||||
|
||||
QNetworkRequest request(url);
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, urlEncoded);
|
||||
|
||||
QNetworkReply* reply = network.post(request, params.toString(QUrl::FullyEncoded).toUtf8());
|
||||
connect(reply, &QNetworkReply::finished,
|
||||
std::bind(&API::onLoginFinished, this, reply, finished)
|
||||
);
|
||||
}
|
||||
|
||||
void API::onRegisterFinished(QNetworkReply* reply, const QJSValue& finished) const {
|
||||
std::unique_ptr<QNetworkReply, NetworkReplyDeleter> rpl(reply);
|
||||
QNetworkReply::NetworkError error = reply->error();
|
||||
if (error != QNetworkReply::NoError)
|
||||
|
@ -125,24 +153,50 @@ void API::onRegisterFinished(QNetworkReply *reply, const QJSValue &finished) con
|
|||
!contentType.canConvert<QString>() ||
|
||||
contentType.toString() != json
|
||||
) {
|
||||
QString err("wrong response content type");
|
||||
qDebug() << "Register failed:" << err;
|
||||
callCallback(finished, err);
|
||||
return;
|
||||
return callCallback(finished, "wrong response content type");
|
||||
}
|
||||
|
||||
QByteArray data = reply->readAll();
|
||||
QJsonDocument document = QJsonDocument::fromJson(data);
|
||||
QJsonObject rootObj = document.object();
|
||||
|
||||
QJsonValue result = rootObj.value("result");
|
||||
if (!result.isString())
|
||||
QJsonValue res = rootObj.value("result");
|
||||
if (!res.isDouble())
|
||||
return callCallback(finished, "malformed json");
|
||||
|
||||
if (result.toString() != "ok")
|
||||
return callCallback(finished, "Registration result was not okay");
|
||||
Codes::Register result = Codes::convertRegister(res.toInt());
|
||||
bool success = result == Codes::Register::success;
|
||||
QString err;
|
||||
if (!success)
|
||||
err = Codes::description(result);
|
||||
|
||||
callCallback(finished, QString(), {QJSValue(true)});
|
||||
callCallback(finished, err, {QJSValue(success)});
|
||||
}
|
||||
|
||||
void API::onLoginFinished(QNetworkReply* reply, const QJSValue& finished) {
|
||||
std::unique_ptr<QNetworkReply, NetworkReplyDeleter> rpl(reply);
|
||||
QNetworkReply::NetworkError error = reply->error();
|
||||
if (error != QNetworkReply::NoError)
|
||||
return callCallback(finished, reply->errorString());
|
||||
|
||||
QVariant contentType = reply->header(QNetworkRequest::ContentTypeHeader);
|
||||
if (!
|
||||
contentType.isValid() ||
|
||||
!contentType.canConvert<QString>() ||
|
||||
contentType.toString() != json
|
||||
) {
|
||||
return callCallback(finished, "wrong response content type");
|
||||
}
|
||||
|
||||
QByteArray data = reply->readAll();
|
||||
QJsonDocument document = QJsonDocument::fromJson(data);
|
||||
QJsonObject rootObj = document.object();
|
||||
|
||||
QJsonValue res = rootObj.value("result");
|
||||
if (!res.isDouble())
|
||||
return callCallback(finished, "malformed json");
|
||||
|
||||
//todo
|
||||
}
|
||||
|
||||
void API::callCallback(const QJSValue& callback, const QString& error, const QJSValueList& arguments) const {
|
||||
|
|
|
@ -34,10 +34,12 @@ signals:
|
|||
public slots:
|
||||
void test(const QString& path, const QJSValue& finished = QJSValue());
|
||||
void sendRegister(const QString& login, const QString& password, const QJSValue& finished = QJSValue());
|
||||
void sendLogin(const QString& login, const QString& password, const QJSValue& finished = QJSValue());
|
||||
|
||||
private slots:
|
||||
void onTestFinished(QNetworkReply* reply, const QUrl& addr, const QJSValue& finished);
|
||||
void onRegisterFinished(QNetworkReply* reply, const QJSValue& finished) const;
|
||||
void onLoginFinished(QNetworkReply* reply, const QJSValue& finished);
|
||||
|
||||
private:
|
||||
void callCallback(const QJSValue& callback, const QString& error = QString(), const QJSValueList& arguments = QJSValueList()) const;
|
||||
|
|
33
API/codes.cpp
Normal file
33
API/codes.cpp
Normal file
|
@ -0,0 +1,33 @@
|
|||
#include "codes.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
constexpr std::array<const char*, uint8_t(Codes::Register::unknownError) + 1> registerErrors({
|
||||
"Successfully registered an account",
|
||||
"Login have not been provided",
|
||||
"The login can not be empty",
|
||||
"User with provided login already exists",
|
||||
"Provided login doesn't comply with server policies",
|
||||
"Password have not been provided",
|
||||
"Password can not be empty",
|
||||
"Provided password doesn't comply with server policies",
|
||||
"Unknown registration error"
|
||||
});
|
||||
|
||||
Codes::Register Codes::convertRegister (int source) {
|
||||
if (source < 0) {
|
||||
std::cerr << "Converting Register response code which is smaller than zero, falling back to unknownError" << std::endl;
|
||||
return Register::unknownError;
|
||||
}
|
||||
|
||||
if (source > (int)Register::unknownError) {
|
||||
std::cerr << "Converting Register response code which is bigger than biggest known, falling back to unknownError" << std::endl;
|
||||
return Register::unknownError;
|
||||
}
|
||||
|
||||
return static_cast<Register>(source);
|
||||
}
|
||||
|
||||
QString Codes::description(Register code) {
|
||||
return registerErrors[(uint8_t)code];
|
||||
}
|
22
API/codes.h
Normal file
22
API/codes.h
Normal file
|
@ -0,0 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
|
||||
namespace Codes {
|
||||
enum class Register {
|
||||
success,
|
||||
noLogin,
|
||||
emptyLogin,
|
||||
loginExists,
|
||||
loginPolicyViolation,
|
||||
noPassword,
|
||||
emptyPassword,
|
||||
passwordPolicyViolation,
|
||||
unknownError
|
||||
};
|
||||
|
||||
Register convertRegister (int source);
|
||||
|
||||
QString description (Register code);
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue