From 24794e2085f59ec70ebeb7ad9b47572c58e4e964 Mon Sep 17 00:00:00 2001 From: Marc Beyer Date: Sat, 5 Feb 2022 23:31:22 +0100 Subject: [PATCH] Create user on first login --- .../vpr/server/controller/UserController.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/server/src/main/java/com/vpr/server/controller/UserController.java b/server/src/main/java/com/vpr/server/controller/UserController.java index 5cb3090..fbd9267 100644 --- a/server/src/main/java/com/vpr/server/controller/UserController.java +++ b/server/src/main/java/com/vpr/server/controller/UserController.java @@ -84,6 +84,27 @@ public class UserController { @RequestParam String login, @RequestParam String password ) { + if(userRepository.findAllUsernames().length == 0){ + byte[] salt = Hasher.GenerateSalt(); + byte[] hash; + try { + hash = Hasher.HashPassword(password, salt); + } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { + e.printStackTrace(); + return new ResponseEntity<>("Fehler beim hashen", HttpStatus.INTERNAL_SERVER_ERROR); + } + + User user = new User(); + user.setName("Admin"); + user.setForename(login); + user.setLogin(login); + user.setPassword(hash); + user.setSalt(salt); + user.setToken(""); + user.setAdmin(true); + + userRepository.save(user); + } System.out.println(login + " tries to login."); User user = userRepository.findByLogin(login); if (user == null) {