-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
[FEATURE REQUEST] update session expire date use *Session not only *Sessions #1485
Comments
Hello @huyinghuan, The Session.LifeTime.Shift(time.Duration) does not work for you? It should do exactly that you want to do, is there any particular reason for asking a new method to be added? Thanks, |
@kataras I use my code is
result: |
Did you try the following? // sess.Lifetime.Shift(24 * time.Hour)
// session.UpdateExpiration(ctx, sess.Lifetime.DurationUntilExpiration())
// OR just
session.UpdateExpiration(ctx, 24* time.hour) The following test passes: func TestSessionsUpdateExpiration(t *testing.T) {
app := iris.New()
cookieName := "mycustomsessionid"
sess := sessions.New(sessions.Config{
Cookie: cookieName,
Expires: 30 * time.Minute,
})
app.Use(sess.Handler())
type response struct {
SessionID string `json:"sessionID"`
Logged bool `json:"logged"`
}
var writeResponse = func(ctx context.Context) {
session := sessions.Get(ctx)
ctx.JSON(response{
SessionID: session.ID(),
Logged: session.GetBooleanDefault("logged", false),
})
}
app.Get("/get", func(ctx context.Context) {
writeResponse(ctx)
})
app.Get("/set", func(ctx iris.Context) {
sessions.Get(ctx).Set("logged", true)
writeResponse(ctx)
})
app.Post("/remember_me", func(ctx iris.Context) {
// re-sends the cookie with the new Expires and MaxAge fields,
// test checks that on same session id too.
sess.UpdateExpiration(ctx, 24*time.Hour)
writeResponse(ctx)
})
e := httptest.New(t, app, httptest.URL("http://example.com"))
tt := e.GET("/set").Expect().Status(httptest.StatusOK)
tt.Cookie(cookieName).MaxAge().Equal(30 * time.Minute)
sessionID := tt.JSON().Object().Raw()["sessionID"].(string)
expectedResponse := response{SessionID: sessionID, Logged: true}
e.GET("/get").Expect().Status(httptest.StatusOK).
JSON().Equal(expectedResponse)
tt = e.POST("/remember_me").Expect().Status(httptest.StatusOK)
tt.Cookie(cookieName).MaxAge().Equal(24 * time.Hour)
tt.JSON().Equal(expectedResponse)
} |
|
Hello @huyinghuan, let me explain the issue here. The
The 1. can be fixed by: sess := sessions.New(sessions.Config{
Cookie: cookieName,
Expires: 30 * time.Minute,
AllowReclaim: true, // <---HERE
}) The 2. can be fixed by modifying the following code: Lines 35 to 40 in b6ac394
to: // UpsertCookie adds a cookie to the response like `SetCookie` does
// but it will also perform a replacement of the cookie
// if already set by a previous `SetCookie` call.
// It reports whether the cookie is new (true) or an existing one was updated (false).
func (ctx *context) UpsertCookie(cookie *http.Cookie, options ...CookieOption) bool {
for _, opt := range options {
opt(cookie)
}
header := ctx.ResponseWriter().Header()
if cookies := header["Set-Cookie"]; len(cookies) > 0 {
s := cookie.Name + "=" // name=?value
for i, c := range cookies {
if strings.HasPrefix(c, s) {
// We need to update the Set-Cookie (to update the expiration or any other cookie's properties).
// Probably the cookie is set and then updated in the first session creation
// (e.g. UpdateExpiration, see https://github.com/kataras/iris/issues/1485).
cookies[i] = cookie.String()
header["Set-Cookie"] = cookies
return false
}
}
}
header.Add("Set-Cookie", cookie.String())
return true
} func AddCookie(ctx context.Context, cookie *http.Cookie, reclaim bool) {
if reclaim {
ctx.Request().AddCookie(cookie)
}
ctx.UpsertCookie(cookie)
} I think this is the problem you are facing in your application? |
Here is the commit which fixes this issue: c61d047. It will be available on the upcoming |
thanks for your suggestion ! It's solved by use config |
Good @huyinghuan! I've also added a new |
…of 'context.SetCookie' Former-commit-id: 31a50e580929616504b9bbbb1d602b0e9274a568
relative to: #1485 Former-commit-id: c4ced38b74af42bfcd17abe6b439b35db6837bbf
Is your feature request related to a problem? Please describe.
here is "remember me" checkbox in my sign in page , at the most time, i use config
set the cookie expire date:
if user checked "remember me", i hope I can set cookie expire date use other date.
Describe the solution you'd like
The text was updated successfully, but these errors were encountered: