Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.web.bind.annotation.RestController;

import vaultweb.apigateway.dto.request.ChangePasswordRequest;
import vaultweb.apigateway.dto.request.ChangeUsernameRequest;
import vaultweb.apigateway.dto.request.LoginRequest;
import vaultweb.apigateway.dto.request.UserRegistrationRequest;
import vaultweb.apigateway.dto.response.AuthResponse;
Expand Down Expand Up @@ -60,16 +61,18 @@ public Mono<Void> logout() {
return authService.logout();
}

@ResponseStatus(HttpStatus.OK)
@PostMapping("change-username")
public String changeUsername() {
return "changeUsername";
public Mono<Void> changeUsername(@Valid @RequestBody ChangeUsernameRequest request) {
return authService.changeUsername(request);
}

@PostMapping("change-email")
public String changeEmail() {
return "changeEmail";
}

@ResponseStatus(HttpStatus.OK)
@PostMapping("change-password")
public Mono<Void> changePassword(@Valid @RequestBody ChangePasswordRequest request) {
return authService.changePassword(request);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package vaultweb.apigateway.dto.request;

import jakarta.validation.constraints.NotEmpty;

public record ChangeUsernameRequest(
@NotEmpty(message = "Your new username is required") String newUsername) {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.stereotype.Service;

import vaultweb.apigateway.dto.request.ChangePasswordRequest;
import vaultweb.apigateway.dto.request.ChangeUsernameRequest;
import vaultweb.apigateway.dto.request.LoginRequest;
import vaultweb.apigateway.dto.request.UserRegistrationRequest;
import vaultweb.apigateway.dto.response.AuthResponse;
Expand Down Expand Up @@ -128,6 +129,41 @@ public Mono<Void> changePassword(ChangePasswordRequest request) {
});
}

public Mono<Void> changeUsername(ChangeUsernameRequest request) {
return securityContextUtil
.getAuthenticatedUsername()
.flatMap(userRepository::findByUsername)
.switchIfEmpty(
Mono.error(
new DefaultException(
"username from token has no registered user",
DefaultExceptionLevels.AUTHENTICATION_EXCEPTION)))
.flatMap(
user -> {
if (request.newUsername().equals(user.getUsername())) {
return Mono.error(
new DefaultException(
"You didn't change anything", DefaultExceptionLevels.DEFAULT_EXCEPTION));
}

return userRepository
.existsByUsername(request.newUsername())
.flatMap(
usernameExists -> {
if (usernameExists) {
return Mono.error(
new DefaultException(
"User with username "
+ request.newUsername()
+ " already exists"));
}

user.setUsername(request.newUsername());
return userRepository.save(user).then();
});
});
}

public Mono<AuthResponse> switchToken(String token) {
// find refresh
return refreshTokenRepository
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ services:
POSTGRES_PASSWORD: password
POSTGRES_DB: auth_gateway_db
ports:
- "5433:5432"
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data

Expand Down