feat(auth): 添加用户登录功能
This commit is contained in:
1 parent
92a9e20934
commit
63c8f2980b
10 files changed
+210
-18
No files matched your search
@@ -0,0 +1,33 @@
|
||||
package auth_ser
|
||||
|
||||
import (
|
||||
"EnGin/internal/db"
|
||||
"EnGin/internal/ent"
|
||||
"EnGin/internal/ent/user"
|
||||
"EnGin/internal/global"
|
||||
"EnGin/internal/utils/pwd"
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrUserNotFound = errors.New("用户不存在")
|
||||
ErrAccount = errors.New("账号和密码错误")
|
||||
)
|
||||
|
||||
func Login(ctx context.Context, username, password string) (*ent.User, error) {
|
||||
u, err := db.Client.User.Query().Where(user.UsernameEQ(username)).WithRoles().Only(ctx)
|
||||
if err != nil {
|
||||
if ent.IsNotFound(err) {
|
||||
return nil, ErrUserNotFound
|
||||
}
|
||||
|
||||
return nil, global.ErrDbQuery
|
||||
}
|
||||
|
||||
if !pwd.CompareHashAndPassword(u.Password, password) {
|
||||
return nil, ErrAccount
|
||||
}
|
||||
|
||||
return u, nil
|
||||
}
|
||||
Reference in new issue
Block a user