#615·goth

在 Oauth 回调期间, Goth 返回错误 "无法找到与该请求匹配的会话"。

作者: Cprakhar创建于 2025年8月3日更新于 2025年8月25日

func NewRouter(cfg *config.Config) *gin.Engine { router := gin.Default()

var sameSite http.SameSite
switch cfg.Env.SameSite {
case "lax":
	sameSite = http.SameSiteLaxMode
case "strict":
	sameSite = http.SameSiteStrictMode
case "none":
	sameSite = http.SameSiteNoneMode
default:
	sameSite = http.SameSiteLaxMode // Default to Lax if not specified
}

store := cookie.NewStore([]byte(cfg.Env.SessionSecret))
store.Options(sessions.Options{
	Path: "/",
	MaxAge: int(cfg.Env.SessionMaxAge),
	HttpOnly: true,
	Secure: cfg.Env.SessionSecure,
	SameSite: sameSite,
})
gothic.Store = store

cors := cors.New(cors.Config{
	AllowOrigins:     []string{cfg.Env.FrontendBaseURL},
	AllowMethods:     []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
	AllowHeaders:     []string{"Origin", "Content-Type", "Accept", "Authorization"},
	ExposeHeaders:    []string{"Content-Length"},
	AllowCredentials: true,
	MaxAge:           12 * time.Hour,
})

router.Use(sessions.Sessions(gothic.SessionName, store))
router.Use(cors)

h := &handlers.Handler{Cfg: cfg}

h.InitProviders()

api := router.Group("/api/v1")

api.GET("/health", h.HandleHealthCheck)
api.POST("/auth/register", h.HandleRegister)
api.GET("/auth/oauth/signin", h.HandleOAuthSignIn)
api.GET("/auth/oauth/callback", h.HandleOAuthCallback)
api.POST("/auth/login", h.HandleLogin)
api.POST("/auth/logout", middleware.RequireAuth(), h.HandleLogout)
api.GET("/auth/me", middleware.RequireAuth(), h.HandleMe)
api.GET("/connections", middleware.RequireAuth(), h.HandleGetConnections)
api.GET("/connections/:id", middleware.RequireAuth(), h.HandleGetConnection)
api.POST("/connections/ping", middleware.RequireAuth(), h.HandlePingConnection)
api.POST("/connections", middleware.RequireAuth(), h.HandleCreateConnection)
api.POST("/connections/:id/activate", middleware.RequireAuth(), h.HandleActivateConnection)
api.DELETE("/connections/:id/deactivate", middleware.RequireAuth(), h.HandleDeactivateConnection)
api.DELETE("/connections/:id", middleware.RequireAuth(), h.HandleDeleteConnection)

}