45 lines
1.3 KiB
Go
45 lines
1.3 KiB
Go
package service
|
||
|
||
import (
|
||
"context"
|
||
|
||
"fashionapi/internal/dto"
|
||
"fashionapi/internal/model"
|
||
"fashionapi/internal/pkg/jwt"
|
||
"fashionapi/internal/repository"
|
||
)
|
||
|
||
// AuthService 账号体系业务接口。
|
||
//
|
||
// 当前只保留 Me(取当前用户)。注册 / 登录已随路由一并下线:前端没有登录注册入口,
|
||
// 对外开放即是垃圾账号与撞库入口。将来接入时补回 Register / Login 与 issue 令牌签发逻辑,
|
||
// 并同步前端 src/lib/api.ts 与登录页。
|
||
type AuthService interface {
|
||
Me(ctx context.Context, userID uint32) (*dto.UserPayload, error)
|
||
}
|
||
|
||
type authService struct {
|
||
users repository.UserRepository
|
||
jwt *jwt.Manager
|
||
}
|
||
|
||
// NewAuthService 创建账号服务。
|
||
func NewAuthService(users repository.UserRepository, jwtManager *jwt.Manager) AuthService {
|
||
return &authService{users: users, jwt: jwtManager}
|
||
}
|
||
|
||
// Me 用令牌中的 uid 取回最新用户信息。
|
||
func (s *authService) Me(ctx context.Context, userID uint32) (*dto.UserPayload, error) {
|
||
user, err := s.users.FindByID(ctx, userID)
|
||
if err != nil {
|
||
// 令牌有效但用户已不存在,同样视为未授权
|
||
return nil, unauthorized("unauthorized")
|
||
}
|
||
p := toUserPayload(*user)
|
||
return &p, nil
|
||
}
|
||
|
||
func toUserPayload(u model.User) dto.UserPayload {
|
||
return dto.UserPayload{ID: u.ID, Username: u.Username, Email: u.Email}
|
||
}
|