gin-contrib / sessions

Gin middleware for session management
MIT License
1.46k stars 196 forks source link

Use always error #109

Open venvis856 opened 4 years ago

venvis856 commented 4 years ago

package example

import ( "fmt" "github.com/gin-contrib/sessions/redis" "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" )

func Session(c *gin.Context){ session := sessions.Default(c)

store, _ := redis.NewStore(10, "tcp", "192.168.1.246:6379", "", []byte("secret"))
session.Set("login_user",store)

err:=session.Save()
if err!=nil {
    fmt.Println(err)
}

//res:=session.Get("login_user")
//fmt.Println(res)
c.String(200,"nihao")

}

error: 2020/01/02 17:35:02 [Recovery] 2020/01/02 - 17:35:02 panic recovered: GET /Session HTTP/1.1 Host: 127.0.0.1:8077 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9 Accept-Encoding: gzip, deflate, br Accept-Language: zh-CN,zh;q=0.9 Cache-Control: no-cache Connection: keep-alive Pragma: no-cache Sec-Fetch-Mode: navigate Sec-Fetch-Site: cross-site Sec-Fetch-User: ?1 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36

Key "github.com/gin-gonic/contrib/sessions" does not exist F:/go/src/gin/vendor/github.com/gin-gonic/gin/context.go:240 (0xafd1a2) (Context).MustGet: panic("Key \"" + key + "\" does not exist") F:/go/src/gin/vendor/github.com/gin-gonic/contrib/sessions/sessions.go:146 (0xafcea7) Default: return c.MustGet(DefaultKey).(Session) F:/go/src/gin/app/example/session.go:11 (0xafcea6) Session: session := sessions.Default(c) F:/go/src/gin/vendor/github.com/gin-gonic/gin/context.go:147 (0x8e04e0) (Context).Next: c.handlersc.index F:/go/src/gin/vendor/github.com/gin-gonic/gin/recovery.go:83 (0x8f4720) RecoveryWithWriter.func1: c.Next() F:/go/src/gin/vendor/github.com/gin-gonic/gin/context.go:147 (0x8e04e0) (Context).Next: c.handlersc.index F:/go/src/gin/vendor/github.com/gin-gonic/gin/logger.go:241 (0x8f3827) LoggerWithConfig.func1: c.Next() F:/go/src/gin/vendor/github.com/gin-gonic/gin/context.go:147 (0x8e04e0) (Context).Next: c.handlersc.index F:/go/src/gin/vendor/github.com/gin-gonic/gin/gin.go:403 (0x8ea9e0) (Engine).handleHTTPRequest: c.Next() F:/go/src/gin/vendor/github.com/gin-gonic/gin/gin.go:364 (0x8ea0c4) (Engine).ServeHTTP: engine.handleHTTPRequest(c) C:/Go/src/net/http/server.go:2774 (0x6b3e7e) serverHandler.ServeHTTP: handler.ServeHTTP(rw, req) C:/Go/src/net/http/server.go:1878 (0x6af967) (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req) C:/Go/src/runtime/asm_amd64.s:1337 (0x458e20) goexit: BYTE $0x90 // NOP  [GIN] 2020/01/02 - 17:35:02 | 500 | 49.9986ms | 127.0.0.1 | GET  /Session

GwynethLlewelyn commented 4 years ago

Seems to be the same error as #115.

sizzlorox commented 4 years ago

I've just ran into this issue and found:

The problem here is that https://github.com/gin-gonic/contrib/blob/master/sessions/sessions.go

The default key is set to github.com/gin-gonic/contrib/sessions where as https://github.com/gin-contrib/sessions/blob/v0.0.3/sessions.go it is set to github.com/gin-contrib/sessions

Wherever you're calling session := sessions.Default(c) you're importing "github.com/gin-gonic/contrib/sessions" instead of "github.com/gin-contrib/sessions"

EDIT:

After further investigation in order to fix this you must find all references to github.com/gin-contrib/sessions (If you're using gin-gonic) to github.com/gin-gonic/contrib/sessions and follow the example here: https://github.com/gin-gonic/contrib/tree/master/sessions

For example I was using the cookie store so I had to refactor

    store := cookie.NewStore([]byte(os.Getenv("SESSION_SECRET")))

to:

    store := sessions.NewCookieStore([]byte("SESSION_SECRET"))
boahost commented 11 months ago

I will have this error:

Key "github.com/gin-gonic/contrib/sessions" does not exist C:/Users/boaho/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:273 (0x86a2a7) (Context).MustGet: panic("Key \"" + key + "\" does not exist") C:/Users/boaho/go/pkg/mod/github.com/gin-gonic/contrib@v0.0.0-20221130124618-7e01895a63f2/sessions/sessions.go:146 (0x86a249) Default: return c.MustGet(DefaultKey).(Session) C:/laragon/www/notificacao_bazar/Controllers/userController.go:35 (0x86d4d6) Store: session := sessions.Default(s) C:/Users/boaho/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174 (0x85b1d9) (Context).Next: c.handlersc.index C:/Users/boaho/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/recovery.go:102 (0x85b1c7) CustomRecoveryWithWriter.func1: c.Next() C:/Users/boaho/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174 (0x85a37d) (Context).Next: c.handlersc.index C:/Users/boaho/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/logger.go:240 (0x85a34c) LoggerWithConfig.func1: c.Next() C:/Users/boaho/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174 (0x85947a) (Context).Next: c.handlersc.index C:/Users/boaho/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/gin.go:620 (0x85910d) (Engine).handleHTTPRequest: c.Next() C:/Users/boaho/go/pkg/mod/github.com/gin-gonic/gin@v1.9.1/gin.go:576 (0x858c3c) (Engine).ServeHTTP: engine.handleHTTPRequest(c) C:/Program Files/Go/src/net/http/server.go:2938 (0x688dcd) serverHandler.ServeHTTP: handler.ServeHTTP(rw, req) C:/Program Files/Go/src/net/http/server.go:2009 (0x684cb3) (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req) C:/Program Files/Go/src/runtime/asm_amd64.s:1650 (0x32b480) goexit: BYTE $0x90 // NOP