init
This commit is contained in:
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user