szoe-pontok/api/auth/middleware.go
2024-10-10 22:27:01 +02:00

57 lines
976 B
Go

package auth
import (
"net/http"
"git.tek.govt.hu/dowerx/szoe-pontok/database/auth"
"github.com/gin-gonic/gin"
)
func LoggedIn(c *gin.Context) {
token, err := c.Cookie("token")
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{
"status": http.StatusUnauthorized,
"error": "missing token",
})
c.Abort()
return
}
neptun, err := auth.LoggedIn(token)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{
"status": http.StatusUnauthorized,
"error": "not logged in",
})
c.Abort()
return
}
c.Set("neptun", neptun)
}
func IsAdmin(c *gin.Context) {
neptun, exists := c.Get("neptun")
if !exists {
c.JSON(http.StatusUnauthorized, gin.H{
"status": http.StatusUnauthorized,
"error": "not logged in",
})
c.Abort()
return
}
err := auth.IsAdmin(neptun.(string))
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{
"status": http.StatusUnauthorized,
"error": "not an admin",
})
c.Abort()
return
}
}