48 lines
1.4 KiB
Go
48 lines
1.4 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"kra/internal/biz"
|
|
"kra/internal/service/dto"
|
|
)
|
|
|
|
type LoginResult struct {
|
|
User *dto.UserResponse `json:"user"`
|
|
Token string `json:"token"`
|
|
ExpiresAt int64 `json:"expiresAt"`
|
|
NeedChangePassword bool `json:"needChangePassword"`
|
|
}
|
|
|
|
type AuthService struct {
|
|
uc *biz.AuthenticationUsecase
|
|
}
|
|
|
|
func NewAuthService(uc *biz.AuthenticationUsecase) *AuthService {
|
|
return &AuthService{uc: uc}
|
|
}
|
|
|
|
func loginResult(value *biz.AuthenticationResult) *LoginResult {
|
|
return &LoginResult{User: convertUser(value.User), Token: value.Token, ExpiresAt: value.ExpiresAt.UnixMilli(), NeedChangePassword: value.NeedChangePassword}
|
|
}
|
|
|
|
func (s *AuthService) Login(ctx context.Context, req *dto.LoginRequest, ip, agent string) (*LoginResult, error) {
|
|
value, err := s.uc.Login(ctx, &biz.LoginAttempt{Username: req.Username, Password: req.Password, CaptchaID: req.CaptchaID, Captcha: req.Captcha, IP: ip, Agent: agent})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return loginResult(value), nil
|
|
}
|
|
|
|
func (s *AuthService) SwitchAuthority(ctx context.Context, id, authorityID uint) (*LoginResult, error) {
|
|
value, err := s.uc.SwitchAuthority(ctx, id, authorityID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return loginResult(value), nil
|
|
}
|
|
|
|
func (s *AuthService) AuthenticateToken(ctx context.Context, token string) (*biz.TokenAuthentication, error) {
|
|
return s.uc.AuthenticateToken(ctx, token)
|
|
}
|