add password restore handler
This commit is contained in:
parent
f7096afaa5
commit
8f16fe84f2
27
cmd/backend/server/handlers/user_restore_password.go
Normal file
27
cmd/backend/server/handlers/user_restore_password.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"backend/internal/core/services"
|
||||||
|
httpserver "backend/internal/http_server"
|
||||||
|
"backend/pkg/logger"
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
type inputRestorePassword struct {
|
||||||
|
Token string `json:"token"`
|
||||||
|
NewPassword string `json:"password"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUserRestorePasswordHandler(log logger.Logger, userService services.UserService) gin.HandlerFunc {
|
||||||
|
return httpserver.WrapGin(log,
|
||||||
|
func(ctx context.Context, input inputRestorePassword) (interface{}, error) {
|
||||||
|
err := userService.ChangePasswordWithToken(ctx, input.Token, input.NewPassword)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
23
cmd/backend/server/handlers/user_send_restore_password.go
Normal file
23
cmd/backend/server/handlers/user_send_restore_password.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"backend/internal/core/services"
|
||||||
|
httpserver "backend/internal/http_server"
|
||||||
|
"backend/pkg/logger"
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
type inputSendRestorePassword struct {
|
||||||
|
Email string `json:"email" validate:"required,email"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUserSendRestorePasswordHandler(log logger.Logger, userService services.UserService) gin.HandlerFunc {
|
||||||
|
return httpserver.WrapGin(log,
|
||||||
|
func(ctx context.Context, input inputSendRestorePassword) (interface{}, error) {
|
||||||
|
err := userService.SendEmailForgotPassword(ctx, input.Email)
|
||||||
|
return nil, err
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
23
cmd/backend/server/handlers/user_send_verify.go
Normal file
23
cmd/backend/server/handlers/user_send_verify.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"backend/internal/core/services"
|
||||||
|
httpserver "backend/internal/http_server"
|
||||||
|
"backend/pkg/logger"
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
type inputSendVerify struct {
|
||||||
|
Email string `json:"email" validate:"required,email"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUserSendVerifyEmailHandler(log logger.Logger, userService services.UserService) gin.HandlerFunc {
|
||||||
|
return httpserver.WrapGin(log,
|
||||||
|
func(ctx context.Context, input inputSendVerify) (interface{}, error) {
|
||||||
|
err := userService.SendEmailVerifyEmail(ctx, input.Email)
|
||||||
|
return nil, err
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
@ -2,9 +2,7 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"backend/internal/core/services"
|
"backend/internal/core/services"
|
||||||
httpserver "backend/internal/http_server"
|
|
||||||
"backend/pkg/logger"
|
"backend/pkg/logger"
|
||||||
"context"
|
|
||||||
|
|
||||||
"html/template"
|
"html/template"
|
||||||
|
|
||||||
@ -71,16 +69,3 @@ func NewUserVerifyEmailHandler(log logger.Logger, userService services.UserServi
|
|||||||
c.Status(200)
|
c.Status(200)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type inputSendVerify struct {
|
|
||||||
Email string `json:"email" validate:"required,email"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewUserSendVerifyEmailHandler(log logger.Logger, userService services.UserService) gin.HandlerFunc {
|
|
||||||
return httpserver.WrapGin(log,
|
|
||||||
func(ctx context.Context, input inputSendVerify) (interface{}, error) {
|
|
||||||
err := userService.SendEmailVerifyEmail(ctx, input.Email)
|
|
||||||
return nil, err
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@ -37,7 +37,7 @@ type UserService interface {
|
|||||||
SendEmailVerifyEmail(ctx context.Context, email string) error
|
SendEmailVerifyEmail(ctx context.Context, email string) error
|
||||||
|
|
||||||
ChangePassword(ctx context.Context, userId, oldPassword, newPassword string) error
|
ChangePassword(ctx context.Context, userId, oldPassword, newPassword string) error
|
||||||
ChangePasswordWithToken(ctx context.Context, userId, actionToken, newPassword string) error
|
ChangePasswordWithToken(ctx context.Context, actionToken, newPassword string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUserService(deps UserServiceDeps) UserService {
|
func NewUserService(deps UserServiceDeps) UserService {
|
||||||
@ -141,7 +141,7 @@ func (u *userService) VerifyEmail(ctx context.Context, actionToken string) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := u.deps.UserRepo.SetUserEmailVerified(ctx, token.UserId); err != nil {
|
if err := u.deps.UserRepo.SetUserEmailVerified(ctx, token.UserId); err != nil {
|
||||||
return nil
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: log warnings somehow
|
//TODO: log warnings somehow
|
||||||
@ -205,12 +205,7 @@ func (u *userService) SendEmailVerifyEmail(ctx context.Context, email string) er
|
|||||||
return u.sendEmailVerifyEmail(ctx, user.Id, user.Email)
|
return u.sendEmailVerifyEmail(ctx, user.Id, user.Email)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *userService) ChangePasswordWithToken(ctx context.Context, userId, actionToken, newPassword string) error {
|
func (u *userService) ChangePasswordWithToken(ctx context.Context, actionToken, newPassword string) error {
|
||||||
user, err := u.getUserById(ctx, userId)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
token, err := u.deps.ActionTokenRepo.GetActionToken(ctx, actionToken, models.ActionTokenTargetForgotPassword)
|
token, err := u.deps.ActionTokenRepo.GetActionToken(ctx, actionToken, models.ActionTokenTargetForgotPassword)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -219,6 +214,14 @@ func (u *userService) ChangePasswordWithToken(ctx context.Context, userId, actio
|
|||||||
return fmt.Errorf("wrong action token")
|
return fmt.Errorf("wrong action token")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
user, err := u.getUserById(ctx, token.UserId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if user == nil {
|
||||||
|
return fmt.Errorf("no such user")
|
||||||
|
}
|
||||||
|
|
||||||
if err := u.updatePassword(ctx, *user, newPassword); err != nil {
|
if err := u.updatePassword(ctx, *user, newPassword); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,7 +12,7 @@ type Handler[Input, Output interface{}] func(ctx context.Context, input Input) (
|
|||||||
|
|
||||||
type ResponseOk struct {
|
type ResponseOk struct {
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Result interface{} `json:"result"`
|
Result interface{} `json:"result,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResponseError struct {
|
type ResponseError struct {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user