-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.h
90 lines (76 loc) · 2.16 KB
/
auth.h
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// Structure to store user credentials
struct User {
string name;
string username;
string password;
};
// Function to sign up a new user
void signUp(fstream& file) {
User user;
// Get user input for name, username, and password
cout << "Enter name: ";
cin >> user.name;
cout << "Enter username: ";
cin >> user.username;
cout << "Enter password: ";
cin >> user.password;
// Write the user data to the file
file << user.name << "," << user.username << "," << user.password << endl;
}
// Function to log in an existing user
bool logIn(fstream& file, string &user) {
string username, password;
// Get user input for username and password
cout << "Enter username: ";
cin >> username;
cout << "Enter password: ";
cin >> password;
// Read the file line by line
string line;
while (getline(file, line)) {
// Extract the name, username, and password from the line
int i = line.find(',');
string name = line.substr(0, i);
int j = line.find(',', i + 1);
string fileUsername = line.substr(i + 1, j - i - 1);
string filePassword = line.substr(j + 1);
// Check if the entered username and password match the ones in the file
if (username == fileUsername && password == filePassword) {
cout << "Logged in successfully as " << name << "." << endl;
user = name;
return true;
}
}
// If we reach here, the username and password did not match any in the file
cout << "Failed to log in. Invalid username or password." << endl;
return false;
}
bool authMain(string &user) {
// Open the file in append mode
fstream file("credentials.txt", ios::app);
// Sign up or log in
cout << "1. Sign up" << endl;
cout << "2. Log in" << endl;
cout << "Enter your choice: ";
int choice;
cin >> choice;
switch (choice) {
case 1:
signUp(file);
cout << "Successfully Signed Up!" << endl;
break;
case 2:
// Open the file in input mode
file.close();
file.open("credentials.txt", ios::in);
return logIn(file, user);
break;
}
// Close the file
file.close();
return false;
}