Some checks failed
Build docker and publish / build (20.15.1) (push) Failing after 6m39s
feat(database): 添加用户算法和盐字段的迁移脚本 fix(subscribe): 修复服务器用户列表缓存问题,临时禁用缓存 style(model): 清理用户模型注释,简化代码结构 chore: 删除无用脚本和测试文件 docs: 添加用户绑定流程文档 perf(login): 优化设备登录性能,添加设备缓存键 fix(unbind): 修复设备解绑时的缓存清理逻辑 refactor(verify): 简化邮箱验证逻辑,移除冗余代码 build(docker): 更新Dockerfile配置,使用scratch基础镜像
76 lines
2.4 KiB
Go
76 lines
2.4 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"github.com/perfect-panel/server/internal/config"
|
|
"github.com/perfect-panel/server/internal/model/user"
|
|
"github.com/perfect-panel/server/internal/svc"
|
|
"github.com/perfect-panel/server/internal/types"
|
|
"github.com/perfect-panel/server/pkg/constant"
|
|
"github.com/perfect-panel/server/pkg/logger"
|
|
"github.com/perfect-panel/server/pkg/xerr"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type VerifyEmailLogic struct {
|
|
logger.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
// Verify Email
|
|
func NewVerifyEmailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VerifyEmailLogic {
|
|
return &VerifyEmailLogic{
|
|
Logger: logger.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
type CacheKeyPayload struct {
|
|
Code string `json:"code"`
|
|
LastAt int64 `json:"lastAt"`
|
|
}
|
|
|
|
func (l *VerifyEmailLogic) VerifyEmail(req *types.VerifyEmailRequest) error {
|
|
cacheKey := fmt.Sprintf("%s:%s:%s", config.AuthCodeCacheKey, constant.Security, req.Email)
|
|
value, err := l.svcCtx.Redis.Get(l.ctx, cacheKey).Result()
|
|
if err != nil {
|
|
l.Errorw("Redis Error", logger.Field("error", err.Error()), logger.Field("cacheKey", cacheKey))
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.VerifyCodeError), "code error")
|
|
}
|
|
|
|
var payload CacheKeyPayload
|
|
err = json.Unmarshal([]byte(value), &payload)
|
|
if err != nil {
|
|
l.Errorw("Redis Error", logger.Field("error", err.Error()), logger.Field("cacheKey", cacheKey))
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.VerifyCodeError), "code error")
|
|
}
|
|
if payload.Code != req.Code {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.VerifyCodeError), "code error")
|
|
}
|
|
l.svcCtx.Redis.Del(l.ctx, cacheKey)
|
|
|
|
u, ok := l.ctx.Value(constant.CtxKeyUser).(*user.User)
|
|
if !ok {
|
|
logger.Error("current user is not found in context")
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.InvalidAccess), "Invalid Access")
|
|
}
|
|
method, err := l.svcCtx.UserModel.FindUserAuthMethodByOpenID(l.ctx, "email", req.Email)
|
|
if err != nil {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "FindUserAuthMethodByOpenID error")
|
|
}
|
|
if method.UserId != u.Id {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.InvalidAccess), "invalid access")
|
|
}
|
|
method.Verified = true
|
|
err = l.svcCtx.UserModel.UpdateUserAuthMethods(l.ctx, method)
|
|
if err != nil {
|
|
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), "UpdateUserAuthMethods error")
|
|
}
|
|
return nil
|
|
}
|