Skip to content

Commit c264d0d

Browse files
committed
validation for login window
1 parent 43b47db commit c264d0d

3 files changed

Lines changed: 30 additions & 19 deletions

File tree

desktop-agent/src/main.cpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -231,30 +231,29 @@ ShowWindow(GetConsoleWindow(), SW_HIDE);
231231
}
232232

233233

234-
if (!loggedIn) {
234+
if (!loggedIn) {
235235
LoginWindow loginGui;
236-
if (loginGui.show()) {
237-
std::string enteredUser = loginGui.getUsername();
238-
std::string enteredPass = loginGui.getPassword();
239-
backendClient.setUsername(enteredUser);
240-
backendClient.setPassword(enteredPass);
241-
242-
if (backendClient.login()) {
243-
PasswordManager::storePassword(enteredUser, enteredPass);
244-
config.setUsername(enteredUser);
245-
config.save("config.json");
246-
loggedIn = true;
236+
while (!loggedIn) { // Loop until authenticated or exited
237+
if (loginGui.show()) {
238+
backendClient.setUsername(loginGui.getUsername());
239+
backendClient.setPassword(loginGui.getPassword());
240+
241+
if (backendClient.login()) {
242+
PasswordManager::storePassword(loginGui.getUsername(), loginGui.getPassword());
243+
config.setUsername(loginGui.getUsername());
244+
config.save("config.json");
245+
loggedIn = true; // This breaks the while loop
246+
} else {
247+
loginGui.setErrorMessage("Invalid credentials. Please try again.");
248+
}
247249
} else {
248-
// If the server rejects the login, we stop here.
249-
return 1;
250-
}
251-
} else {
252-
return 0;
250+
// If the user closes the window manually (X or Cancel)
251+
return 0;
252+
}
253253
}
254254
}
255255

256256

257-
258257
// Initialize camera
259258
CameraCapture camera(config.getCameraIndex());
260259
if (!camera.initialize()){

desktop-agent/src/ui/LoginWindow.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,22 @@ bool LoginWindow::show(){
127127
ImGuiInputTextFlags_Password);
128128
ImGui::PopItemWidth();
129129

130+
// Error message if credentials are wrong
131+
if(!m_errorMessage.empty()){
132+
ImGui::Dummy(ImVec2(0,5));
133+
ImGui::PushStyleColor(ImGuiCol_Text,ImVec4(1.0f,0.3f,0.3f,1.0f));
134+
ImGui::TextWrapped("%s",m_errorMessage.c_str());
135+
ImGui::PopStyleColor();
136+
}
137+
130138
ImGui::Dummy(ImVec2(0, 25));
131139

132140

133141
// Login button
134142
if(ImGui::Button("Sign In", ImVec2(-1, 55))){
135143
m_username = m_userBuf;
136144
m_password = m_passBuf;
145+
m_errorMessage ="";
137146
ImGui::End();
138147
ImGui::Render();
139148
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

desktop-agent/src/ui/LoginWindow.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class LoginWindow{
1414

1515
std::string getUsername() const {return m_username; }
1616
std::string getPassword() const {return m_password;}
17-
17+
18+
void setErrorMessage(const std::string& message){m_errorMessage = message;}
19+
1820

1921

2022
private:
@@ -23,6 +25,7 @@ class LoginWindow{
2325
char m_passBuf[64] = "";
2426
std::string m_username;
2527
std::string m_password;
28+
std::string m_errorMessage;
2629
bool m_loginTriggered = false;
2730

2831

0 commit comments

Comments
 (0)