add html template

This commit is contained in:
Sergey Chubaryan 2025-02-16 03:02:50 +03:00
parent 20aa4a3d7b
commit 8c1ced8e5a

View File

@ -4,48 +4,68 @@ import (
"backend/internal/core/services" "backend/internal/core/services"
"backend/pkg/logger" "backend/pkg/logger"
"html/template"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
type A struct { type HtmlTemplate struct {
TabTitle string
Title string Title string
Text string Text string
Link string Link string
LinkText string LinkText string
} }
func NewUserVerifyEmailHandler(log logger.Logger, userService services.UserService) gin.HandlerFunc { const htmlTemplate = `
htmlOk := ` <html>
<html> <head>
<head> <title>{{.TabTitle}}</title>
<title>Verify Email</title> </head>
</head> <body>
<body> {{if .Title}}
<h1>Email successfuly verified</h1> <h1>{{.Title}}</h1>
</body> {{end}}
</html>
`
htmlNotOk := ` <h3>{{.Text}}</h3>
<html> <head> <title>Verify Email</title> </head> <body>
<h1>Email was not verified</h1> {{if .Link}}
</body> </html> <a href="{{.Link}}">{{.LinkText}}</a>
` {{end}}
</body>
</html>
`
func NewUserVerifyEmailHandler(log logger.Logger, userService services.UserService) gin.HandlerFunc {
template, err := template.New("verify-email").Parse(htmlTemplate)
if err != nil {
log.Fatal().Err(err).Msg("Error parsing template")
}
return func(c *gin.Context) { return func(c *gin.Context) {
tmp := HtmlTemplate{
TabTitle: "Verify Email",
Text: "Error verifying email",
}
token, ok := c.GetQuery("token") token, ok := c.GetQuery("token")
if !ok || token == "" { if !ok || token == "" {
c.Data(400, "text/html", []byte(htmlNotOk)) log.Error().Err(err).Msg("No token in query param")
template.Execute(c.Writer, tmp)
c.Status(400)
return return
} }
err := userService.VerifyEmail(c, token) err := userService.VerifyEmail(c, token)
if err != nil { if err != nil {
log.Error().Err(err).Msg("Error verifying email") log.Error().Err(err).Msg("Error verifying email")
c.Data(400, "text/html", []byte(htmlNotOk)) template.Execute(c.Writer, tmp)
c.Status(400)
return return
} }
c.Data(200, "text/html", []byte(htmlOk)) tmp.Text = "Email successfully verified"
template.Execute(c.Writer, tmp)
c.Status(200)
} }
} }