diff options
| author | Bobby <[email protected]> | 2025-12-22 12:41:27 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2025-12-22 12:41:27 +0530 |
| commit | f17d9142781eb1a659ea53311d9225b244ad209c (patch) | |
| tree | 1b03e1f96cca8559ad1a7e37e74279136a3bf157 | |
| parent | ed74f2cffa902a3e9aeb614f8dcf65f04658a597 (diff) | |
| download | lain-f17d9142781eb1a659ea53311d9225b244ad209c.tar.xz lain-f17d9142781eb1a659ea53311d9225b244ad209c.zip | |
better auth and error handler setup
| -rw-r--r-- | controllers/errors.go | 9 | ||||
| -rw-r--r-- | lain/main.go | 6 | ||||
| -rw-r--r-- | middleware/middleware.go | 2 | ||||
| -rw-r--r-- | router/base.go | 13 | ||||
| -rw-r--r-- | router/router.go | 23 | ||||
| -rw-r--r-- | utils/auth/auth.go | 9 | ||||
| -rw-r--r-- | utils/shortcuts/redirect.go | 6 |
7 files changed, 63 insertions, 5 deletions
diff --git a/controllers/errors.go b/controllers/errors.go index 019083a..8af9b22 100644 --- a/controllers/errors.go +++ b/controllers/errors.go @@ -16,6 +16,15 @@ func BadRequest(context *fiber.Ctx, err error) error { }) } +func DefaultError(context *fiber.Ctx, err error) error { + return shortcuts.RenderError(types.TemplateError{ + Context: context, + PageTitle: "Error", + ErrorMessage: shortcuts.BuildErrorMessage(err, "An error occurred while processing your request."), + StatusCode: fiber.StatusInternalServerError, + }) +} + func Forbidden(context *fiber.Ctx, err error) error { return shortcuts.RenderError(types.TemplateError{ Context: context, diff --git a/lain/main.go b/lain/main.go index a1606b8..b659cd0 100644 --- a/lain/main.go +++ b/lain/main.go @@ -27,10 +27,8 @@ func main() { engine := django.New("./templates", ".django") engine.Reload(config.Server.DevMode) app := fiber.New(fiber.Config{ - Views: engine, - ErrorHandler: func(ctx *fiber.Ctx, err error) error { - return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error") - }, // Will be extracted to a separate file later + Views: engine, + ErrorHandler: router.ErrorHandler, }) app.Use(logger.New()) diff --git a/middleware/middleware.go b/middleware/middleware.go index c8749bb..30e376e 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -3,5 +3,5 @@ package middleware import "github.com/gofiber/fiber/v2" func Initialize(app *fiber.App) { - app.Use(authentication) + // app.Use(authentication) } diff --git a/router/base.go b/router/base.go new file mode 100644 index 0000000..a5e8e96 --- /dev/null +++ b/router/base.go @@ -0,0 +1,13 @@ +package router + +import ( + "lain/types" + "lain/utils/shortcuts" + "lain/utils/urls" +) + +func init() { + urls.SetNamespace("") + + urls.Path(types.GET, "/", shortcuts.RedirectTo("auth.login"), "home") +} diff --git a/router/router.go b/router/router.go index e5c9303..62a7576 100644 --- a/router/router.go +++ b/router/router.go @@ -1,6 +1,7 @@ package router import ( + "lain/controllers" "lain/utils/urls" "github.com/gofiber/fiber/v2" @@ -11,3 +12,25 @@ func Initialize(router *fiber.App) { urls.Attach(router) } + +func ErrorHandler(ctx *fiber.Ctx, err error) error { + code := fiber.StatusInternalServerError + if e, ok := err.(*fiber.Error); ok { + code = e.Code + } + + switch code { + case fiber.StatusBadRequest: + return controllers.BadRequest(ctx, err) + case fiber.StatusForbidden: + return controllers.Forbidden(ctx, err) + case fiber.StatusInternalServerError: + return controllers.InternalServerError(ctx, err) + case fiber.StatusNotFound: + return controllers.NotFound(ctx, err) + case fiber.StatusUnauthorized: + return controllers.Unauthorized(ctx, err) + default: + return controllers.DefaultError(ctx, err) + } +} diff --git a/utils/auth/auth.go b/utils/auth/auth.go index 4b0fbff..8fba2d2 100644 --- a/utils/auth/auth.go +++ b/utils/auth/auth.go @@ -15,3 +15,12 @@ func IsAuthenticated(context *fiber.Ctx) bool { email := session.Get("email") return email != nil } + +func RequireAuthentication(handler fiber.Handler) fiber.Handler { + return func(context *fiber.Ctx) error { + if !IsAuthenticated(context) { + return fiber.ErrUnauthorized + } + return handler(context) + } +} diff --git a/utils/shortcuts/redirect.go b/utils/shortcuts/redirect.go index aa760dc..77e1959 100644 --- a/utils/shortcuts/redirect.go +++ b/utils/shortcuts/redirect.go @@ -21,3 +21,9 @@ func RedirectWithStatus(ctx *fiber.Ctx, routeName string, statusCode int) error } return ctx.Redirect(path, statusCode) } + +func RedirectTo(route string) fiber.Handler { + return func(ctx *fiber.Ctx) error { + return Redirect(ctx, route) + } +} |
