From 6663119c366ceb0fcd388aafa8b886be0e7e0fa5 Mon Sep 17 00:00:00 2001 From: JonasEbbinghaus25 Date: Wed, 12 Jun 2024 19:29:35 +0200 Subject: [PATCH 01/10] register --- .../dhbwhub/controller/AuthController.java | 2 +- .../dhbwhub/payload/request/LoginRequest.java | 8 +++++++ src/main/web/src/services/AuthService.tsx | 23 ++++++++++++++----- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/tinf22b6/dhbwhub/controller/AuthController.java b/src/main/java/de/tinf22b6/dhbwhub/controller/AuthController.java index e13121cd..6d26b5ef 100644 --- a/src/main/java/de/tinf22b6/dhbwhub/controller/AuthController.java +++ b/src/main/java/de/tinf22b6/dhbwhub/controller/AuthController.java @@ -102,7 +102,7 @@ public ResponseEntity signup(@Valid @RequestBody SignupRequest signupRequest) userRepository.save(newUser); - return ResponseEntity.ok(new MessageResponse("User registered successfully!")); + return login(new LoginRequest(newAccount.getUsername(), newAccount.getPassword(), false)); } @PostMapping("email-verification") diff --git a/src/main/java/de/tinf22b6/dhbwhub/payload/request/LoginRequest.java b/src/main/java/de/tinf22b6/dhbwhub/payload/request/LoginRequest.java index 02d85b41..046580c8 100644 --- a/src/main/java/de/tinf22b6/dhbwhub/payload/request/LoginRequest.java +++ b/src/main/java/de/tinf22b6/dhbwhub/payload/request/LoginRequest.java @@ -16,4 +16,12 @@ public class LoginRequest { private Boolean rememberMe; + public LoginRequest() {} + + public LoginRequest(String username, String password, Boolean rememberMe) { + this.username = username; + this.password = password; + this.rememberMe = rememberMe; + } + } diff --git a/src/main/web/src/services/AuthService.tsx b/src/main/web/src/services/AuthService.tsx index cdfcd6ac..d325cf23 100644 --- a/src/main/web/src/services/AuthService.tsx +++ b/src/main/web/src/services/AuthService.tsx @@ -2,12 +2,23 @@ import axios from "axios"; import config from "../config/config"; import {jwtDecode} from "jwt-decode"; -export const register = (username: string, email: string, password: string) => { - return axios.post(config.apiUrl + "api/auth/signup", { - username, - email, - password, - }); +export const register = async (username: string, email: string, password: string): Promise => { + // return axios.post(config.apiUrl + "api/auth/signup", { + // username, + // email, + // password, + // }); + + const response = await axios + .post(config.apiUrl + "api/auth/signup", { + username, + email, + password, + }); + saveUserDataToLocalStorage(response.data); + localStorage.setItem('oAuthUser', 'false'); + + return response.data; }; export const saveUserDataToLocalStorage = (data: { accountId: number; userId: number; username: string; accessToken: string; }): void => { From 79d957ba204f9c287cfae6a185b83122ea5da746 Mon Sep 17 00:00:00 2001 From: JonasEbbinghaus25 Date: Wed, 12 Jun 2024 19:31:42 +0200 Subject: [PATCH 02/10] register --- .../java/de/tinf22b6/dhbwhub/controller/AuthController.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tinf22b6/dhbwhub/controller/AuthController.java b/src/main/java/de/tinf22b6/dhbwhub/controller/AuthController.java index 6d26b5ef..d4153740 100644 --- a/src/main/java/de/tinf22b6/dhbwhub/controller/AuthController.java +++ b/src/main/java/de/tinf22b6/dhbwhub/controller/AuthController.java @@ -64,7 +64,10 @@ public class AuthController { @PostMapping("login") public ResponseEntity login(@Valid @RequestBody LoginRequest loginRequest) { + return loginAndCreateJWT(loginRequest); + } + private ResponseEntity loginAndCreateJWT (@Valid @RequestBody LoginRequest loginRequest) { Authentication auth = authenticationManager.authenticate( new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword())); @@ -102,7 +105,7 @@ public ResponseEntity signup(@Valid @RequestBody SignupRequest signupRequest) userRepository.save(newUser); - return login(new LoginRequest(newAccount.getUsername(), newAccount.getPassword(), false)); + return loginAndCreateJWT(new LoginRequest(newAccount.getUsername(), newAccount.getPassword(), false)); } @PostMapping("email-verification") From 4e1676bd2411fe3ac5beb3c57146c32c73f5cde3 Mon Sep 17 00:00:00 2001 From: JonasEbbinghaus25 Date: Wed, 12 Jun 2024 19:40:13 +0200 Subject: [PATCH 03/10] register --- .../java/de/tinf22b6/dhbwhub/controller/AuthController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/de/tinf22b6/dhbwhub/controller/AuthController.java b/src/main/java/de/tinf22b6/dhbwhub/controller/AuthController.java index d4153740..255d5a01 100644 --- a/src/main/java/de/tinf22b6/dhbwhub/controller/AuthController.java +++ b/src/main/java/de/tinf22b6/dhbwhub/controller/AuthController.java @@ -105,7 +105,7 @@ public ResponseEntity signup(@Valid @RequestBody SignupRequest signupRequest) userRepository.save(newUser); - return loginAndCreateJWT(new LoginRequest(newAccount.getUsername(), newAccount.getPassword(), false)); + return loginAndCreateJWT(new LoginRequest(signupRequest.getUsername(), signupRequest.getPassword(), false)); } @PostMapping("email-verification") From fc5d277a2affc0d99026291afa9a2f9b6137c505 Mon Sep 17 00:00:00 2001 From: JonasEbbinghaus25 Date: Wed, 12 Jun 2024 19:44:28 +0200 Subject: [PATCH 04/10] register --- src/main/web/src/services/AuthService.tsx | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main/web/src/services/AuthService.tsx b/src/main/web/src/services/AuthService.tsx index d325cf23..aa95ff39 100644 --- a/src/main/web/src/services/AuthService.tsx +++ b/src/main/web/src/services/AuthService.tsx @@ -3,12 +3,6 @@ import config from "../config/config"; import {jwtDecode} from "jwt-decode"; export const register = async (username: string, email: string, password: string): Promise => { - // return axios.post(config.apiUrl + "api/auth/signup", { - // username, - // email, - // password, - // }); - const response = await axios .post(config.apiUrl + "api/auth/signup", { username, From 3ea988a87c7fc5c3b9c8362909dc5cf75da328ac Mon Sep 17 00:00:00 2001 From: JonasEbbinghaus25 Date: Wed, 12 Jun 2024 19:56:33 +0200 Subject: [PATCH 05/10] register --- src/main/web/src/services/AuthService.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/web/src/services/AuthService.tsx b/src/main/web/src/services/AuthService.tsx index aa95ff39..697b8da8 100644 --- a/src/main/web/src/services/AuthService.tsx +++ b/src/main/web/src/services/AuthService.tsx @@ -16,7 +16,7 @@ export const register = async (username: string, email: string, password: string }; export const saveUserDataToLocalStorage = (data: { accountId: number; userId: number; username: string; accessToken: string; }): void => { - localStorage.setItem('accountId', data.accountId.toString()); + localStorage.setItem('accountId', "" + data.accountId.toString()); localStorage.setItem('userId', data.userId.toString()); localStorage.setItem('username', data.username); localStorage.setItem('token', data.accessToken); From 44775c5dac3849965cfc074a273cab8d8bf98fda Mon Sep 17 00:00:00 2001 From: JonasEbbinghaus25 Date: Wed, 12 Jun 2024 20:01:55 +0200 Subject: [PATCH 06/10] register --- src/main/web/src/services/AuthService.tsx | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/web/src/services/AuthService.tsx b/src/main/web/src/services/AuthService.tsx index 697b8da8..4ff6fccc 100644 --- a/src/main/web/src/services/AuthService.tsx +++ b/src/main/web/src/services/AuthService.tsx @@ -16,10 +16,14 @@ export const register = async (username: string, email: string, password: string }; export const saveUserDataToLocalStorage = (data: { accountId: number; userId: number; username: string; accessToken: string; }): void => { - localStorage.setItem('accountId', "" + data.accountId.toString()); - localStorage.setItem('userId', data.userId.toString()); - localStorage.setItem('username', data.username); - localStorage.setItem('token', data.accessToken); + if (data.accountId !== undefined && data.userId !== undefined && data.username && data.accessToken) { + localStorage.setItem('accountId', data.accountId.toString()); + localStorage.setItem('userId', data.userId.toString()); + localStorage.setItem('username', data.username); + localStorage.setItem('token', data.accessToken); + } else { + console.error('Invalid user data:', data); + } }; export const login = async (username: string, password: string, rememberMe: boolean): Promise => { From 01a906be4bf137103abc3d2921d94dce69f76b5d Mon Sep 17 00:00:00 2001 From: JonasEbbinghaus25 Date: Wed, 12 Jun 2024 20:03:55 +0200 Subject: [PATCH 07/10] register --- src/main/web/src/services/AuthService.tsx | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/web/src/services/AuthService.tsx b/src/main/web/src/services/AuthService.tsx index 4ff6fccc..697b8da8 100644 --- a/src/main/web/src/services/AuthService.tsx +++ b/src/main/web/src/services/AuthService.tsx @@ -16,14 +16,10 @@ export const register = async (username: string, email: string, password: string }; export const saveUserDataToLocalStorage = (data: { accountId: number; userId: number; username: string; accessToken: string; }): void => { - if (data.accountId !== undefined && data.userId !== undefined && data.username && data.accessToken) { - localStorage.setItem('accountId', data.accountId.toString()); - localStorage.setItem('userId', data.userId.toString()); - localStorage.setItem('username', data.username); - localStorage.setItem('token', data.accessToken); - } else { - console.error('Invalid user data:', data); - } + localStorage.setItem('accountId', "" + data.accountId.toString()); + localStorage.setItem('userId', data.userId.toString()); + localStorage.setItem('username', data.username); + localStorage.setItem('token', data.accessToken); }; export const login = async (username: string, password: string, rememberMe: boolean): Promise => { From d7840a03f4779cec7dd93c57ec5fd597199b3c7a Mon Sep 17 00:00:00 2001 From: JonasEbbinghaus25 Date: Wed, 12 Jun 2024 20:04:15 +0200 Subject: [PATCH 08/10] register --- src/main/web/src/services/AuthService.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/web/src/services/AuthService.tsx b/src/main/web/src/services/AuthService.tsx index 697b8da8..aa95ff39 100644 --- a/src/main/web/src/services/AuthService.tsx +++ b/src/main/web/src/services/AuthService.tsx @@ -16,7 +16,7 @@ export const register = async (username: string, email: string, password: string }; export const saveUserDataToLocalStorage = (data: { accountId: number; userId: number; username: string; accessToken: string; }): void => { - localStorage.setItem('accountId', "" + data.accountId.toString()); + localStorage.setItem('accountId', data.accountId.toString()); localStorage.setItem('userId', data.userId.toString()); localStorage.setItem('username', data.username); localStorage.setItem('token', data.accessToken); From d840d6ec9228cf950a34f88feafb54ac4cdb116f Mon Sep 17 00:00:00 2001 From: JonasEbbinghaus25 Date: Wed, 12 Jun 2024 20:07:05 +0200 Subject: [PATCH 09/10] register --- src/main/web/src/services/tests/AuthService.test.tsx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/web/src/services/tests/AuthService.test.tsx b/src/main/web/src/services/tests/AuthService.test.tsx index 7c9ac11c..604b18a2 100644 --- a/src/main/web/src/services/tests/AuthService.test.tsx +++ b/src/main/web/src/services/tests/AuthService.test.tsx @@ -16,12 +16,17 @@ describe('AuthService', () => { const username = 'testuser'; const email = 'test@example.com'; const password = 'password'; - const responseData = {message: 'User registered successfully'}; + const rememberMe = false; + const responseData = {accountId: 1, userId: 2, username: 'testuser', accessToken: 'jwt-token'}; mock.onPost(config.apiUrl + 'api/auth/signup').reply(200, responseData); const response = await authService.register(username, email, password); expect(response.data).toEqual(responseData); + expect(localStorage.getItem('accountId')).toEqual('1'); + expect(localStorage.getItem('userId')).toEqual('2'); + expect(localStorage.getItem('username')).toEqual('testuser'); + expect(localStorage.getItem('token')).toEqual('jwt-token'); }); }); From 10d892e58692dd5ddcf07cd5df2d8d8d24ceb22e Mon Sep 17 00:00:00 2001 From: JonasEbbinghaus25 Date: Wed, 12 Jun 2024 20:10:09 +0200 Subject: [PATCH 10/10] register --- src/main/web/src/services/tests/AuthService.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/web/src/services/tests/AuthService.test.tsx b/src/main/web/src/services/tests/AuthService.test.tsx index 604b18a2..e89ebf2f 100644 --- a/src/main/web/src/services/tests/AuthService.test.tsx +++ b/src/main/web/src/services/tests/AuthService.test.tsx @@ -22,7 +22,7 @@ describe('AuthService', () => { const response = await authService.register(username, email, password); - expect(response.data).toEqual(responseData); + expect(response).toEqual(responseData); expect(localStorage.getItem('accountId')).toEqual('1'); expect(localStorage.getItem('userId')).toEqual('2'); expect(localStorage.getItem('username')).toEqual('testuser');