Initial commit
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/sujit-baniya/fiber-boilerplate/utils"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gookit/validate"
|
||||
"github.com/sujit-baniya/fiber-boilerplate/app"
|
||||
"github.com/sujit-baniya/fiber-boilerplate/pkg/auth"
|
||||
"github.com/sujit-baniya/fiber-boilerplate/pkg/models"
|
||||
)
|
||||
|
||||
func ValidateRegisterPost(c *fiber.Ctx) error {
|
||||
// Declara el modelo RegisterForm
|
||||
var register models.RegisterForm
|
||||
|
||||
// Intenta parsear el cuerpo de la solicitud
|
||||
if err := c.BodyParser(®ister); err != nil {
|
||||
return app.Http.Flash.WithError(c, fiber.Map{
|
||||
"message": "Error al procesar los datos del formulario: " + err.Error(),
|
||||
}).Redirect("/register-gas")
|
||||
}
|
||||
|
||||
// Realiza la validación del formulario
|
||||
v := validate.Struct(register)
|
||||
if v == nil {
|
||||
return app.Http.Flash.WithError(c, fiber.Map{
|
||||
"message": "Error en la validación de los datos del formulario.",
|
||||
}).Redirect("/register-gas")
|
||||
}
|
||||
|
||||
// Verifica si hay errores de validación
|
||||
if !v.Validate() {
|
||||
errorMessage := "Error desconocido en los datos del formulario."
|
||||
if len(v.Errors) > 0 {
|
||||
errorMessage = v.Errors.One() // Obtiene el primer error si existe
|
||||
}
|
||||
return app.Http.Flash.WithError(c, fiber.Map{
|
||||
"message": errorMessage,
|
||||
}).Redirect("/register-gas")
|
||||
}
|
||||
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
func ValidateConfirmToken(c *fiber.Ctx) error {
|
||||
t := utils.Decrypt(c.Query("t"), app.Http.Server.Key)
|
||||
fmt.Println(t)
|
||||
user, err := models.GetUserByUsuarioVerify(t)
|
||||
if err != nil {
|
||||
return app.Http.Flash.WithError(c, fiber.Map{
|
||||
"message": err.Error(),
|
||||
}).Redirect("/login")
|
||||
}
|
||||
|
||||
if user.EmailVerified {
|
||||
return app.Http.Flash.WithError(c, fiber.Map{
|
||||
"message": "Usuario was already validated",
|
||||
}).Redirect("/login")
|
||||
}
|
||||
user.EmailVerified = true
|
||||
app.Http.Database.DB.Save(&user)
|
||||
|
||||
auth.Login(c, user.ID, app.Http.Server.Key) //nolint:wsl
|
||||
c.Locals("user", user)
|
||||
return c.Next()
|
||||
}
|
||||
Reference in New Issue
Block a user