add login
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
f2011b953c
commit
866009cf09
|
@ -28,7 +28,6 @@ func (s *Server) Ping(context.Context, *proto.PingReq) (*proto.PingRsp, error) {
|
|||
return &proto.PingRsp{}, nil
|
||||
}
|
||||
|
||||
// AddUser implements proto.SmmCoreServer.
|
||||
func (s *Server) AddUser(ctx context.Context, req *proto.AddUserReq) (*proto.User, error) {
|
||||
user, err := s.userService.AddUser(
|
||||
ctx,
|
||||
|
@ -46,9 +45,21 @@ func (s *Server) AddUser(ctx context.Context, req *proto.AddUserReq) (*proto.Use
|
|||
}, nil
|
||||
}
|
||||
|
||||
// Login implements proto.SmmCoreServer.
|
||||
func (s *Server) Login(context.Context, *proto.LoginReq) (*proto.User, error) {
|
||||
panic("unimplemented")
|
||||
func (s *Server) Login(ctx context.Context, req *proto.LoginReq) (*proto.User, error) {
|
||||
user, err := s.userService.Login(
|
||||
ctx,
|
||||
&user.UserEntity{
|
||||
Username: req.Username,
|
||||
Password: req.Password,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &proto.User{
|
||||
Id: int32(user.Id),
|
||||
Username: req.Username,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// AddBudget implements proto.SmmCoreServer.
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -24,6 +24,12 @@ func (e *UsernameAlreadyExistsErr) Error() string {
|
|||
return "username already exists error"
|
||||
}
|
||||
|
||||
type UserNotFoundErr struct{}
|
||||
|
||||
func (e *UserNotFoundErr) Error() string {
|
||||
return "user not found error"
|
||||
}
|
||||
|
||||
type UserService struct {
|
||||
db *pgxpool.Pool
|
||||
}
|
||||
|
@ -54,6 +60,18 @@ func (s *UserService) AddUser(ctx context.Context, user *UserEntity) (*UserEntit
|
|||
return user, nil
|
||||
}
|
||||
|
||||
func (s *UserService) Login(ctx context.Context, user *UserEntity) (*UserEntity, error) {
|
||||
query := `SELECT id FROM users WHERE username = @username AND password = @password`
|
||||
args := pgx.NamedArgs{
|
||||
"username": user.Username,
|
||||
"password": hashPassword(user.Username, user.Password, "crab"),
|
||||
}
|
||||
if err := s.db.QueryRow(ctx, query, args).Scan(&user.Id); err != nil {
|
||||
return nil, &UserNotFoundErr{}
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func hashPassword(username string, password string, salt string) string {
|
||||
return getMD5Hash(username + password + salt)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue