fix(core-backend): enable CORS for Flutter Web signup
Chrome treats localhost:5555 and 127.0.0.1:8080 as different origins. Handle OPTIONS preflight and emit Access-Control-Allow-* so /auth/signup works from flutter run -d chrome. Co-authored-by: okuma <o0kuma@users.noreply.github.com>
This commit is contained in:
parent
941d600cfd
commit
9abc09c2a6
|
|
@ -49,8 +49,32 @@ type draftMessageRequest struct {
|
||||||
K int `json:"k"`
|
K int `json:"k"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// corsMiddleware allows Flutter Web (and other local origins) to call the API.
|
||||||
|
// Browsers treat http://localhost:5555 and http://127.0.0.1:8080 as different
|
||||||
|
// origins, so Chrome signup fails without OPTIONS + Allow-Origin headers.
|
||||||
|
func corsMiddleware() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
origin := c.GetHeader("Origin")
|
||||||
|
if origin == "" {
|
||||||
|
origin = "*"
|
||||||
|
}
|
||||||
|
c.Header("Access-Control-Allow-Origin", origin)
|
||||||
|
c.Header("Vary", "Origin")
|
||||||
|
c.Header("Access-Control-Allow-Credentials", "true")
|
||||||
|
c.Header("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With")
|
||||||
|
c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS")
|
||||||
|
c.Header("Access-Control-Max-Age", "600")
|
||||||
|
if c.Request.Method == http.MethodOptions {
|
||||||
|
c.AbortWithStatus(http.StatusNoContent)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func setupRouter(db *gorm.DB, relay *ConnectionManager, ai *AIServiceClient) *gin.Engine {
|
func setupRouter(db *gorm.DB, relay *ConnectionManager, ai *AIServiceClient) *gin.Engine {
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
r.Use(corsMiddleware())
|
||||||
|
|
||||||
r.GET("/health", func(c *gin.Context) {
|
r.GET("/health", func(c *gin.Context) {
|
||||||
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue