Add bcrypt user file auth, JWT middleware on all write routes, Pinia authStore with localStorage persistence, login view with redirect support, and v-if guards on all CRUD controls and admin nav link. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
28 lines
755 B
Go
28 lines
755 B
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"osdb/internal/auth"
|
|
)
|
|
|
|
// JWTAuth returns an Echo middleware that requires a valid Bearer JWT.
|
|
func JWTAuth(secret string) echo.MiddlewareFunc {
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
header := c.Request().Header.Get("Authorization")
|
|
if !strings.HasPrefix(header, "Bearer ") {
|
|
return c.JSON(http.StatusUnauthorized, map[string]string{"error": "missing token"})
|
|
}
|
|
tokenStr := strings.TrimPrefix(header, "Bearer ")
|
|
if _, err := auth.ValidateJWT(tokenStr, secret); err != nil {
|
|
return c.JSON(http.StatusUnauthorized, map[string]string{"error": "invalid token"})
|
|
}
|
|
return next(c)
|
|
}
|
|
}
|
|
}
|