39 lines
799 B
Go
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()
|
|
}
|
|
}
|