From ec702d93c567ec03f75508460f4b9fd46a5c061f Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Fri, 1 May 2020 23:57:47 +0200 Subject: [PATCH] Add sample user controller with sign-up and name of current user --- .../geotime/security/SecurityConstants.java | 2 +- .../de/hft/geotime/user/UserController.java | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 backend/src/main/java/de/hft/geotime/user/UserController.java diff --git a/backend/src/main/java/de/hft/geotime/security/SecurityConstants.java b/backend/src/main/java/de/hft/geotime/security/SecurityConstants.java index 27ac9e8..d568748 100644 --- a/backend/src/main/java/de/hft/geotime/security/SecurityConstants.java +++ b/backend/src/main/java/de/hft/geotime/security/SecurityConstants.java @@ -5,5 +5,5 @@ public class SecurityConstants { public static final long EXPIRATION_TIME = 864_000_000; // 10 days public static final String TOKEN_PREFIX = "Bearer "; public static final String HEADER_STRING = "Authorization"; - public static final String SIGN_UP_URL = "/users/sign-up"; + public static final String SIGN_UP_URL = "/user/sign-up"; } \ No newline at end of file diff --git a/backend/src/main/java/de/hft/geotime/user/UserController.java b/backend/src/main/java/de/hft/geotime/user/UserController.java new file mode 100644 index 0000000..5d73f85 --- /dev/null +++ b/backend/src/main/java/de/hft/geotime/user/UserController.java @@ -0,0 +1,35 @@ +package de.hft.geotime.user; + +import org.springframework.security.core.Authentication; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; + +@RestController +@RequestMapping("/user") +public class UserController { + + private TimetrackUserRepository userRepository; + private BCryptPasswordEncoder bCryptPasswordEncoder; + + public UserController(TimetrackUserRepository userRepository, BCryptPasswordEncoder bCryptPasswordEncoder) { + this.userRepository = userRepository; + this.bCryptPasswordEncoder = bCryptPasswordEncoder; + } + + @GetMapping + public String getUsername(Authentication authentication) { + TimetrackUser timetrackUser = userRepository.findFirstByUsername(authentication.getName()); + return "Welcome back " + timetrackUser.getFirstname() + " " + timetrackUser.getLastname(); + } + + // TODO: implement register, maybe move to another class + @PostMapping("/sign-up") + public HashMap signUp(@RequestBody HashMap payload) { + return payload; + // user.setPassword(bCryptPasswordEncoder.encode(user.getPassword())); + // userRepository.save(user); + } + +}