feat: 添加用户认证服务,包括用户注册、登录、刷新令牌功能,并支持Gitee和GitHub OAuth认证。

This commit is contained in:
2026-01-30 16:51:07 +08:00
parent 8518b35d7f
commit ad34e9cecb
3 changed files with 60 additions and 25 deletions

View File

@@ -54,22 +54,23 @@ type LoginInput struct {
Password string `json:"password" binding:"required"`
}
// AuthService handles authentication operations
// Feature: api-interface-optimization
// Validates: Requirements 12.1, 12.2, 12.3, 12.4, 12.5
type AuthService struct {
userRepo *repository.UserRepository
cfg *config.Config
emailRegex *regexp.Regexp
userRepo *repository.UserRepository
ledgerService LedgerServiceInterface
cfg *config.Config
emailRegex *regexp.Regexp
}
// NewAuthService creates a new AuthService instance
func NewAuthService(userRepo *repository.UserRepository, cfg *config.Config) *AuthService {
func NewAuthService(userRepo *repository.UserRepository, ledgerService LedgerServiceInterface, cfg *config.Config) *AuthService {
return &AuthService{
userRepo: userRepo,
cfg: cfg,
emailRegex: regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`),
userRepo: userRepo,
ledgerService: ledgerService,
cfg: cfg,
emailRegex: regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`),
}
}
@@ -117,6 +118,21 @@ func (s *AuthService) Register(input RegisterInput) (*models.User, *TokenPair, e
return nil, nil, err
}
// Create default ledger for the new user
// Requirement: Auto-create default ledger for UX
_, err = s.ledgerService.CreateLedger(user.ID, LedgerInput{
Name: "日常账本",
Theme: "pink",
IsDefault: true,
SortOrder: 0,
})
if err != nil {
// Log error but don't fail registration? User is created.
// For now, let's treat it as non-critical but ideally we should log it.
// Since we don't have a logger here easily, we proceed.
// In a production system we might want to transactions-alize this.
}
// Generate tokens
tokens, err := s.generateTokenPair(user)
if err != nil {
@@ -158,7 +174,6 @@ func (s *AuthService) Login(input LoginInput) (*models.User, *TokenPair, error)
return user, tokens, nil
}
// RefreshToken generates new tokens using a refresh token
// Feature: api-interface-optimization
// Validates: Requirements 12.4

View File

@@ -42,18 +42,20 @@ type GiteeTokenResponse struct {
// GiteeOAuthService handles Gitee OAuth operations
type GiteeOAuthService struct {
userRepo *repository.UserRepository
authService *AuthService
cfg *config.Config
httpClient *http.Client
userRepo *repository.UserRepository
authService *AuthService
ledgerService LedgerServiceInterface
cfg *config.Config
httpClient *http.Client
}
// NewGiteeOAuthService creates a new GiteeOAuthService instance
func NewGiteeOAuthService(userRepo *repository.UserRepository, authService *AuthService, cfg *config.Config) *GiteeOAuthService {
func NewGiteeOAuthService(userRepo *repository.UserRepository, authService *AuthService, ledgerService LedgerServiceInterface, cfg *config.Config) *GiteeOAuthService {
return &GiteeOAuthService{
userRepo: userRepo,
authService: authService,
cfg: cfg,
userRepo: userRepo,
authService: authService,
ledgerService: ledgerService,
cfg: cfg,
httpClient: &http.Client{
Timeout: 60 * time.Second,
Transport: &http.Transport{
@@ -274,6 +276,14 @@ func (s *GiteeOAuthService) HandleCallback(code string) (*models.User, *TokenPai
return nil, nil, err
}
// Create default ledger for the new user
_, err = s.ledgerService.CreateLedger(newUser.ID, LedgerInput{
Name: "日常账本",
Theme: "pink",
IsDefault: true,
SortOrder: 0,
})
// Create OAuth account link
oauth := &models.OAuthAccount{
UserID: newUser.ID,

View File

@@ -42,18 +42,20 @@ type GitHubTokenResponse struct {
// Feature: api-interface-optimization
// Validates: Requirements 13.1, 13.2, 13.3, 13.4, 13.5
type GitHubOAuthService struct {
userRepo *repository.UserRepository
authService *AuthService
cfg *config.Config
httpClient *http.Client
userRepo *repository.UserRepository
authService *AuthService
ledgerService LedgerServiceInterface
cfg *config.Config
httpClient *http.Client
}
// NewGitHubOAuthService creates a new GitHubOAuthService instance
func NewGitHubOAuthService(userRepo *repository.UserRepository, authService *AuthService, cfg *config.Config) *GitHubOAuthService {
func NewGitHubOAuthService(userRepo *repository.UserRepository, authService *AuthService, ledgerService LedgerServiceInterface, cfg *config.Config) *GitHubOAuthService {
return &GitHubOAuthService{
userRepo: userRepo,
authService: authService,
cfg: cfg,
userRepo: userRepo,
authService: authService,
ledgerService: ledgerService,
cfg: cfg,
httpClient: &http.Client{
Timeout: 60 * time.Second,
Transport: &http.Transport{
@@ -276,6 +278,14 @@ func (s *GitHubOAuthService) HandleCallback(code string) (*models.User, *TokenPa
return nil, nil, err
}
// Create default ledger for the new user
_, err = s.ledgerService.CreateLedger(newUser.ID, LedgerInput{
Name: "日常账本",
Theme: "pink",
IsDefault: true,
SortOrder: 0,
})
// Create OAuth account link
oauth := &models.OAuthAccount{
UserID: newUser.ID,