Initial commit
This commit is contained in:
20
internal/middleware/auth.go
Normal file
20
internal/middleware/auth.go
Normal file
@ -0,0 +1,20 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
jwtware "github.com/gofiber/contrib/jwt"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func errorHandler(c *fiber.Ctx, err error) error {
|
||||
c.Status(401)
|
||||
return err
|
||||
}
|
||||
|
||||
func Protected() fiber.Handler {
|
||||
return jwtware.New(jwtware.Config{
|
||||
SigningKey: jwtware.SigningKey{Key: []byte(os.Getenv("JWT_SECRET"))},
|
||||
ErrorHandler: errorHandler,
|
||||
})
|
||||
}
|
43
internal/middleware/error_hander.go
Normal file
43
internal/middleware/error_hander.go
Normal file
@ -0,0 +1,43 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type ProblemDetails struct {
|
||||
Type string
|
||||
Title string
|
||||
Status int
|
||||
Detail string
|
||||
}
|
||||
|
||||
func ErrorHandler() fiber.Handler {
|
||||
return func(c *fiber.Ctx) error {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
status := fiber.StatusInternalServerError
|
||||
c.Status(status).JSON(ProblemDetails{
|
||||
Type: fmt.Sprintf("https://httpstatuses.io/%d", status),
|
||||
Title: "Internal Server Error",
|
||||
Status: status,
|
||||
Detail: fmt.Sprint(r),
|
||||
})
|
||||
}
|
||||
}()
|
||||
|
||||
err := c.Next()
|
||||
if err != nil {
|
||||
status := c.Response().StatusCode()
|
||||
c.JSON(ProblemDetails{
|
||||
Type: fmt.Sprintf("https://httpstatuses.io/%d", status),
|
||||
Title: err.Error(),
|
||||
Status: status,
|
||||
Detail: "No further details available",
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user