2024-01-16 00:36:49 +01:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"azote-backend/tokens"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func RequireAuth(c *gin.Context) {
|
|
|
|
|
|
|
|
session, err := token.ParseToken(c)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatus(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if time.Now().Unix() > session.ExpiresAt.Unix() {
|
|
|
|
c.AbortWithStatus(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-18 23:45:06 +01:00
|
|
|
c.Set("userId", session.Bearer.String())
|
2024-01-16 00:36:49 +01:00
|
|
|
c.Next()
|
|
|
|
}
|