GithubHelp home page GithubHelp logo

Clear on Server shutdown? about sessions HOT 7 OPEN

gin-contrib avatar gin-contrib commented on July 30, 2024
Clear on Server shutdown?

from sessions.

Comments (7)

ShawnOY avatar ShawnOY commented on July 30, 2024 2

Hi @ghiewa ,
I have same issue, and I found the reason.

In my case, I use 301 code to redirect, first time is working, if I try to logout again and it's not working, I found my browser cache the 301 redirect, 301 is move permanently, when I change from 301 to 302, it's work!

In your case, try to use http.StatusFound.

from sessions.

jarmo avatar jarmo commented on July 30, 2024 2

Easiest way to reset all sessions on a server restart would be to change authentication and/or encryption keys on each restart. For example:

sessionStore := cookie.NewStore(generateRandomBytes(64), generateRandomBytes(32))
router.Use(sessions.Sessions("mysite", sessionStore))

Don't forget to use crypto/rand instead of math/rand for generating your random bytes!

from sessions.

ghiewa avatar ghiewa commented on July 30, 2024

defer ?

from sessions.

MTRNord avatar MTRNord commented on July 30, 2024

Not sure what that means. I am just starting to learn go. Could you give me a code example?

from sessions.

ghiewa avatar ghiewa commented on July 30, 2024

What I said defer should looks like,

	router.Use(func(c *gin.Context) {
		defer func() {
			session := sessions.Default(c)
			session.Clear()
			session.Save()
		}()
		c.Next()
	})

but tricky thing is I fail to archieve the goal. are checking if MaxAge = -1 is ok or not

from sessions.

ghiewa avatar ghiewa commented on July 30, 2024

sad! I can't delete cookie as well. My coding like this in a logout handler,

func Logout(ctx *gin.Context) {
	session := sessions.Default(ctx)
	session.Clear()
	//session.Options(sessions.Options{MaxAge:-1})  // TODO
	err := session.Save()
	if err != nil {
		log.Println(err)
	}
	ctx.Redirect(http.StatusMovedPermanently, login_url)
}

@appleboy help us!

from sessions.

zereraz avatar zereraz commented on July 30, 2024

I am facing the issue of losing the session on server restart. Redis key that worked is still in redis, cookie I send is also the same, only thing that changes is server restart and somehow the session is gone!
Please let me know what to change.

from sessions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.