Skip to content
This repository has been archived by the owner on Sep 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #951 from SE-TINF22B6/profile-page
Browse files Browse the repository at this point in the history
Profile page adpation
  • Loading branch information
maxschwinghammer authored Jun 12, 2024
2 parents 267dec0 + 10d892e commit d8901ba
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()));

Expand Down Expand Up @@ -102,7 +105,7 @@ public ResponseEntity<?> signup(@Valid @RequestBody SignupRequest signupRequest)

userRepository.save(newUser);

return ResponseEntity.ok(new MessageResponse("User registered successfully!"));
return loginAndCreateJWT(new LoginRequest(signupRequest.getUsername(), signupRequest.getPassword(), false));
}

@PostMapping("email-verification")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
17 changes: 11 additions & 6 deletions src/main/web/src/services/AuthService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ 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<any> => {
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 => {
Expand Down
9 changes: 7 additions & 2 deletions src/main/web/src/services/tests/AuthService.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(response).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');
});
});

Expand Down

0 comments on commit d8901ba

Please sign in to comment.