add verify-email handler

This commit is contained in:
Sergey Chubaryan 2025-02-15 12:45:57 +03:00
parent a56b0eed56
commit 20aa4a3d7b
4 changed files with 60 additions and 5 deletions

View File

@ -1,5 +1,5 @@
port: 8080 port: 8080
postgres_url: "postgres://postgres:postgres@localhost:5432/postgres" postgres_url: "postgres://postgres:postgres@localhost:5432/postgres"
jwt_signing_key: "./config_defaults/jwt_signing_key" jwt_signing_key: "./jwt_signing_key"
kafka_url: "localhost:9092" kafka_url: "localhost:9092"
kafka_topic: "backend_events" kafka_topic: "backend_events"

View File

@ -0,0 +1,51 @@
package handlers
import (
"backend/internal/core/services"
"backend/pkg/logger"
"github.com/gin-gonic/gin"
)
type A struct {
Title string
Text string
Link string
LinkText string
}
func NewUserVerifyEmailHandler(log logger.Logger, userService services.UserService) gin.HandlerFunc {
htmlOk := `
<html>
<head>
<title>Verify Email</title>
</head>
<body>
<h1>Email successfuly verified</h1>
</body>
</html>
`
htmlNotOk := `
<html> <head> <title>Verify Email</title> </head> <body>
<h1>Email was not verified</h1>
</body> </html>
`
return func(c *gin.Context) {
token, ok := c.GetQuery("token")
if !ok || token == "" {
c.Data(400, "text/html", []byte(htmlNotOk))
return
}
err := userService.VerifyEmail(c, token)
if err != nil {
log.Error().Err(err).Msg("Error verifying email")
c.Data(400, "text/html", []byte(htmlNotOk))
return
}
c.Data(200, "text/html", []byte(htmlOk))
}
}

View File

@ -39,17 +39,21 @@ func NewServer(opts NewServerOpts) *httpserver.Server {
r.Use(httpserver.NewRequestLogMiddleware(opts.Logger, opts.Tracer, prometheus)) r.Use(httpserver.NewRequestLogMiddleware(opts.Logger, opts.Tracer, prometheus))
r.Use(httpserver.NewTracingMiddleware(opts.Tracer)) r.Use(httpserver.NewTracingMiddleware(opts.Tracer))
v1 := r.Group("/v1") r.GET("/verify-user", handlers.NewUserVerifyEmailHandler(opts.Logger, opts.UserService))
api := r.Group("/api")
v1 := api.Group("/v1")
userGroup := v1.Group("/user") userGroup := v1.Group("/user")
{ {
userGroup.POST("/create", handlers.NewUserCreateHandler(opts.Logger, opts.UserService)) userGroup.POST("/create", handlers.NewUserCreateHandler(opts.Logger, opts.UserService))
userGroup.POST("/login", handlers.NewUserLoginHandler(opts.Logger, opts.UserService)) userGroup.POST("/login", handlers.NewUserLoginHandler(opts.Logger, opts.UserService))
} }
dummyGroup := v1.Group("/dummy") dummyGroup := v1.Group("/dummy")
{
dummyGroup.Use(middleware.NewAuthMiddleware(opts.UserService)) dummyGroup.Use(middleware.NewAuthMiddleware(opts.UserService))
{
dummyGroup.GET("", handlers.NewDummyHandler()) dummyGroup.GET("", handlers.NewDummyHandler())
dummyGroup.POST("/forgot-password", func(c *gin.Context) { dummyGroup.POST("/forgot-password", func(c *gin.Context) {
user := utils.GetUserFromRequest(c) user := utils.GetUserFromRequest(c)

View File

@ -50,7 +50,7 @@ func (a *actionTokenRepo) GetActionToken(ctx context.Context, value string, targ
query := ` query := `
select id, user_id from action_tokens select id, user_id from action_tokens
where where
value=$2 and target=$3 value=$1 and target=$2
and CURRENT_TIMESTAMP < expiration;` and CURRENT_TIMESTAMP < expiration;`
row := a.db.QueryRowContext(ctx, query, value, target) row := a.db.QueryRowContext(ctx, query, value, target)
@ -67,7 +67,7 @@ func (a *actionTokenRepo) GetActionToken(ctx context.Context, value string, targ
func (a *actionTokenRepo) DeleteActionToken(ctx context.Context, id string) error { func (a *actionTokenRepo) DeleteActionToken(ctx context.Context, id string) error {
query := `delete from action_tokens where id=$1;` query := `delete from action_tokens where id=$1;`
if _, err := a.db.ExecContext(ctx, query); err != nil { if _, err := a.db.ExecContext(ctx, query, id); err != nil {
return err return err
} }
return nil return nil