add html template
This commit is contained in:
parent
20aa4a3d7b
commit
8c1ced8e5a
@ -4,48 +4,68 @@ import (
|
||||
"backend/internal/core/services"
|
||||
"backend/pkg/logger"
|
||||
|
||||
"html/template"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type A struct {
|
||||
type HtmlTemplate struct {
|
||||
TabTitle string
|
||||
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>
|
||||
`
|
||||
const htmlTemplate = `
|
||||
<html>
|
||||
<head>
|
||||
<title>{{.TabTitle}}</title>
|
||||
</head>
|
||||
<body>
|
||||
{{if .Title}}
|
||||
<h1>{{.Title}}</h1>
|
||||
{{end}}
|
||||
|
||||
htmlNotOk := `
|
||||
<html> <head> <title>Verify Email</title> </head> <body>
|
||||
<h1>Email was not verified</h1>
|
||||
</body> </html>
|
||||
`
|
||||
<h3>{{.Text}}</h3>
|
||||
|
||||
{{if .Link}}
|
||||
<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) {
|
||||
tmp := HtmlTemplate{
|
||||
TabTitle: "Verify Email",
|
||||
Text: "Error verifying email",
|
||||
}
|
||||
|
||||
token, ok := c.GetQuery("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
|
||||
}
|
||||
|
||||
err := userService.VerifyEmail(c, token)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Error verifying email")
|
||||
c.Data(400, "text/html", []byte(htmlNotOk))
|
||||
template.Execute(c.Writer, tmp)
|
||||
c.Status(400)
|
||||
return
|
||||
}
|
||||
|
||||
c.Data(200, "text/html", []byte(htmlOk))
|
||||
tmp.Text = "Email successfully verified"
|
||||
template.Execute(c.Writer, tmp)
|
||||
c.Status(200)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user