encoding the password before saving it

oups I Forgor
This commit is contained in:
Bartha Maxime 2024-03-12 22:35:25 +01:00
parent 5c728098df
commit dae59f67ce
2 changed files with 10 additions and 12 deletions

View File

@ -41,10 +41,10 @@ public class MockController {
@PostMapping("/mock") @PostMapping("/mock")
public void postMock(){ public void postMock(){
User herobrine = new User("brine","hero","admin@admin.com","in your WalLs","ShadowsLand",new Date(0), "none",Role.Admin,passwordEncoder.encode("admin")); User herobrine = new User("brine","hero","admin@admin.com","in your WalLs","ShadowsLand",new Date(0), "none",Role.Admin,"admin");
User joe = new User("Mama","Joe","student@student.com","roundabout","DaWarudo",new Date(0), "None",Role.Student,passwordEncoder.encode("student")); User joe = new User("Mama","Joe","student@student.com","roundabout","DaWarudo",new Date(0), "None",Role.Student,"student");
User meh = new User("Inspiration","lackOf","secretary@secretary.com","a Box","the street",new Date(0),"none", Role.Teacher,passwordEncoder.encode("secretary")); User meh = new User("Inspiration","lackOf","secretary@secretary.com","a Box","the street",new Date(0),"none", Role.Teacher,"secretary");
User joke = new User("CthemBalls","Lemme","teacher@teacher.com","lab","faculty",new Date(0), "none",Role.Teacher,passwordEncoder.encode("teacher")); User joke = new User("CthemBalls","Lemme","teacher@teacher.com","lab","faculty",new Date(0), "none",Role.Teacher,"teacher");
mockUsers = new ArrayList<User>(Arrays.asList(herobrine,joe,meh,joke)); mockUsers = new ArrayList<User>(Arrays.asList(herobrine,joe,meh,joke));

View File

@ -5,16 +5,10 @@ import org.springframework.stereotype.Service;
import ovh.herisson.Clyde.Repositories.UserRepository; import ovh.herisson.Clyde.Repositories.UserRepository;
import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.User; import ovh.herisson.Clyde.Tables.User;
import java.util.*;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@Service @Service
public class UserService { public class UserService {
private final UserRepository userRepo; private final UserRepository userRepo;
private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
@ -35,16 +29,20 @@ public class UserService {
} }
public boolean checkPassword(User user, String tryingPassword){ public boolean checkPassword(User user, String tryingPassword){
return passwordEncoder.matches(tryingPassword, user.getPassword()); return passwordEncoder.matches(tryingPassword, user.getPassword());
} }
public void save(User user){ public void save(User user){
user.setPassword(encodePassword(user.getPassword()));
userRepo.save(user); userRepo.save(user);
} }
public Iterable<User> getAll(){ public Iterable<User> getAll(){
return userRepo.findAll(); return userRepo.findAll();
} }
public String encodePassword(String rawPassword){
return passwordEncoder.encode(rawPassword);
}
} }