-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from kaizerpwn/feature/auth
Authentication code separated to AuthService
- Loading branch information
Showing
3 changed files
with
101 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
app/src/main/java/com/ibrahimokic/ordermanagement/service/AuthService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.ibrahimokic.ordermanagement.service; | ||
|
||
import com.ibrahimokic.ordermanagement.domain.dto.UserDto; | ||
import com.ibrahimokic.ordermanagement.domain.dto.api.LoginRequest; | ||
import com.ibrahimokic.ordermanagement.domain.entity.User; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import jakarta.validation.Valid; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
|
||
public interface AuthService { | ||
public ResponseEntity<?> loginUser(@Validated @RequestBody LoginRequest request, HttpServletResponse response); | ||
public ResponseEntity<User> registerUser(@RequestBody(required = false) @Valid UserDto userDto, HttpServletResponse response); | ||
} |
81 changes: 81 additions & 0 deletions
81
app/src/main/java/com/ibrahimokic/ordermanagement/service/impl/AuthServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.ibrahimokic.ordermanagement.service.impl; | ||
|
||
import com.ibrahimokic.ordermanagement.domain.dto.UserDto; | ||
import com.ibrahimokic.ordermanagement.domain.dto.api.LoginRequest; | ||
import com.ibrahimokic.ordermanagement.domain.entity.User; | ||
import com.ibrahimokic.ordermanagement.mapper.Mapper; | ||
import com.ibrahimokic.ordermanagement.security.JwtIssuer; | ||
import com.ibrahimokic.ordermanagement.service.AuthService; | ||
import com.ibrahimokic.ordermanagement.service.UserService; | ||
import jakarta.servlet.http.Cookie; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.hibernate.MappingException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class AuthServiceImpl implements AuthService { | ||
private final UserService userService; | ||
private final JwtIssuer jwtIssuer; | ||
private final Mapper<User, UserDto> userMapper; | ||
|
||
@Override | ||
public ResponseEntity<?> loginUser(LoginRequest request, HttpServletResponse response) { | ||
User user = userService.loginUser(request); | ||
|
||
if (user != null) { | ||
String accessToken = jwtIssuer.issue( | ||
user.getUserId(), | ||
request.getUsername(), | ||
user.getRole() | ||
); | ||
|
||
Cookie cookie = new Cookie("accessToken", accessToken); | ||
cookie.setHttpOnly(true); | ||
cookie.setMaxAge(24 * 60 * 60); | ||
cookie.setPath("/"); | ||
response.addCookie(cookie); | ||
|
||
return ResponseEntity.status(HttpStatus.OK) | ||
.contentType(MediaType.TEXT_PLAIN) | ||
.body("Successfully logged in"); | ||
} else { | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND) | ||
.contentType(MediaType.TEXT_PLAIN) | ||
.body("Username and password does not match any user in the database"); | ||
} | ||
} | ||
|
||
@Override | ||
public ResponseEntity<User> registerUser(UserDto userDto, HttpServletResponse response) { | ||
try { | ||
if (userDto == null) { | ||
return ResponseEntity.badRequest().build(); | ||
} | ||
|
||
|
||
User user = userMapper.mapFrom(userDto); | ||
User createdUser = userService.createUser(user); | ||
|
||
String accessToken = jwtIssuer.issue( | ||
user.getUserId(), | ||
user.getUsername(), | ||
user.getRole() | ||
); | ||
|
||
Cookie cookie = new Cookie("accessToken", accessToken); | ||
cookie.setHttpOnly(true); | ||
cookie.setMaxAge(24 * 60 * 60); | ||
cookie.setPath("/"); | ||
response.addCookie(cookie); | ||
|
||
return ResponseEntity.status(HttpStatus.CREATED).body(createdUser); | ||
} catch (MappingException mappingException) { | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); | ||
} | ||
} | ||
} |