Back to

Package session

Latest Go to latest

The highest tagged major version is .

Published: Jan 4, 2018 | License: MIT | Module:



var (

	//SecretKey to encrypt session
	//please clear the browser buffer(ctrl+shift+delete) in case you change the key and token name to new one
	SecretKey = "welcome to Scott Huang's Session and JWT util, please change to your secret accodingly."

	//JwtTokenName is JWT token name, also is the JWT session name too
	//Not too long and also do not include blank, such as do not set as "a blank name"
	JwtTokenName = "jwtTokenSession"

	//DefaultFlashSessionName to store the flash session
	//Not too long and also do not include blank, such as do not set as "a blank name"
	DefaultFlashSessionName = "myDefaultFlashSessionName"

	//DefaultSessionName to store other session message
	//Not too long and also do not include blank, such as do not set as "a blank name"
	DefaultSessionName = "myDefaultSessionName"

	//DefaultOption to provide default option
	//Maxage set the session duration by second
	DefaultOption = &sessions.Options{
		Path:     "/",
		MaxAge:   3600 * 1,
		HttpOnly: true,

func ClearMiddleware

func ClearMiddleware() gin.HandlerFunc

ClearMiddleware clear mem to avoid leak. you should add this middleware at your main gin.router

Please see note from Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory! ClearHandler actually invoke Clear func

func Delete

func Delete(c *gin.Context, key interface{}) (err error)

Delete try delete default session key value

func DeleteAllSession

func DeleteAllSession(c *gin.Context)

DeleteAllSession will delete JwtTokenName/DefaultSessionName/DefaultFlashSessionName usually used when user logout

func DeleteNormalSession

func DeleteNormalSession(c *gin.Context)

DeleteNormalSession will delete DefaultSessionName and DefaultFlashSessionName

func DeleteSession

func DeleteSession(c *gin.Context, sessionName string) (err error)

DeleteSession delete a session instead of Set, the delete will set MaxAge to -1

func DeleteSessionValue

func DeleteSessionValue(c *gin.Context, sessionName string, key interface{}) (err error)

DeleteSessionValue try delete the gived session key value

func DeleteTokenSession

func DeleteTokenSession(c *gin.Context) (err error)

DeleteTokenSession delete before generate JWT token string

func GenerateJWTToken

func GenerateJWTToken(username string, tokenDuration time.Duration) (tokenString string, err error)

GenerateJWTToken per gived username and token duration

Claims example as below, and we only use below top 3 claims

"iat": 1416797419, //start
"exp": 1448333419, //end
"sub": "",  //username

"iss": "Online JWT Builder",
"aud": "",
"GivenName": "Johnny",
"Surname": "Rocket",
"Email": "",
"Role": [ "Manager", "Project Administrator" ]

func GetDefaultSessionValue

func GetDefaultSessionValue(c *gin.Context, key interface{}) (value interface{}, err error)

GetDefaultSessionValue levarage GetSessionValue to get key value

func GetFlashes

func GetFlashes(c *gin.Context) []interface{}

GetFlashes return previously flashes from default session

func GetInt

func GetInt(c *gin.Context, key interface{}) (value int, err error)

GetInt levarage Get to get key value and convert to int

func GetSessionFlashes

func GetSessionFlashes(c *gin.Context, sessionName string) []interface{}

GetSessionFlashes return previously flashes per gived session

func GetSessionValue

func GetSessionValue(c *gin.Context, sessionName string, key interface{}) (value interface{}, err error)

GetSessionValue session value according The base get func, other will leverage this func

func GetString

func GetString(c *gin.Context, key interface{}) (value string, err error)

GetString levarage Get to get key value and convert to string

func GetTokenString

func GetTokenString(c *gin.Context) (tokenString string, err error)

GetTokenString to get tokenString

func NewStore

func NewStore()

NewStore only create one session store globally The main program need call sessions.NewStore() to initialize store User also can set sessions var before call this function to adjust some default parameters

Usage Sample:

//in your main(), setup session after r = gin.Default()
sessions.JwtTokenName = "YourJWTTokenName"                       //string without blank
sessions.DefaultSessionName = "YourDefaultSessionName"           //string without blank
sessions.DefaultFlashSessionName = "YourDefaultFlashSessionName" //string without blank
sessions.SecretKey = "Your Secerect Key (*&%(&*%$"               //string with any
sessions.NewStore()                                              //setup the session store
r.Use(sessions.ClearMiddleware())                                //important to avoid memory leak
//end setup session

func Set

func Set(c *gin.Context, key, value interface{}) (err error)

Set set key value by interface

func SetFlash

func SetFlash(c *gin.Context, flash interface{}) (err error)

SetFlash set new flash to default session

func SetMessage

func SetMessage(c *gin.Context, message Message) (err error)

SetMessage will set session into gin.Context per gived SesionMessage It is the basic function for other set func to leverage

Usage Sample:

 err := sessions.SetMessage(c,
		Key:   sessions.JwtTokenName,
		Value: tokenString,
		// SessionName: "",
		// Options: &sessions.Options{
		// 	Path:     "/",
		// 	MaxAge:   3600 * 1, //1 hour for session. Btw, the token itself have valid period, so, better set it as same
		// 	HttpOnly: true,
		// },

func SetSessionFlash

func SetSessionFlash(c *gin.Context, flash Flash) (err error)

SetSessionFlash set new flash to givied session

func SetTokenString

func SetTokenString(c *gin.Context, tokenString string, seconds int) (err error)

SetTokenString into JwtTokenSession

func ValidateJWTToken

func ValidateJWTToken(c *gin.Context) (username string, err error)

ValidateJWTToken valide JWT per headder firstly And then try get from session if above failed will return valid username in case no err username == "" also mean failed

type Flash

type Flash struct {
	Flash       interface{}
	SessionName string

Flash type to contain new flash and its session

type Message

type Message struct {
	Key         interface{}
	Value       interface{}
	SessionName string
	Options     *sessions.Options

Message struct contain message you would like to set in session usually you just provide Key and Value only

type Options

type Options = sessions.Options

Options stores configuration for a session or session store. Fields are a subset of http.Cookie fields.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier