-
Notifications
You must be signed in to change notification settings - Fork 196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Different stores for many sessions #124
Comments
@dusansimic I have been playing with this in a fork as I needed exactly this. My use case is: Cookies for CSRF type SessionStore struct {
Name string
Store Store
}
func SessionsMany(sessionStores []SessionStore) gin.HandlerFunc {
return func(c *gin.Context) {
sessions := make(map[string]Session, len(sessionStores))
for _, sessionStore := range sessionStores {
sessions[sessionStore.Name] = &session{sessionStore.Name, c.Request, sessionStore.Store, nil, false, c.Writer}
}
c.Set(DefaultKey, sessions)
defer context.Clear(c.Request)
c.Next()
}
} It's basically a stand-in replacement for the current SessionsMany although obviously, the types differ so it should probably be a separate helper method to keep backwards compatibility. An example of using it would be: store, err := redis.NewStore(10, "tcp", "localhost:6379", "", []byte("session_secret"), []byte("encryption_secret"))
if err != nil {
log.Fatalf("Could not connect to session database: %s", err)
}
store.Options(sessions.Options{
Path: "/",
MaxAge: 60 * 60 * 24, // 1 days
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteStrictMode,
})
cookieStore := cookie.NewStore([]byte("session_secret"), []byte("encryption_secret"))
cookieStore.Options(sessions.Options{
Path: "/",
MaxAge: 60 * 60 * 24 * 30, // 30 days
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteStrictMode,
})
sessionStores := []sessions.SessionStore{
{
Name: "session",
Store: cookieStore,
},
{
Name: "user_session",
Store: store,
},
}
httpRouter.Use(sessions.SessionsMany(sessionStores)) Happy to raise a PR if at least someone else wants this still |
@appleboy Apologies for tagging you directly but you seem to be one of the more active maintainers. Is this something you would entertain being added? Do you have any preference on a name for the new helper function that would enable it? |
@srbry Send the PR first. |
@appleboy have you had a chance to look at this at all yet? |
@srbry I will take a look this weekend. |
@appleboy When can I see this one? |
@avexbesuke We need @srbry to fix the conflicts first. |
User can create many sessions using
SessionsMany()
supplying array of names for sessions. The problem is that the same store is used for that. I'd like to use store A for session A and store B for session B. Could this feature be implemented without breaking current API?The text was updated successfully, but these errors were encountered: