Skip to content

Commit 4215ab6

Browse files
edited auth api
1 parent 9386a8c commit 4215ab6

File tree

5 files changed

+62
-23
lines changed

5 files changed

+62
-23
lines changed

src/main/java/com/dchprojects/mydictionaryrestapi/api/AuthApi.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.dchprojects.mydictionaryrestapi.domain.dto.AuthRequest;
44
import com.dchprojects.mydictionaryrestapi.domain.dto.AuthResponse;
55
import com.dchprojects.mydictionaryrestapi.domain.dto.CreateUserRequest;
6-
import com.dchprojects.mydictionaryrestapi.domain.entity.UserEntity;
76
import com.dchprojects.mydictionaryrestapi.service.AuthService;
87
import io.swagger.v3.oas.annotations.tags.Tag;
98
import lombok.RequiredArgsConstructor;
@@ -39,7 +38,7 @@ public ResponseEntity<AuthResponse> login(@RequestBody @Valid AuthRequest reques
3938
}
4039

4140
@PostMapping("/register")
42-
public ResponseEntity<UserEntity> register(@RequestBody @Valid CreateUserRequest createUserRequest) {
41+
public ResponseEntity<AuthResponse> register(@RequestBody @Valid CreateUserRequest createUserRequest) {
4342
try {
4443
return new ResponseEntity<>(authService.register(createUserRequest), HttpStatus.OK);
4544
} catch (ValidationException validationException) {
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
package com.dchprojects.mydictionaryrestapi.domain.dto;
22

3+
import com.dchprojects.mydictionaryrestapi.domain.entity.UserEntity;
4+
35
import com.fasterxml.jackson.annotation.JsonProperty;
46

57
public class AuthResponse {
68

7-
@JsonProperty("access_token")
8-
private String accessToken;
9+
@JsonProperty("user_entity")
10+
private UserEntity userEntity;
911

10-
@JsonProperty("expiration_date")
11-
private String expirationDate;
12+
@JsonProperty("jwt")
13+
private JWTResponse jwtResponse;
1214

13-
public AuthResponse(String accessToken, String expirationDate) {
14-
this.accessToken = accessToken;
15-
this.expirationDate = expirationDate;
16-
}
15+
public AuthResponse(UserEntity userEntity,
16+
JWTResponse jwtResponse) {
17+
18+
this.userEntity = userEntity;
19+
this.jwtResponse = jwtResponse;
1720

18-
public String getAccessToken() {
19-
return accessToken;
2021
}
2122

22-
public String getExpirationDate() { return expirationDate; }
23+
public UserEntity getUserEntity() { return userEntity; }
24+
25+
public JWTResponse getJwtResponse() { return jwtResponse; }
2326

24-
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.dchprojects.mydictionaryrestapi.domain.dto;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
public class JWTResponse {
6+
7+
@JsonProperty("access_token")
8+
private String accessToken;
9+
10+
@JsonProperty("expiration_date")
11+
private String expirationDate;
12+
13+
public JWTResponse(String accessToken,
14+
String expirationDate) {
15+
16+
this.accessToken = accessToken;
17+
this.expirationDate = expirationDate;
18+
19+
}
20+
21+
public String getAccessToken() {
22+
return accessToken;
23+
}
24+
25+
public String getExpirationDate() { return expirationDate; }
26+
27+
}

src/main/java/com/dchprojects/mydictionaryrestapi/service/AuthService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
import com.dchprojects.mydictionaryrestapi.domain.dto.AuthRequest;
44
import com.dchprojects.mydictionaryrestapi.domain.dto.AuthResponse;
55
import com.dchprojects.mydictionaryrestapi.domain.dto.CreateUserRequest;
6-
import com.dchprojects.mydictionaryrestapi.domain.entity.UserEntity;
76

87
public interface AuthService {
98

109
public AuthResponse login(AuthRequest request);
1110

12-
public UserEntity register(CreateUserRequest createUserRequest);
11+
public AuthResponse register(CreateUserRequest createUserRequest);
1312

1413
}

src/main/java/com/dchprojects/mydictionaryrestapi/service/impl/AuthServiceImpl.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package com.dchprojects.mydictionaryrestapi.service.impl;
22

33
import com.dchprojects.mydictionaryrestapi.configuration.security.JwtTokenUtil;
4-
import com.dchprojects.mydictionaryrestapi.domain.dto.AuthRequest;
5-
import com.dchprojects.mydictionaryrestapi.domain.dto.AuthResponse;
6-
import com.dchprojects.mydictionaryrestapi.domain.dto.CreateUserRequest;
7-
import com.dchprojects.mydictionaryrestapi.domain.dto.JwtTokenResponse;
4+
import com.dchprojects.mydictionaryrestapi.domain.dto.*;
85
import com.dchprojects.mydictionaryrestapi.domain.entity.UserEntity;
96
import com.dchprojects.mydictionaryrestapi.service.AuthService;
107
import com.dchprojects.mydictionaryrestapi.service.UserService;
@@ -30,6 +27,7 @@ public class AuthServiceImpl implements AuthService {
3027
@Override
3128
public AuthResponse login(AuthRequest request) {
3229
try {
30+
3331
Authentication authenticate = authenticationManager
3432
.authenticate(new UsernamePasswordAuthenticationToken(request.getNickname(),
3533
request.getPassword()));
@@ -40,8 +38,11 @@ public AuthResponse login(AuthRequest request) {
4038

4139
JwtTokenResponse jwtTokenResponse = jwtTokenUtil.generateAccessToken(userEntity);
4240

43-
return new AuthResponse(jwtTokenResponse.getAccessToken(), jwtTokenResponse.getExpirationDate().toString());
41+
JWTResponse jwtResponse = new JWTResponse(jwtTokenResponse.getAccessToken(),
42+
jwtTokenResponse.getExpirationDate().toString());
4443

44+
return new AuthResponse(userEntity, jwtResponse);
45+
4546
} catch (BadCredentialsException badCredentialsException) {
4647
throw new BadCredentialsException(badCredentialsException.getLocalizedMessage());
4748
} catch (NoSuchElementException noSuchElementException) {
@@ -50,9 +51,19 @@ public AuthResponse login(AuthRequest request) {
5051
}
5152

5253
@Override
53-
public UserEntity register(CreateUserRequest createUserRequest) {
54+
public AuthResponse register(CreateUserRequest createUserRequest) {
5455
try {
55-
return userService.createUser(createUserRequest);
56+
57+
UserEntity savedUser = userService.createUser(createUserRequest);
58+
59+
JwtTokenResponse jwtTokenResponse = jwtTokenUtil.generateAccessToken(savedUser);
60+
JWTResponse jwtResponse = new JWTResponse(jwtTokenResponse.getAccessToken(),
61+
jwtTokenResponse.getExpirationDate().toString());
62+
63+
return new AuthResponse(savedUser, jwtResponse);
64+
65+
} catch (BadCredentialsException badCredentialsException) {
66+
throw new BadCredentialsException(badCredentialsException.getLocalizedMessage());
5667
} catch (ValidationException validationException) {
5768
throw new ValidationException(validationException.getLocalizedMessage());
5869
}

0 commit comments

Comments
 (0)