Last active
April 8, 2022 15:47
-
-
Save jsonw23/1f2cfeb573e50b38e8f79d28a949af9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package middleware | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"github.com/gin-contrib/sessions" | |
"github.com/gin-contrib/sessions/redis" | |
"github.com/gin-gonic/gin" | |
) | |
func InitAuthStore(r *gin.Engine) { | |
serviceDisc, deployed := os.LookupEnv("COPILOT_SERVICE_DISCOVERY_ENDPOINT") | |
var redisHost string | |
if deployed { | |
redisHost = fmt.Sprintf("redis.%s:6379", serviceDisc) | |
} else { | |
redisHost = "localhost:6379" | |
} | |
store, error := redis.NewStore(10, "tcp", | |
redisHost, "", | |
[]byte(os.Getenv("AUTH_CLIENT_SECRET"))) | |
if error != nil { | |
log.Panic(error) | |
} | |
r.Use(sessions.Sessions("auth", store)) | |
} | |
func Auth() gin.HandlerFunc { | |
return func(c *gin.Context) { | |
session := sessions.Default(c) | |
session.Set("test", "value") | |
session.Save() | |
c.Next() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment