Files
neo-blog/internal/controller/v1/user.go
Snowykami b0b32c93d1 feat: add email verification and password reset functionality
- Introduced environment variables for database and email configurations.
- Implemented email verification code generation and validation.
- Added password reset feature with email verification.
- Updated user registration and profile management APIs.
- Refactored user security settings to include email and password updates.
- Enhanced console layout with internationalization support.
- Removed deprecated settings page and integrated global settings.
- Added new reset password page and form components.
- Updated localization files for new features and translations.
2025-09-23 00:33:34 +08:00

267 lines
7.3 KiB
Go

package v1
import (
"context"
"strconv"
"strings"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/snowykami/neo-blog/internal/ctxutils"
"github.com/snowykami/neo-blog/internal/dto"
"github.com/snowykami/neo-blog/internal/service"
"github.com/snowykami/neo-blog/pkg/constant"
"github.com/snowykami/neo-blog/pkg/errs"
"github.com/snowykami/neo-blog/pkg/resps"
utils2 "github.com/snowykami/neo-blog/pkg/utils"
)
type UserController struct {
service *service.UserService
}
func NewUserController() *UserController {
return &UserController{
service: service.NewUserService(),
}
}
func (u *UserController) Login(ctx context.Context, c *app.RequestContext) {
var userLoginReq dto.UserLoginReq
if err := c.BindAndValidate(&userLoginReq); err != nil {
resps.BadRequest(c, resps.ErrParamInvalid)
return
}
resp, err := u.service.UserLogin(&userLoginReq)
if err != nil {
serviceErr := errs.AsServiceError(err)
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
return
}
ctxutils.SetTokenAndRefreshTokenCookie(c, resp.Token, resp.RefreshToken)
resps.Ok(c, resps.Success, utils.H{
"token": resp.Token,
"user": resp.User,
})
}
func (u *UserController) Register(ctx context.Context, c *app.RequestContext) {
var userRegisterReq dto.UserRegisterReq
if err := c.BindAndValidate(&userRegisterReq); err != nil {
resps.BadRequest(c, resps.ErrParamInvalid)
return
}
resp, err := u.service.UserRegister(&userRegisterReq)
if err != nil {
serviceErr := errs.AsServiceError(err)
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
return
}
ctxutils.SetTokenAndRefreshTokenCookie(c, resp.Token, resp.RefreshToken)
resps.Ok(c, resps.Success, utils.H{
"token": resp.Token,
"user": resp.User,
})
}
func (u *UserController) Logout(ctx context.Context, c *app.RequestContext) {
ctxutils.ClearTokenAndRefreshTokenCookie(c)
resps.Ok(c, resps.Success, nil)
// 尝试吊销服务端状态:若用户登录的情况下
// TODO: 添加服务端状态的吊销逻辑
}
func (u *UserController) OidcList(ctx context.Context, c *app.RequestContext) {
oidcConfigs, err := u.service.ListOidcConfigs()
if err != nil {
serviceErr := errs.AsServiceError(err)
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
return
}
resps.Ok(c, resps.Success, oidcConfigs)
}
func (u *UserController) OidcLogin(ctx context.Context, c *app.RequestContext) {
name := c.Param("name")
code := c.Query("code")
state := c.Query("state")
redirectUri := c.Query("redirect_back") // 前端路由登录前的重定向地址
if redirectUri == "" {
redirectUri = "/"
}
oidcLoginReq := &dto.OidcLoginReq{
Name: name,
Code: code,
State: state,
}
resp, err := u.service.OidcLogin(oidcLoginReq)
if err != nil {
serviceErr := errs.AsServiceError(err)
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
return
}
ctxutils.SetTokenAndRefreshTokenCookie(c, resp.Token, resp.RefreshToken)
resps.Redirect(c, redirectUri) // 重定向到前端路由
}
func (u *UserController) GetUser(ctx context.Context, c *app.RequestContext) {
userID := c.Param("id")
userIDInt, err := strconv.Atoi(userID)
if err != nil || userIDInt <= 0 {
currentUserID, ok := ctxutils.GetCurrentUserID(ctx)
if !ok {
resps.Unauthorized(c, resps.ErrUnauthorized)
return
}
userIDInt = int(currentUserID)
}
resp, err := u.service.GetUser(&dto.GetUserReq{UserID: uint(userIDInt)})
if err != nil {
serviceErr := errs.AsServiceError(err)
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
return
}
resps.Ok(c, resps.Success, resp.User)
}
func (u *UserController) GetUserByUsername(ctx context.Context, c *app.RequestContext) {
username := c.Param("username")
if username == "" {
resps.BadRequest(c, resps.ErrParamInvalid)
return
}
resp, err := u.service.GetUserByUsername(&dto.GetUserByUsernameReq{Username: username})
if err != nil {
serviceErr := errs.AsServiceError(err)
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
return
}
resps.Ok(c, resps.Success, resp.User)
}
func (u *UserController) UpdateUser(ctx context.Context, c *app.RequestContext) {
userID := c.Param("id")
if userID == "" {
resps.BadRequest(c, resps.ErrParamInvalid)
return
}
userIDInt, err := strconv.Atoi(userID)
if err != nil || userIDInt <= 0 {
resps.BadRequest(c, resps.ErrParamInvalid)
return
}
var updateUserReq dto.UpdateUserReq
if err := c.BindAndValidate(&updateUserReq); err != nil {
resps.BadRequest(c, resps.ErrParamInvalid)
return
}
updateUserReq.ID = uint(userIDInt)
currentUser, ok := ctxutils.GetCurrentUser(ctx)
if !ok {
resps.Unauthorized(c, resps.ErrUnauthorized)
return
}
if currentUser.ID != updateUserReq.ID {
resps.Forbidden(c, resps.ErrForbidden)
return
}
resp, err := u.service.UpdateUser(&updateUserReq)
if err != nil {
serviceErr := errs.AsServiceError(err)
resps.Custom(c, serviceErr.Code, err.Error(), nil)
return
}
resps.Ok(c, resps.Success, resp)
}
func (u *UserController) VerifyEmail(ctx context.Context, c *app.RequestContext) {
var verifyEmailReq dto.VerifyEmailReq
if err := c.BindAndValidate(&verifyEmailReq); err != nil {
resps.BadRequest(c, resps.ErrParamInvalid)
return
}
if verifyEmailReq.Email == "" {
resps.BadRequest(c, resps.ErrParamInvalid)
}
resp, err := u.service.RequestVerifyEmail(&verifyEmailReq)
if err != nil {
serviceErr := errs.AsServiceError(err)
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
return
}
resps.Ok(c, resps.Success, resp)
}
func (u *UserController) ChangePassword(ctx context.Context, c *app.RequestContext) {
var updatePasswordReq dto.UpdatePasswordReq
if err := c.BindAndValidate(&updatePasswordReq); err != nil {
resps.BadRequest(c, resps.ErrParamInvalid)
return
}
ok, err := u.service.UpdatePassword(ctx, &updatePasswordReq)
if err != nil {
resps.InternalServerError(c, err.Error())
return
}
if !ok {
resps.BadRequest(c, "Failed to change password")
return
}
resps.Ok(c, resps.Success, nil)
}
func (u *UserController) ResetPassword(ctx context.Context, c *app.RequestContext) {
var resetPasswordReq dto.ResetPasswordReq
if err := c.BindAndValidate(&resetPasswordReq); err != nil {
resps.BadRequest(c, resps.ErrParamInvalid)
return
}
email := strings.TrimSpace(string(c.GetHeader(constant.HeaderKeyEmail)))
if email == "" {
resps.BadRequest(c, "Email header is required")
return
}
resetPasswordReq.Email = email
ok, err := u.service.ResetPassword(&resetPasswordReq)
if err != nil {
resps.InternalServerError(c, err.Error())
return
}
if !ok {
resps.BadRequest(c, "Failed to reset password")
return
}
resps.Ok(c, resps.Success, nil)
}
func (u *UserController) ChangeEmail(ctx context.Context, c *app.RequestContext) {
email := strings.TrimSpace(string(c.GetHeader(constant.HeaderKeyEmail)))
if email == "" {
resps.BadRequest(c, "Email header is required")
return
}
ok, err := u.service.UpdateEmail(ctx, email)
if err != nil {
resps.InternalServerError(c, err.Error())
return
}
if !ok {
resps.BadRequest(c, "Failed to change email")
return
}
resps.Ok(c, resps.Success, nil)
}
func (u *UserController) GetCaptchaConfig(ctx context.Context, c *app.RequestContext) {
resps.Ok(c, "ok", utils.H{
"provider": utils2.Env.Get(constant.EnvKeyCaptchaProvider),
"site_key": utils2.Env.Get(constant.EnvKeyCaptchaSiteKey),
"url": utils2.Env.Get(constant.EnvKeyCaptchaUrl),
})
}