41 lines
794 B
Go
41 lines
794 B
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const sessionCookie = "Atlas-Session"
|
|
|
|
func (s *Server) sessionMiddleware() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
sid, err := c.Cookie(sessionCookie)
|
|
if err != nil || sid == "" {
|
|
sid = uuid.New().String()
|
|
http.SetCookie(c.Writer, &http.Cookie{
|
|
Name: sessionCookie,
|
|
Value: sid,
|
|
Path: "/",
|
|
MaxAge: int((30 * 24 * time.Hour).Seconds()),
|
|
HttpOnly: true,
|
|
SameSite: http.SameSiteLaxMode,
|
|
})
|
|
}
|
|
_ = s.app.Store.UpsertSession(c.Request.Context(), sid)
|
|
c.Set("session_id", sid)
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
func sessionID(c *gin.Context) string {
|
|
if v, ok := c.Get("session_id"); ok {
|
|
if s, ok := v.(string); ok {
|
|
return s
|
|
}
|
|
}
|
|
return ""
|
|
}
|