-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthManager.cpp
More file actions
65 lines (60 loc) · 2.12 KB
/
Copy pathAuthManager.cpp
File metadata and controls
65 lines (60 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "AuthManager.hpp"
#include "User.h"
#include "Utils.h"
#include "Utils.cpp"
#include <fstream>
#include <filesystem>
#include <cstring>
using namespace std;
namespace fs = std::filesystem;
// Handles user login
bool AuthManager::login(string& username) {
Utils::printBlue("\n-- Login --\n");
string uname, pwd;
cout << "Username: "; getline(cin, uname);
cout << "Password: "; getline(cin, pwd);
ifstream infile("users.dat", ios::binary);
char unameBuf[50], pwdBuf[50];
// Read each user record from file
while (infile.read(unameBuf, sizeof(unameBuf))) {
infile.read(pwdBuf, sizeof(pwdBuf));
// Compare input with stored credentials
if (uname == unameBuf && pwd == pwdBuf) {
Utils::printGreen("\n-|#| Login successful!\n");
username = uname;
fs::create_directories("UserNotes/" + uname);
filesystem::create_directories("UserNotes/" + uname);
return true;
}
else {
Utils::printRed("\n-|X| Invalid username or password.\n");
}
}
return false; // Login failed
};
// Handles user signup
void AuthManager::signup() {
Utils::printBlue("\n-- Signup --\n");
string uname, pwd;
cout << "Choose username: "; getline(cin, uname);
cout << "Choose password: "; getline(cin, pwd);
ifstream infile("users.dat", ios::binary);
char unameBuf[50];
// Check if username already exists
while (infile.read(unameBuf, sizeof(unameBuf))) {
infile.ignore(50); // Skip password
if (uname == unameBuf) {
Utils::printYellow("\n-|!| Username already exists.\n");
return;
}
}
infile.close();
// Add new user to file
ofstream outfile("users.dat", ios::app | ios::binary);
char unameArray[50] = {}, pwdArray[50] = {};
strncpy(unameArray, uname.c_str(), 49);
strncpy(pwdArray, pwd.c_str(), 49);
outfile.write(unameArray, sizeof(unameArray));
outfile.write(pwdArray, sizeof(pwdArray));
Utils::printGreen("\n-|#| Signup successful. Please login.\n");
}