This commit is contained in:
2026-09-15 09:15:53 -05:00
parent 97c930196a
commit ab9573b875
21 changed files with 1664 additions and 0 deletions
+59
View File
@@ -0,0 +1,59 @@
package v1
import (
"log"
"net/http"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"jp.wtf/go-fishing/internal/api"
"jp.wtf/go-fishing/internal/db"
)
type V1Router struct {
DB *db.Database
}
func (h *V1Router) AnglerAuthMiddleware(c *gin.Context) {
s := sessions.Default(c)
val := s.Get("angler_id")
if val == nil || val == "" {
api.FailAndAbort(c, http.StatusUnauthorized, api.APIErrorCodeUnauthorized, "You are not logged in!")
return
}
valStr, ok := val.(string)
angler_id, uuid_err := uuid.Parse(valStr)
if uuid_err != nil || !ok {
s.Clear()
s.Save()
api.FailAndAbort(c, http.StatusInternalServerError, api.APIErrorCodeUnknown, "Session data malformed")
return
}
angler, err := h.DB.GetAnglerByID(angler_id)
if err != nil {
s.Clear()
s.Save()
log.Print("Failed to find angler: ", err.Error())
api.FailAndAbort(c, http.StatusUnauthorized, api.APIErrorCodeUnauthorized, "Bad or expired session")
return
}
c.Set("angler", angler)
s.Save()
}
func (h *V1Router) RegisterRoutes(rg *gin.RouterGroup) {
auth := rg.Group("/auth")
auth.POST("/login", h.Login)
auth.POST("/signup", h.Signup)
auth.POST("/logout", h.Logout)
auth.GET("/me", h.GetMe)
rg.GET("/cast", h.AnglerAuthMiddleware, h.Cast)
}