Skip to content
Snippets Groups Projects
Commit acb92219 authored by harryTheWizzard's avatar harryTheWizzard
Browse files

added register user endpoint

parent 93473f73
No related branches found
No related tags found
No related merge requests found
......@@ -33,7 +33,8 @@ public class SecurityConfig {
.cors().and()
.csrf().disable()
.authorizeHttpRequests(auth-> auth
.requestMatchers(HttpMethod.POST, "/auth/**").permitAll()
.requestMatchers(HttpMethod.POST, "api/auth/**").permitAll()
.requestMatchers(HttpMethod.POST, "api/user/**").permitAll()
.anyRequest().authenticated()
)
.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt)
......
package ntnu.idatt2016.v233.SmartMat.controller;
import lombok.AllArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.entity.request.RegisterUser;
import ntnu.idatt2016.v233.SmartMat.entity.user.Authority;
import ntnu.idatt2016.v233.SmartMat.entity.user.User;
import ntnu.idatt2016.v233.SmartMat.service.user.UserService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@AllArgsConstructor
@RestController
@RequestMapping("/api/user")
public class UserController {
UserService userService;
PasswordEncoder passwordEncoder;
/**
* Use this JSON format:
* {
* "username":"kari123",
* "password":"sjokoladekake",
* "email":"kari.nordman@gmail.com",
* "firstName":"kari",
* "lastName":"nordmann",
* "birthDate":"2001-12-12"
* }
*
* create a new user in the database
* uses the user service
* @param user The user to be registered.
*/
@PostMapping("/register")
public void register(@RequestBody RegisterUser user) {
if(user.username() == null || user.username().trim().isEmpty() || user.username().length() > 50 ||
user.password() == null || user.password().trim().isEmpty() || user.password().length() > 50 ||
user.email() == null || user.email().trim().isEmpty() || user.email().length() > 50 ||
user.firstName() == null || user.firstName().trim().isEmpty() || user.firstName().length() > 50 ||
user.lastName() == null || user.lastName().trim().isEmpty() || user.lastName().length() > 50 ||
user.birthDate() == null) {
return;
}
if(userService.getUserFromUsername(user.username()).isPresent()) {
return;
}
User newUser = User.builder()
.authority(Authority.USER)
.username(user.username())
.password(passwordEncoder.encode(user.password()))
.email(user.email())
.firstName(user.firstName())
.lastName(user.lastName())
.dateOfBirth(user.birthDate())
.build();
userService.saveUser(newUser);
}
}
package ntnu.idatt2016.v233.SmartMat.entity.request;
import java.sql.Date;
/**
* RegisterUser is a record class representing a register request.
* @param username the username of the user
* @param password the password of the user
* @param email the email of the user
*
*/
public record RegisterUser (String username, String password, String email, String firstName, String lastName,
Date birthDate){
}
......@@ -9,6 +9,7 @@ import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.sql.Date;
import java.util.Collection;
import java.util.List;
......@@ -27,13 +28,25 @@ import java.util.List;
@Builder
public class User implements UserDetails {
@Id
@Column(name = "user_name")
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
@Column(name = "enabled")
private boolean enabled;
@Column
private String email;
@Column
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "birthdate")
private Date dateOfBirth;
@Enumerated(EnumType.STRING)
private Authority authority;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment