clortho/lib/apis/auth_middleware.go
Maxime Duchene-Savard ea2bb235a2 work
2025-04-14 23:50:22 -04:00

39 lines
799 B
Go

package apis
import (
"clortho/lib/users"
"github.com/gin-gonic/gin"
"net/http"
)
func AuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
// Get the token from the Authorization header
authCookie, err := c.Cookie("CLORTHO_AUTH")
if err != nil {
//c.JSON(http.StatusUnauthorized, gin.H{"error": "authorization token required"})
return
}
session, err := users.GetSessionFromCookie(authCookie)
if err != nil {
//c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
return
}
c.Set("session", session)
c.Next()
}
}
func LoggedInMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
_, hasSession := c.Get("session")
if !hasSession {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
return
}
c.Next()
}
}