37 lines
830 B
Go
Executable File
37 lines
830 B
Go
Executable File
package middlewares
|
|
|
|
import (
|
|
"net/url"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gookit/validate"
|
|
"github.com/sujit-baniya/fiber-boilerplate/pkg/auth"
|
|
"github.com/sujit-baniya/fiber-boilerplate/pkg/models"
|
|
)
|
|
|
|
func RedirectToHomePageOnLogin(c *fiber.Ctx) error {
|
|
if auth.IsLoggedIn(c) {
|
|
return c.Redirect("/app/dashboard")
|
|
}
|
|
return c.Next()
|
|
}
|
|
|
|
func ValidateLoginPost(c *fiber.Ctx) error {
|
|
var login models.Login
|
|
if err := c.BodyParser(&login); err != nil {
|
|
return c.Redirect("/login?error=" + url.QueryEscape(err.Error()))
|
|
}
|
|
v := validate.Struct(login)
|
|
if !v.Validate() {
|
|
return c.Redirect("/login?error=" + url.QueryEscape(v.Errors.One()))
|
|
}
|
|
|
|
user, err := login.CheckLogin()
|
|
if err != nil {
|
|
return c.Redirect("/login?error=" + url.QueryEscape(err.Error()))
|
|
}
|
|
|
|
c.Locals("user", user)
|
|
return c.Next()
|
|
}
|