migrate to fiber
This commit is contained in:
@@ -4,19 +4,19 @@ import (
|
||||
"filething/models"
|
||||
"net/http"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
func AdminMiddleware() echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
user := c.Get("user").(*models.User)
|
||||
func AdminMiddleware(db *bun.DB) func(c fiber.Ctx) error {
|
||||
return func(c fiber.Ctx) error {
|
||||
user := c.Locals("user").(*models.User)
|
||||
|
||||
if !user.Admin {
|
||||
return echo.NewHTTPError(http.StatusForbidden, "You are not an administrator")
|
||||
}
|
||||
|
||||
return next(c)
|
||||
if !user.Admin {
|
||||
return echo.NewHTTPError(http.StatusForbidden, "You are not an administrator")
|
||||
}
|
||||
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,61 +7,56 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/google/uuid"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
const UserContextKey = "user"
|
||||
|
||||
func SessionMiddleware(db *bun.DB) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
// Extract sessionToken from the cookie
|
||||
cookie, err := c.Cookie("sessionToken")
|
||||
if err != nil {
|
||||
if err == http.ErrNoCookie {
|
||||
return echo.NewHTTPError(http.StatusUnauthorized, "Session token missing")
|
||||
}
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Bad request")
|
||||
}
|
||||
|
||||
sessionId, err := uuid.Parse(cookie.Value)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Bad request")
|
||||
}
|
||||
|
||||
session := &models.Session{
|
||||
ID: sessionId,
|
||||
}
|
||||
err = db.NewSelect().Model(session).WherePK().Scan(context.Background())
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
if err == sql.ErrNoRows {
|
||||
return echo.NewHTTPError(http.StatusUnauthorized, "Invalid session token")
|
||||
}
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Database error")
|
||||
}
|
||||
|
||||
user := &models.User{
|
||||
ID: session.UserID,
|
||||
}
|
||||
err = db.NewSelect().Model(user).Relation("Plan").WherePK().Scan(context.Background())
|
||||
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return echo.NewHTTPError(http.StatusUnauthorized, "Invalid session token")
|
||||
}
|
||||
fmt.Println(err)
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "Database error")
|
||||
}
|
||||
|
||||
// Store the user in the context
|
||||
c.Set(UserContextKey, user)
|
||||
|
||||
// Continue to the next handler
|
||||
return next(c)
|
||||
func SessionMiddleware(db *bun.DB) func(c fiber.Ctx) error {
|
||||
return func(c fiber.Ctx) error {
|
||||
// Extract session token from the cookie
|
||||
sessionToken := c.Cookies("sessionToken")
|
||||
if sessionToken == "" {
|
||||
return c.Status(http.StatusUnauthorized).JSON(fiber.Map{"message": "Session token missing"})
|
||||
}
|
||||
|
||||
// Parse session ID
|
||||
sessionId, err := uuid.Parse(sessionToken)
|
||||
if err != nil {
|
||||
return c.Status(http.StatusBadRequest).JSON(fiber.Map{"message": "Invalid session token"})
|
||||
}
|
||||
|
||||
// Fetch session from database
|
||||
session := &models.Session{
|
||||
ID: sessionId,
|
||||
}
|
||||
err = db.NewSelect().Model(session).WherePK().Scan(context.Background())
|
||||
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return c.Status(http.StatusUnauthorized).JSON(fiber.Map{"message": "Invalid session token"})
|
||||
}
|
||||
fmt.Println(err)
|
||||
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{"message": "Database error"})
|
||||
}
|
||||
|
||||
user := &models.User{
|
||||
ID: session.UserID,
|
||||
}
|
||||
err = db.NewSelect().Model(user).Relation("Plan").WherePK().Scan(context.Background())
|
||||
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return c.Status(http.StatusUnauthorized).JSON(fiber.Map{"message": "Invalid session token"})
|
||||
}
|
||||
fmt.Println(err)
|
||||
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{"message": "Database error"})
|
||||
}
|
||||
|
||||
c.Locals("user", user)
|
||||
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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