generated from VLADIMIR/template
add login route
This commit is contained in:
@@ -47,3 +47,13 @@ func (s *server) RefreshPassword(ctx context.Context, req *proto.RefreshPassword
|
||||
}
|
||||
return &proto.RefreshPasswordRsp{}, nil
|
||||
}
|
||||
|
||||
func (s *server) Login(ctx context.Context, req *proto.LoginReq) (*proto.LoginRsp, error) {
|
||||
err := s.usersService.Login(ctx, req.Email, req.Password)
|
||||
if err != nil {
|
||||
return &proto.LoginRsp{
|
||||
Error: err.Error(),
|
||||
}, nil
|
||||
}
|
||||
return &proto.LoginRsp{}, nil
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package repos
|
||||
|
||||
type User struct {
|
||||
Username string
|
||||
Email string
|
||||
ID string
|
||||
Username string
|
||||
Email string
|
||||
PasswordHash string
|
||||
Role string
|
||||
IsActive bool
|
||||
}
|
||||
|
||||
@@ -3,7 +3,9 @@ package users_repo
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"evening_detective_server/internal/repos"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
@@ -75,3 +77,33 @@ func (s *UsersRepo) UpdateUserPassword(
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *UsersRepo) GetUserByEmail(
|
||||
ctx context.Context,
|
||||
email string,
|
||||
) (*repos.User, error) {
|
||||
user := &repos.User{}
|
||||
row := s.pool.QueryRow(
|
||||
ctx,
|
||||
`SELECT id, username, email, password_hash, role, is_active
|
||||
FROM users
|
||||
WHERE email = $1`,
|
||||
email,
|
||||
)
|
||||
err := row.Scan(
|
||||
&user.ID,
|
||||
&user.Username,
|
||||
&user.Email,
|
||||
&user.PasswordHash,
|
||||
&user.Role,
|
||||
&user.IsActive,
|
||||
)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrUserNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package users_service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"evening_detective_server/internal/modules/email_sender"
|
||||
"evening_detective_server/internal/modules/password_generator"
|
||||
"evening_detective_server/internal/repos/users_repo"
|
||||
@@ -91,3 +92,29 @@ func (s *UsersService) RefreshPassword(
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *UsersService) Login(
|
||||
ctx context.Context,
|
||||
email string,
|
||||
password string,
|
||||
) error {
|
||||
user, err := s.usersRepo.GetUserByEmail(
|
||||
ctx,
|
||||
email,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(password)); err != nil {
|
||||
return errors.New("Не верен email или пароль")
|
||||
}
|
||||
|
||||
fmt.Println(user, err)
|
||||
|
||||
// gen tokens (module)
|
||||
|
||||
// save refresh token?
|
||||
|
||||
return nil // claims?
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user