Skip to content

Commit 87eb154

Browse files
committed
Fix authentication, add registration page
1 parent 53460f5 commit 87eb154

File tree

6 files changed

+330
-44
lines changed

6 files changed

+330
-44
lines changed
Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
11
package net.hackyourfuture.coursehub.data;
22

3+
import org.springframework.security.core.CredentialsContainer;
34
import org.springframework.security.core.GrantedAuthority;
45
import org.springframework.security.core.authority.SimpleGrantedAuthority;
56
import org.springframework.security.core.userdetails.UserDetails;
67

78
import java.util.Collection;
89
import java.util.List;
910

10-
public record AuthenticatedUser(Integer userId, String firstName, String lastName, String emailAddress, Role role) implements UserDetails {
11+
public final class AuthenticatedUser implements UserDetails,
12+
CredentialsContainer {
13+
private final Integer userId;
14+
private final String firstName;
15+
private final String lastName;
16+
private final String emailAddress;
17+
private final Role role;
18+
private String passwordHash;
19+
20+
public AuthenticatedUser(
21+
Integer userId,
22+
String firstName,
23+
String lastName,
24+
String emailAddress,
25+
String passwordHash,
26+
Role role
27+
) {
28+
this.userId = userId;
29+
this.firstName = firstName;
30+
this.lastName = lastName;
31+
this.emailAddress = emailAddress;
32+
this.passwordHash = passwordHash;
33+
this.role = role;
34+
}
1135

1236
@Override
1337
public String getUsername() {
@@ -16,11 +40,36 @@ public String getUsername() {
1640

1741
@Override
1842
public String getPassword() {
19-
return null;
43+
return passwordHash;
2044
}
2145

2246
@Override
2347
public Collection<? extends GrantedAuthority> getAuthorities() {
2448
return List.of(new SimpleGrantedAuthority("ROLE_" + role.name()));
2549
}
50+
51+
@Override
52+
public void eraseCredentials() {
53+
passwordHash = null;
54+
}
55+
56+
public Integer getUserId() {
57+
return userId;
58+
}
59+
60+
public String getFirstName() {
61+
return firstName;
62+
}
63+
64+
public String getLastName() {
65+
return lastName;
66+
}
67+
68+
public String getEmailAddress() {
69+
return emailAddress;
70+
}
71+
72+
public Role getRole() {
73+
return role;
74+
}
2675
}

src/main/java/net/hackyourfuture/coursehub/service/UserAuthenticationService.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,26 @@
1010
import org.springframework.security.core.context.SecurityContextHolder;
1111
import org.springframework.security.core.userdetails.UserDetailsService;
1212
import org.springframework.security.core.userdetails.UsernameNotFoundException;
13+
import org.springframework.security.crypto.password.PasswordEncoder;
1314
import org.springframework.stereotype.Service;
1415

1516
@Service
1617
public class UserAuthenticationService implements UserDetailsService {
1718
private final UserAccountRepository userAccountRepository;
1819
private final StudentRepository studentRepository;
1920
private final InstructorRepository instructorRepository;
21+
private final PasswordEncoder passwordEncoder;
2022

2123
public UserAuthenticationService(
2224
UserAccountRepository userAccountRepository,
2325
StudentRepository studentRepository,
24-
InstructorRepository instructorRepository
26+
InstructorRepository instructorRepository,
27+
PasswordEncoder passwordEncoder
2528
) {
2629
this.userAccountRepository = userAccountRepository;
2730
this.studentRepository = studentRepository;
2831
this.instructorRepository = instructorRepository;
32+
this.passwordEncoder = passwordEncoder;
2933
}
3034

3135
@Override
@@ -38,11 +42,11 @@ public AuthenticatedUser loadUserByUsername(String emailAddress) throws Username
3842
return switch (user.role()) {
3943
case student -> {
4044
StudentEntity student = studentRepository.findById(user.userId());
41-
yield new AuthenticatedUser(user.userId(), student.firstName(), student.lastName(), user.emailAddress(), user.role());
45+
yield new AuthenticatedUser(user.userId(), student.firstName(), student.lastName(), user.emailAddress(), user.passwordHash(), user.role());
4246
}
4347
case instructor -> {
4448
InstructorEntity instructor = instructorRepository.findById(user.userId());
45-
yield new AuthenticatedUser(user.userId(), instructor.firstName(), instructor.lastName(), user.emailAddress(), user.role());
49+
yield new AuthenticatedUser(user.userId(), instructor.firstName(), instructor.lastName(), user.emailAddress(), user.passwordHash(), user.role());
4650
}
4751
};
4852
}
@@ -62,6 +66,7 @@ public void register(String firstName, String lastName, String emailAddress, Str
6266
// For simplicity, we will register every new user as a student
6367
// In a real-world application, you might want to allow registering as an instructor as well
6468
// and have an admin approve instructor accounts before they can log in
65-
studentRepository.insertStudent(firstName, lastName, emailAddress, password);
69+
var passwordHash = passwordEncoder.encode(password);
70+
studentRepository.insertStudent(firstName, lastName, emailAddress, passwordHash);
6671
}
6772
}

src/main/java/net/hackyourfuture/coursehub/web/UserAuthenticationController.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.hackyourfuture.coursehub.web;
22

33
import jakarta.servlet.http.HttpServletRequest;
4-
import net.hackyourfuture.coursehub.repository.InstructorRepository;
54
import net.hackyourfuture.coursehub.repository.StudentRepository;
65
import net.hackyourfuture.coursehub.service.UserAuthenticationService;
76
import net.hackyourfuture.coursehub.web.model.HttpErrorResponse;
@@ -88,11 +87,11 @@ private LoginSuccessResponse authenticate(HttpServletRequest httpRequest, String
8887
// Retrieve the corresponding user data to return in a login response
8988
var authenticatedUser = userAuthenticationService.currentAuthenticatedUser();
9089
return new LoginSuccessResponse(
91-
authenticatedUser.userId(),
92-
authenticatedUser.firstName(),
93-
authenticatedUser.lastName(),
94-
authenticatedUser.emailAddress(),
95-
authenticatedUser.role()
90+
authenticatedUser.getUserId(),
91+
authenticatedUser.getFirstName(),
92+
authenticatedUser.getLastName(),
93+
authenticatedUser.getEmailAddress(),
94+
authenticatedUser.getRole()
9695
);
9796
}
9897
}

0 commit comments

Comments
 (0)