40 lines
976 B
Go
40 lines
976 B
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"go-user-service/libs"
|
|
"go-user-service/models"
|
|
userpb "go-user-service/proto/gen"
|
|
"go-user-service/utils"
|
|
"sync"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
type UserService struct {
|
|
userpb.UnimplementedUserServiceServer
|
|
tokens map[string]string // token -> userID
|
|
mu sync.RWMutex
|
|
}
|
|
|
|
func (u *UserService) Login(ctx context.Context, req *userpb.LoginRequest) (*userpb.LoginResponse, error) {
|
|
u.mu.RLock()
|
|
defer u.mu.RUnlock()
|
|
// 查询是否有这个用户名
|
|
var user models.User
|
|
db := libs.GetDB()
|
|
db.Model(&models.User{}).Where("username = ?", req.Username).First(&user)
|
|
if user.Username != "" {
|
|
// 验证密码
|
|
var crypto utils.Crypto
|
|
hashPassword, _ := crypto.PasswordEncryption(user.Password + user.Salt)
|
|
|
|
if user.Password == hashPassword {
|
|
|
|
}
|
|
return nil, status.Errorf(codes.Unauthenticated, "密码错误")
|
|
}
|
|
return nil, status.Errorf(codes.NotFound, "用户不存在")
|
|
}
|