fix:rename后重新生成token
This commit is contained in:
@@ -4,50 +4,13 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AccountHandler struct {
|
type AccountHandler struct {
|
||||||
accountService *AccountService
|
accountService *AccountService
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreateAccountRequest struct {
|
|
||||||
Username string `json:"username"`
|
|
||||||
Password string `json:"password"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type RenameRequest struct {
|
|
||||||
NewUsername string `json:"new_username"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type FindByIDRequest struct {
|
|
||||||
ID uint `json:"id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type FindByIDResponse struct {
|
|
||||||
ID uint `json:"id"`
|
|
||||||
Username string `json:"username"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type FindByUsernameRequest struct {
|
|
||||||
Username string `json:"username"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type FindByUsernameResponse struct {
|
|
||||||
ID uint `json:"id"`
|
|
||||||
Username string `json:"username"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type ChangePasswordRequest struct {
|
|
||||||
Username string `json:"username"`
|
|
||||||
OldPassword string `json:"old_password"`
|
|
||||||
NewPassword string `json:"new_password"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type LoginRequest struct {
|
|
||||||
Username string `json:"username"`
|
|
||||||
Password string `json:"password"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewAccountHandler(accountService *AccountService) *AccountHandler {
|
func NewAccountHandler(accountService *AccountService) *AccountHandler {
|
||||||
return &AccountHandler{accountService: accountService}
|
return &AccountHandler{accountService: accountService}
|
||||||
}
|
}
|
||||||
@@ -78,11 +41,24 @@ func (h *AccountHandler) Rename(c *gin.Context) {
|
|||||||
c.JSON(400, gin.H{"error": err.Error()})
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := h.accountService.Rename(c.Request.Context(), accountID, req.NewUsername); err != nil {
|
token, err := h.accountService.Rename(c.Request.Context(), accountID, req.NewUsername)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, ErrNewUsernameRequired) {
|
||||||
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if errors.Is(err, ErrUsernameTaken) {
|
||||||
|
c.JSON(409, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
c.JSON(404, gin.H{"error": "account not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
c.JSON(500, gin.H{"error": err.Error()})
|
c.JSON(500, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(200, gin.H{"message": "account renamed"})
|
c.JSON(200, gin.H{"token": token})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *AccountHandler) ChangePassword(c *gin.Context) {
|
func (h *AccountHandler) ChangePassword(c *gin.Context) {
|
||||||
|
|||||||
@@ -22,10 +22,30 @@ func (ar *AccountRepository) CreateAccount(ctx context.Context, account *Account
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ar *AccountRepository) Rename(ctx context.Context, id uint, newUsername string) error {
|
func (ar *AccountRepository) Rename(ctx context.Context, id uint, newUsername string) error {
|
||||||
if err := ar.db.WithContext(ctx).Model(&Account{}).Where("id = ?", id).Update("username", newUsername).Error; err != nil {
|
result := ar.db.WithContext(ctx).Model(&Account{}).Where("id = ?", id).Update("username", newUsername)
|
||||||
|
if result.Error != nil {
|
||||||
|
return result.Error
|
||||||
|
}
|
||||||
|
if result.RowsAffected == 0 {
|
||||||
|
return gorm.ErrRecordNotFound
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ar *AccountRepository) RenameWithToken(ctx context.Context, id uint, newUsername string, token string) error {
|
||||||
|
return ar.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||||
|
result := tx.Model(&Account{}).Where("id = ?", id).Update("username", newUsername)
|
||||||
|
if result.Error != nil {
|
||||||
|
return result.Error
|
||||||
|
}
|
||||||
|
if result.RowsAffected == 0 {
|
||||||
|
return gorm.ErrRecordNotFound
|
||||||
|
}
|
||||||
|
if err := tx.Model(&Account{}).Where("id = ?", id).Update("token", token).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ar *AccountRepository) ChangePassword(ctx context.Context, id uint, newPassword string) error {
|
func (ar *AccountRepository) ChangePassword(ctx context.Context, id uint, newPassword string) error {
|
||||||
|
|||||||
@@ -5,13 +5,20 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"feedsystem_video_go/internal/auth"
|
"feedsystem_video_go/internal/auth"
|
||||||
|
|
||||||
|
"github.com/go-sql-driver/mysql"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AccountService struct {
|
type AccountService struct {
|
||||||
accountRepository *AccountRepository
|
accountRepository *AccountRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrUsernameTaken = errors.New("username already exists")
|
||||||
|
ErrNewUsernameRequired = errors.New("new_username is required")
|
||||||
|
)
|
||||||
|
|
||||||
func NewAccountService(accountRepository *AccountRepository) *AccountService {
|
func NewAccountService(accountRepository *AccountRepository) *AccountService {
|
||||||
return &AccountService{accountRepository: accountRepository}
|
return &AccountService{accountRepository: accountRepository}
|
||||||
}
|
}
|
||||||
@@ -28,11 +35,28 @@ func (as *AccountService) CreateAccount(ctx context.Context, account *Account) e
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *AccountService) Rename(ctx context.Context, accountID uint, newUsername string) error {
|
func (as *AccountService) Rename(ctx context.Context, accountID uint, newUsername string) (string, error) {
|
||||||
if err := as.accountRepository.Rename(ctx, accountID, newUsername); err != nil {
|
if newUsername == "" {
|
||||||
return err
|
return "", ErrNewUsernameRequired
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
|
token, err := auth.GenerateToken(accountID, newUsername)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := as.accountRepository.RenameWithToken(ctx, accountID, newUsername, token); err != nil {
|
||||||
|
var mysqlErr *mysql.MySQLError
|
||||||
|
if errors.As(err, &mysqlErr) && mysqlErr.Number == 1062 {
|
||||||
|
return "", ErrUsernameTaken
|
||||||
|
}
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return token, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *AccountService) ChangePassword(ctx context.Context, username, oldPassword, newPassword string) error {
|
func (as *AccountService) ChangePassword(ctx context.Context, username, oldPassword, newPassword string) error {
|
||||||
|
|||||||
Reference in New Issue
Block a user