szoe-pontok/api/auth/middleware.go

57 lines
976 B
Go
Raw Permalink Normal View History

2024-10-10 19:41:49 +00:00
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) {
2024-10-10 20:27:01 +00:00
neptun, exists := c.Get("neptun")
2024-10-10 19:41:49 +00:00
2024-10-10 20:27:01 +00:00
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
}
2024-10-10 19:41:49 +00:00
}