clortho/apis/auth_middleware.go
Maxime Duchene-Savard 5a23f5ecfe first commit
2025-03-30 22:44:56 -04:00

39 lines
795 B
Go

package apis
import (
"clortho/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()
}
}