migrate to fiber
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
var unauthenticatedPages = []string{
|
||||
@@ -17,36 +16,34 @@ var authenticatedPages = []string{
|
||||
"/home",
|
||||
}
|
||||
|
||||
func AuthCheckMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
path := c.Request().URL.Path
|
||||
func AuthCheckMiddleware(c fiber.Ctx) error {
|
||||
path := c.Path()
|
||||
|
||||
// bypass auth checks for static and dev resources
|
||||
if strings.HasPrefix(path, "/_nuxt/") || strings.HasSuffix(path, ".js") || strings.HasSuffix(path, ".css") {
|
||||
return next(c)
|
||||
}
|
||||
|
||||
_, cookieErr := c.Cookie("sessionToken")
|
||||
authenticated := cookieErr == nil
|
||||
|
||||
if Contains(unauthenticatedPages, path) && authenticated {
|
||||
return c.Redirect(http.StatusFound, "/home")
|
||||
}
|
||||
|
||||
if Contains(authenticatedPages, path) && !authenticated {
|
||||
return c.Redirect(http.StatusFound, "/login")
|
||||
}
|
||||
|
||||
if strings.Contains(path, "/home") && !authenticated {
|
||||
return c.Redirect(http.StatusFound, "/login")
|
||||
}
|
||||
|
||||
if strings.Contains(path, "/admin") && !authenticated {
|
||||
return c.Redirect(http.StatusFound, "/login")
|
||||
}
|
||||
|
||||
return next(c)
|
||||
// bypass auth checks for static and dev resources
|
||||
if strings.HasPrefix(path, "/_nuxt/") || strings.HasSuffix(path, ".js") || strings.HasSuffix(path, ".css") {
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
cookie := c.Cookies("sessionToken")
|
||||
authenticated := cookie != ""
|
||||
|
||||
if Contains(unauthenticatedPages, path) && authenticated {
|
||||
return c.Redirect().To("/home")
|
||||
}
|
||||
|
||||
if Contains(authenticatedPages, path) && !authenticated {
|
||||
return c.Redirect().To("/login")
|
||||
}
|
||||
|
||||
if strings.Contains(path, "/home") && !authenticated {
|
||||
return c.Redirect().To("/login")
|
||||
}
|
||||
|
||||
if strings.Contains(path, "/admin") && !authenticated {
|
||||
return c.Redirect().To("/login")
|
||||
}
|
||||
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
func Contains(s []string, element string) bool {
|
||||
|
||||
Reference in New Issue
Block a user