feat: portal auth login and password reset
This commit is contained in:
@@ -16,6 +16,7 @@ import (
|
||||
"go.ipao.vip/gen"
|
||||
"go.ipao.vip/gen/field"
|
||||
"go.ipao.vip/gen/types"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// @provider
|
||||
@@ -84,6 +85,31 @@ func (t *user) Create(ctx context.Context, user *models.User) (*models.User, err
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// ResetPasswordByUsername 通过用户名(手机号)重置密码。
|
||||
func (t *user) ResetPasswordByUsername(ctx context.Context, username, newPassword string) error {
|
||||
username = strings.TrimSpace(username)
|
||||
if username == "" {
|
||||
return errors.New("username is required")
|
||||
}
|
||||
if newPassword == "" {
|
||||
return errors.New("new_password is required")
|
||||
}
|
||||
|
||||
m, err := t.FindByUsername(ctx, username)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// bcrypt hash,避免直接落明文。
|
||||
bytes, err := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "generate password hash failed")
|
||||
}
|
||||
|
||||
m.Password = string(bytes)
|
||||
return m.Save(ctx)
|
||||
}
|
||||
|
||||
// SetStatus 设置用户状态(超级管理员侧)。
|
||||
func (t *user) SetStatus(ctx context.Context, userID int64, status consts.UserStatus) error {
|
||||
m, err := t.FindByID(ctx, userID)
|
||||
|
||||
Reference in New Issue
Block a user