forked from saurg/2048-multiplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword_authentication.cpp
More file actions
143 lines (101 loc) · 2.57 KB
/
Copy pathpassword_authentication.cpp
File metadata and controls
143 lines (101 loc) · 2.57 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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_map>
#include <assert.h>
using namespace std;
typedef unordered_map<string,string> stringmap;
//This function takes string and return its hash value
size_t create_hash(string password)
{
stringmap mymap;
stringmap::hasher fn = mymap.hash_function();
return fn(password);
}
//this function will return true if user is already registered
int is_user_registered(string username,string password)
{
string user_name,pass_word;
ifstream file;
file.open("login.txt",ios::in);
while(!file.eof())
{
file >> user_name >> pass_word;
if(user_name == username)
{
cout <<"\nUsername already exists\nEnter different username\n";
return true;
}
else
continue;
}
return false;
}
// this function registers new user and add its details to file
void register_user(string username, string password){
ofstream file;
// opening file login.txt in append mode
file.open("login.txt",ios::app);
//assert(file != NULL);
file << username << " "<< create_hash(password) << endl;
file.close();
}
// this function checks for validity of username and password the player entered
bool check_if_valid(string username, string password)
{
string user_name ;
size_t pass_word;
ifstream file;
file.open("login.txt",ios::in);
while(!file.eof())
{
file >> user_name >> pass_word;
if(user_name == username && pass_word == create_hash(password))
{
cout << "\nusername and Password validated\nLogging U in the game\n";
return true;
}
else
continue;
}
file.close();
cout << "\nThe username or password u entered is not valid\n";
return false;
}
void login()
{
string password,username;
// User getline so that even spaces can be included while entering username and password
// But there should not be any spaces in username
char option ;
cout << "New?? (y/n)\n";
option = getchar();
switch(option)
{
case 'y': do{
cout << "Username: ";
cin >> username;
cout << "Password: ";
cin >> password;
}while(is_user_registered(username, password));
register_user(username,password);
cout << "\nU r registered\n********** Enjoy the game :D **********\n";
// enter here the function to start the game
break;
case 'n':
do{
cout << "Username:";
cin >> username;
cout << "Password:";
cin >> password;
}while(!check_if_valid(username,password));
break;
default: cout << "Oops!!! U pressed wrong key\n";
break;
}
}
int main()
{
login();
return 0;
}