Home > Net >  Integrating QML and C for login purposes
Integrating QML and C for login purposes

Time:12-07

I'm trying to create a simple login app, which will authenticate via Google Firebase. So far, I have the authentication code working fine. Header file:

#ifndef AUTHHANDLER_H
#define AUTHHANDLER_H

#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QJsonDocument>

class AuthHandler : public QObject
{
    Q_OBJECT
public:
    explicit AuthHandler(QObject *parent = nullptr);
    ~AuthHandler();
    void setAPIKey(const QString & apiKey);
    void signUserUp(const QString & emailAddress, const QString & password);
    void signUserIn(const QString & emailAddress, const QString & password);

public slots:
    void networkReplyReadyRead();

    void performAuthenticatedDatabaseCall();

signals:
    void userSignedIn();

private:
   void performPOST( const QString & url, const QJsonDocument & payload);
   void parseResponse( const QByteArray & response);
   QString m_apiKey;
   QNetworkAccessManager * m_networkAccessManager;
   QNetworkReply * m_networkReply;
   QString m_idToken;


};

#endif // AUTHHANDLER_H

Authentication .cpp file

#include "authhandler.h"
#include <QDebug>
#include <QVariantMap>
#include <QNetworkRequest>
#include <QJsonObject>

AuthHandler::AuthHandler(QObject *parent)
    : QObject{parent}
    , m_apiKey( QString() )
{
    m_networkAccessManager = new QNetworkAccessManager( this );
    connect( this, &AuthHandler::userSignedIn, this, &AuthHandler::performAuthenticatedDatabaseCall );
}

AuthHandler::~AuthHandler()
{
   m_networkAccessManager->deleteLater();
}

void AuthHandler::setAPIKey(const QString &apiKey)
{
    m_apiKey = apiKey;
}

void AuthHandler::signUserUp(const QString &emailAddress, const QString &password)
{
    QString signUpEndpoint = "https://identitytoolkit.googleapis.com/v1/accounts:signUp?key="   m_apiKey;

    QVariantMap variantPayload;
    variantPayload["email"] = emailAddress;
    variantPayload["password"] = password;
    variantPayload["returnSecureToken"] = true;

    QJsonDocument jsonPayload = QJsonDocument::fromVariant( variantPayload );
    performPOST( signUpEndpoint, jsonPayload);

}

void AuthHandler::signUserIn(const QString &emailAddress, const QString &password)
{
    QString signInEndpoint = "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key="   m_apiKey;

    QVariantMap variantPayload;
    variantPayload["email"] = emailAddress;
    variantPayload["password"] = password;
    variantPayload["returnSecureToken"] = true;

    QJsonDocument jsonPayload = QJsonDocument::fromVariant( variantPayload );
    performPOST( signInEndpoint, jsonPayload);

}

main.cpp file

#include <QCoreApplication>
#include "authhandler.h"

#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    //QCoreApplication a(argc, argv);

   // AuthHandler authHandler;
    //authHandler.setAPIKey("AIzaSyB-nAN8OlgxHzXC5gpISkodjEZJ7IdSpgI");
   // authHandler.signUserIn("[email protected]", "password123!");



    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    AuthHandler authHandler;
    authHandler.setAPIKey("AIzaSyB-nAN8OlgxHzXC5gpISkodjEZJ7IdSpgI");
    authHandler.signUserIn("[email protected]", "password123!");



    return app.exec();
}

As you can see, I'm currently hardcoding the values used in the SignUserIn function. These values are then updated in Firebase, and this is working. My question is, how do I make it so that the hard-coded values are input by an user in a qml file? I created a qml file with the required layout, but I'm having trouble setting it up so that the .cpp file function executes when a button is clicked in qml.

CodePudding user response:

  1. Make the method signUserIn a slot in the AuthHandler class

    public slots: 
        void signUserIn(const QString & emailAddress, const QString & password);
    
  2. Register your AuthHandler to the QML engine in main.cpp

    qmlRegisterSingletonInstance("AuthHandler", 1, 0, "AuthHandler", &authHandler);
    
  3. Import the AuthHandler in QML

    import AuthHandler
    
  4. Send the user credentials from QML to your handler

         TextEdit {
             id: user
             text: "user"
         }
    
         TextEdit {
             id: password
             text: "password"
         }
    
         Button {
             text: qsTr("Login");
             onClicked: {
                 console.log("login: "   user.text   " password: "   password.text);
                 AuthHandler.signUserIn(user.text, password.text);
             }
         }
    
  • Related