generated from VLADIMIR/template
add refresh-password route
This commit is contained in:
+12
-2
@@ -28,8 +28,8 @@ func (s *server) Echo(_ context.Context, req *proto.EchoReq) (*proto.EchoRsp, er
|
||||
return &proto.EchoRsp{Text: req.Text}, nil
|
||||
}
|
||||
|
||||
func (s *server) Signup(ctx context.Context, signupReq *proto.SignupReq) (*proto.SignupRsp, error) {
|
||||
err := s.usersService.AddUser(ctx, signupReq.Username, signupReq.Email)
|
||||
func (s *server) Signup(ctx context.Context, req *proto.SignupReq) (*proto.SignupRsp, error) {
|
||||
err := s.usersService.AddUser(ctx, req.Username, req.Email)
|
||||
if err != nil {
|
||||
return &proto.SignupRsp{
|
||||
Error: err.Error(),
|
||||
@@ -37,3 +37,13 @@ func (s *server) Signup(ctx context.Context, signupReq *proto.SignupReq) (*proto
|
||||
}
|
||||
return &proto.SignupRsp{}, nil
|
||||
}
|
||||
|
||||
func (s *server) RefreshPassword(ctx context.Context, req *proto.RefreshPasswordReq) (*proto.RefreshPasswordRsp, error) {
|
||||
err := s.usersService.RefreshPassword(ctx, req.Email)
|
||||
if err != nil {
|
||||
return &proto.RefreshPasswordRsp{
|
||||
Error: err.Error(),
|
||||
}, nil
|
||||
}
|
||||
return &proto.RefreshPasswordRsp{}, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package repos
|
||||
|
||||
type User struct {
|
||||
Username string
|
||||
Email string
|
||||
}
|
||||
@@ -7,6 +7,10 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrUserNotFound = errors.New("Пользователь не найден")
|
||||
)
|
||||
|
||||
type UsersRepo struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
@@ -21,7 +25,7 @@ func (s *UsersRepo) AddUser(
|
||||
ctx context.Context,
|
||||
username string,
|
||||
email string,
|
||||
passwoedHash string,
|
||||
passwordHash string,
|
||||
role string,
|
||||
) error {
|
||||
tag, err := s.pool.Exec(
|
||||
@@ -34,7 +38,7 @@ func (s *UsersRepo) AddUser(
|
||||
)`,
|
||||
username,
|
||||
email,
|
||||
passwoedHash,
|
||||
passwordHash,
|
||||
role,
|
||||
|
||||
username,
|
||||
@@ -48,3 +52,26 @@ func (s *UsersRepo) AddUser(
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *UsersRepo) UpdateUserPassword(
|
||||
ctx context.Context,
|
||||
email string,
|
||||
passwordHash string,
|
||||
) error {
|
||||
tag, err := s.pool.Exec(
|
||||
ctx,
|
||||
`UPDATE users
|
||||
SET password_hash = $1
|
||||
WHERE email = $2`,
|
||||
passwordHash,
|
||||
email,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return ErrUserNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -60,3 +60,34 @@ func (s *UsersService) AddUser(
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *UsersService) RefreshPassword(
|
||||
ctx context.Context,
|
||||
email string,
|
||||
) error {
|
||||
password, err := s.passwordGenerator.Generate()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = s.usersRepo.UpdateUserPassword(
|
||||
ctx,
|
||||
email,
|
||||
string(hashedPassword),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = s.emailSender.Send(ctx, email_sender.Message{
|
||||
To: email,
|
||||
Subject: "Приветствую тебя, детектив!",
|
||||
Body: fmt.Sprintf("Вот твой новый пароль для входа в систему, не теряй: %s", password),
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user