Add dashboard for authenticated users with JWT check and static files

This commit is contained in:
2026-01-22 21:13:31 -05:00
parent e5ca12117c
commit 3d325c71a2
3 changed files with 497 additions and 0 deletions
+31
View File
@@ -8,9 +8,12 @@ import (
"io"
"log"
"net/http"
"strings"
"github.com/go-playground/validator/v10"
jwtgo "github.com/golang-jwt/jwt"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
@@ -83,8 +86,36 @@ func main() {
// Routes
handlers.SetupRoutes(protected, queries)
// Static files
e.Static("/static", "static")
// Page routes
e.GET("/", func(c echo.Context) error {
// Check if user has valid JWT token
tokenString := c.Request().Header.Get("Authorization")
if tokenString != "" && strings.HasPrefix(tokenString, "Bearer ") {
tokenString = strings.TrimPrefix(tokenString, "Bearer ")
token, err := jwtgo.Parse(tokenString, func(token *jwtgo.Token) (interface{}, error) {
return []byte(cfg.JWTSecret), nil
})
if err == nil && token.Valid {
// User is authenticated, get user info and serve dashboard
claims := token.Claims.(jwtgo.MapClaims)
userID := claims["user_id"].(string)
user, err := queries.GetUser(c.Request().Context(), pgtype.UUID{Bytes: uuid.MustParse(userID), Valid: true})
if err == nil {
return c.Render(http.StatusOK, "dashboard.html", map[string]interface{}{
"User": map[string]string{
"ID": uuid.UUID(user.ID.Bytes).String(),
"Username": user.Username,
"Email": user.Email,
},
})
}
}
}
// User not authenticated or token invalid, serve landing page
return c.Render(http.StatusOK, "index.html", nil)
})
e.GET("/login", func(c echo.Context) error {