answer/internal/service/auth/auth.go

90 lines
3.2 KiB
Go
Raw Normal View History

2022-09-27 17:59:05 +08:00
package auth
import (
"context"
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/pkg/token"
2022-09-27 17:59:05 +08:00
"github.com/segmentfault/pacman/log"
)
// AuthRepo auth repository
type AuthRepo interface {
GetUserCacheInfo(ctx context.Context, accessToken string) (userInfo *entity.UserCacheInfo, err error)
SetUserCacheInfo(ctx context.Context, accessToken string, userInfo *entity.UserCacheInfo) error
RemoveUserCacheInfo(ctx context.Context, accessToken string) (err error)
2022-10-26 16:59:33 +08:00
SetUserStatus(ctx context.Context, userID string, userInfo *entity.UserCacheInfo) (err error)
2022-09-27 17:59:05 +08:00
GetUserStatus(ctx context.Context, userID string) (userInfo *entity.UserCacheInfo, err error)
RemoveUserStatus(ctx context.Context, userID string) (err error)
GetBackyardUserCacheInfo(ctx context.Context, accessToken string) (userInfo *entity.UserCacheInfo, err error)
SetBackyardUserCacheInfo(ctx context.Context, accessToken string, userInfo *entity.UserCacheInfo) error
RemoveBackyardUserCacheInfo(ctx context.Context, accessToken string) (err error)
2022-09-27 17:59:05 +08:00
}
// AuthService kit service
type AuthService struct {
authRepo AuthRepo
}
// NewAuthService email service
func NewAuthService(authRepo AuthRepo) *AuthService {
return &AuthService{
authRepo: authRepo,
}
}
func (as *AuthService) GetUserCacheInfo(ctx context.Context, accessToken string) (userInfo *entity.UserCacheInfo, err error) {
userCacheInfo, err := as.authRepo.GetUserCacheInfo(ctx, accessToken)
if err != nil {
return nil, err
}
cacheInfo, _ := as.authRepo.GetUserStatus(ctx, userCacheInfo.UserID)
if cacheInfo != nil {
log.Infof("user status updated: %+v", cacheInfo)
userCacheInfo.UserStatus = cacheInfo.UserStatus
userCacheInfo.EmailStatus = cacheInfo.EmailStatus
// update current user cache info
err := as.authRepo.SetUserCacheInfo(ctx, accessToken, userCacheInfo)
if err != nil {
return nil, err
}
}
return userCacheInfo, nil
}
func (as *AuthService) SetUserCacheInfo(ctx context.Context, userInfo *entity.UserCacheInfo) (accessToken string, err error) {
accessToken = token.GenerateToken()
err = as.authRepo.SetUserCacheInfo(ctx, accessToken, userInfo)
return accessToken, err
}
func (as *AuthService) UpdateUserCacheInfo(ctx context.Context, token string, userInfo *entity.UserCacheInfo) (err error) {
err = as.authRepo.SetUserCacheInfo(ctx, token, userInfo)
if err != nil {
return err
}
if err := as.authRepo.RemoveUserStatus(ctx, userInfo.UserID); err != nil {
log.Error(err)
}
return
}
2022-09-27 17:59:05 +08:00
func (as *AuthService) RemoveUserCacheInfo(ctx context.Context, accessToken string) (err error) {
return as.authRepo.RemoveUserCacheInfo(ctx, accessToken)
}
//cms
func (as *AuthService) GetCmsUserCacheInfo(ctx context.Context, accessToken string) (userInfo *entity.UserCacheInfo, err error) {
return as.authRepo.GetBackyardUserCacheInfo(ctx, accessToken)
2022-09-27 17:59:05 +08:00
}
func (as *AuthService) SetCmsUserCacheInfo(ctx context.Context, accessToken string, userInfo *entity.UserCacheInfo) (err error) {
err = as.authRepo.SetBackyardUserCacheInfo(ctx, accessToken, userInfo)
2022-09-27 17:59:05 +08:00
return err
}
func (as *AuthService) RemoveCmsUserCacheInfo(ctx context.Context, accessToken string) (err error) {
return as.authRepo.RemoveBackyardUserCacheInfo(ctx, accessToken)
2022-09-27 17:59:05 +08:00
}