Banking_System_Server  1.0.0
Qt-based banking app for user/admin account management, transactions, secure server communication via PostgreSQL/Supabase.
LoginRequest.h
Go to the documentation of this file.
1 
8 #ifndef LOGINREQUEST_H
9 #define LOGINREQUEST_H
10 
11 #include "Request.h"
12 #include "db.h"
13 
19 class LoginRequest : public Request
20 {
21 private:
22  DB::DatabaseManager* dbManager = nullptr;
23 
24 public:
30  LoginRequest() : dbManager(DB::DatabaseManager::createInstance())
31  {
32  // Log to database log table (if needed)
33  }
34 
45  QJsonObject execute(const QJsonObject& jsonObj, QMutex& m) override
46  {
47  QMutexLocker locker(&m); // Lock the mutex for the duration of this function
48 
49  QString email;
50  QString password;
51 
52  QJsonObject response;
53  QJsonObject data;
54 
55  response.insert("Response", 1);
56 
57  // Extract the data array
58  if (jsonObj.contains("Data"))
59  {
60  QJsonObject dataObj = jsonObj["Data"].toObject();
61 
62  if (dataObj.contains("email"))
63  {
64  email = dataObj.value("email").toString();
65  }
66 
67  if (dataObj.contains("password"))
68  {
69  password = dataObj.value("password").toString();
70  }
71  }
72  else
73  {
74  qCritical() << "Data not found";
75  }
76 
77  do
78  {
79  if (!isDBConnectionValid(dbManager))
80  {
81  return CreateDBConnectionError(response, data);
82  }
83 
84  DB::DbResult result = dbManager->select("*")->table("users")->where("email =", email)->exec();
85  int user_id = result.first().value("id").toInt();
86  QJsonObject obj = result.first();
87 
88  if (result.isEmpty())
89  {
90  return CreateErrorResponse(response, data, "email not found");
91  }
92 
93  result = dbManager->select("password")->table("users")->where("id =", user_id)->exec();
94  if (result.first().value("password").toString() != password)
95  {
96  return CreateErrorResponse(response, data, "Invalid password");
97  }
98 
99  data.insert("status", int(true));
100  data.insert("message", "Login successful");
101  data.insert("first_name", obj.value("first_name").toString());
102  data.insert("role", obj.value("role").toString());
103 
104  response.insert("Data", data);
105  } while (false);
106 
107  // Convert response to JSON
108  QJsonDocument responseDoc(response);
109  QByteArray responseData = responseDoc.toJson();
110 
111  // Send response
112  qDebug().noquote() << "<-- Login::Response :\n" << responseDoc.toJson(QJsonDocument::Indented);
113 
114  return response;
115  }
116 };
117 
118 #endif // LOGINREQUEST_H
This file contains the declaration of the Request class, which is an abstract base class for handling...
Manages database connections and SQL operations.
Definition: db.h:80
DatabaseManager * table(const QString &value)
Sets the table for the query.
Definition: db.cpp:115
DatabaseManager * where(const QString &value, const QVariant &val=QVariant())
Adds a WHERE clause to the query.
Definition: db.cpp:121
DbResult exec()
Executes the built query.
Definition: db.cpp:266
DatabaseManager * select(const QString &value)
Selects columns for the query.
Definition: db.cpp:107
The DbResult class represents a result set returned from a database query.
Definition: dbresult.h:24
QJsonObject first() const
Retrieves the first item in the result set.
Definition: dbresult.cpp:25
bool isEmpty() const
Checks if the result set is empty.
Definition: dbresult.cpp:20
The LoginRequest class handles user login requests.
Definition: LoginRequest.h:20
LoginRequest()
Constructor for the LoginRequest class.
Definition: LoginRequest.h:30
QJsonObject execute(const QJsonObject &jsonObj, QMutex &m) override
Executes the login request.
Definition: LoginRequest.h:45
The Request class is an abstract base class for handling different types of requests.
Definition: Request.h:25
QJsonObject CreateErrorResponse(QJsonObject &response, QJsonObject &dataObj, QString message)
Creates a generic error JSON response.
Definition: Request.h:90
QJsonObject CreateDBConnectionError(QJsonObject &response, QJsonObject &dataObj)
Creates a JSON response indicating a database connection error.
Definition: Request.h:65
bool isDBConnectionValid(DB::DatabaseManager *dbManager)
Checks if the database connection is valid.
Definition: Request.h:36
Database management classes for handling database connections and operations.