65 lines
2.2 KiB
Java
65 lines
2.2 KiB
Java
package com.webgame.webgamebackend.api;
|
|
|
|
import com.webgame.webgamebackend.common.dto.ApiResponse;
|
|
import com.webgame.webgamebackend.common.dto.account.LoginRequest;
|
|
import com.webgame.webgamebackend.common.dto.account.LoginResponse;
|
|
import com.webgame.webgamebackend.common.dto.account.LogoutRequest;
|
|
import com.webgame.webgamebackend.common.dto.account.RefreshTokenRequest;
|
|
import com.webgame.webgamebackend.common.dto.account.RegisterRequest;
|
|
import com.webgame.webgamebackend.domain.account.AccountService;
|
|
import jakarta.validation.Valid;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.validation.annotation.Validated;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
/**
|
|
* 账号控制器
|
|
*/
|
|
@Validated
|
|
@RestController
|
|
@RequestMapping("/account")
|
|
@RequiredArgsConstructor
|
|
public class AccountController {
|
|
|
|
private final AccountService accountService;
|
|
|
|
/**
|
|
* 登录
|
|
*/
|
|
@PostMapping("/login")
|
|
public ApiResponse<LoginResponse> login(@Valid @RequestBody LoginRequest request) {
|
|
LoginResponse result = accountService.login(request);
|
|
return ApiResponse.ok(result);
|
|
}
|
|
|
|
/**
|
|
* 注册
|
|
*/
|
|
@PostMapping("/register")
|
|
public ApiResponse<LoginResponse> register(@Valid @RequestBody RegisterRequest request) {
|
|
LoginResponse result = accountService.register(request);
|
|
return ApiResponse.ok(result);
|
|
}
|
|
|
|
/**
|
|
* 刷新令牌,用 refresh token 换取新的 access token 和 refresh token
|
|
*/
|
|
@PostMapping("/refresh")
|
|
public ApiResponse<LoginResponse> refresh(@Valid @RequestBody RefreshTokenRequest request) {
|
|
LoginResponse result = accountService.refresh(request.refreshToken());
|
|
return ApiResponse.ok(result);
|
|
}
|
|
|
|
/**
|
|
* 登出,使 access token 和 refresh token 失效
|
|
*/
|
|
@PostMapping("/logout")
|
|
public ApiResponse<Void> logout(@Valid @RequestBody LogoutRequest request) {
|
|
accountService.logout(request.refreshToken());
|
|
return ApiResponse.ok(null);
|
|
}
|
|
}
|