Skip to content
Snippets Groups Projects
Commit 1f5549f8 authored by Torbjørn Antonsen's avatar Torbjørn Antonsen
Browse files

Added logger to AuthenticationController and bean in...

Added logger to AuthenticationController and bean in FullstackProsjektApplication that adds admin user to database
parent cf4f3908
Branches
No related tags found
1 merge request!10refactored backend to its own directory to separate front end and backend code
Pipeline #267458 passed
package edu.ntnu.idatt2105;
import edu.ntnu.idatt2105.model.Role;
import edu.ntnu.idatt2105.model.User;
import edu.ntnu.idatt2105.repository.RoleRepository;
import edu.ntnu.idatt2105.repository.UserRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.CrossOrigin;
import java.util.HashSet;
import java.util.Set;
@SpringBootApplication
@CrossOrigin(origins = "http://localhost:5173")
public class FullstackProsjektApplication {
public static void main(String[] args) {
SpringApplication.run(FullstackProsjektApplication.class, args);
}
@Bean
CommandLineRunner run(RoleRepository roleRepository, UserRepository userRepository, PasswordEncoder encoder) {
return args -> {
// Exit early if DB already contains the admin user
if (roleRepository.findByAuthority("ADMIN").isPresent()) return;
Role adminRole = roleRepository.save(new Role("ADMIN"));
roleRepository.save(new Role("USER"));
Set<Role> roles = new HashSet<>();
roles.add(adminRole);
User admin = new User(2, "potetmos", encoder.encode("potetmos"), roles);
userRepository.save(admin);
};
}
}
\ No newline at end of file
......@@ -6,15 +6,19 @@ import edu.ntnu.idatt2105.dto.TokenDTO;
import edu.ntnu.idatt2105.dto.UserRegistrationDTO;
import edu.ntnu.idatt2105.model.User;
import edu.ntnu.idatt2105.service.AuthenticationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/auth")
@CrossOrigin(origins = "http://localhost:5173")
public class AuthenticationController {
private final AuthenticationService authService;
private static final Logger logger = LoggerFactory.getLogger(AuthenticationController.class);
public AuthenticationController(AuthenticationService authService) {
this.authService = authService;
......@@ -28,6 +32,7 @@ public class AuthenticationController {
@PostMapping("/login")
public LoginResponseDTO loginUser(@RequestBody UserRegistrationDTO registrationDTO) {
logger.info("Logging in with user " + registrationDTO.toString());
return authService.loginUser(registrationDTO.getName(), registrationDTO.getPassword());
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment