-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthenticationController.java
More file actions
49 lines (39 loc) · 1.64 KB
/
AuthenticationController.java
File metadata and controls
49 lines (39 loc) · 1.64 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
package controllers;
import interfaces.IAuthenticationService;
import models.User;
import java.util.HashMap;
import java.util.Map;
// This class handles the logic for Registration and Login
public class AuthenticationController implements IAuthenticationService {
// Simulating a Database using a Map (Email -> User Object)
private Map<String, User> usersDatabase = new HashMap<>();
@Override
public User registerUser(String studentId, String email, String password, String fullName) {
// 1. Check if email already exists
if(usersDatabase.containsKey(email)) {
System.out.println("Error: Email already exists.");
return null;
}
// 2. Encrypt Password (Simple simulation)
String encryptedPass = "HASHED_" + password;
// 3. Create new User object
User newUser = new User(studentId, email, encryptedPass, fullName);
// 4. Save to our "Database"
usersDatabase.put(email, newUser);
System.out.println("Success: User registered successfully!");
return newUser;
}
@Override
public User login(String email, String password) {
// 1. Find user by email
User user = usersDatabase.get(email);
// 2. Validate password
if (user != null && user.getPasswordHash().equals("HASHED_" + password)) {
System.out.println("Login Successful! Welcome " + user.getFullName());
return user;
} else {
System.out.println("Error: Invalid Email or Password.");
return null;
}
}
}