Skip to content
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

Key "github.com/gin-gonic/contrib/sessions" does not exist #533

Closed
paulm17 opened this issue Feb 16, 2016 · 11 comments
Closed

Key "github.com/gin-gonic/contrib/sessions" does not exist #533

paulm17 opened this issue Feb 16, 2016 · 11 comments

Comments

@paulm17
Copy link

paulm17 commented Feb 16, 2016

I'm able to get the basic examples to work, but trying to get it to work with my MVC skeleton using gin.

So my routes.go is this:

package router

import (
    "github.com/gin-gonic/gin"
    "github.com/paulm/platform/auth"
    "github.com/paulm/platform/controllers"
    "github.com/paulm/platform/libraries/common"
)

func Route() {
    // Creates a router without any middleware by default
    r := gin.New()

    // Initialise Controllers
    c := &controllers.Core{
        Engine: r,
    }

    c.Init()

    // Add Static Assets
    r.Static("/assets", "./public/assets")
    r.StaticFile("/favicon.ico", "./public/favicon.ico")

    // Read Config
    r.Use(common.ReadConfigFile())

    // Init Sessions
    r.Use(auth.InitSession())

    // Connect to Database
    //r.Use(models.Connect())

    // Wire up Routes
    authorized := r.Group("/")
    // per group middleware! in this case we use the custom created
    // AuthRequired() middleware just in the "authorized" group.
    authorized.Use(auth.RequireLogin())
    {
        authorized.GET("/", c.Login)
    }

    // listen and serve on 0.0.0.0:7777
    r.Run(":7777")
}

My auth.go is this:

package auth

import (
    "fmt"
    "github.com/gin-gonic/contrib/sessions"
    "github.com/gin-gonic/gin"
    "github.com/paulm/platform/libraries/common"
)

func InitSession() gin.HandlerFunc {
    return func(c *gin.Context) {
        var config common.TomlConfig
        config = common.ReadConfig(c)

        store := sessions.NewCookieStore([]byte(config.Session.Secret))
        sessions.Sessions(config.Session.Store, store)

        c.Next()
    }
}

// RequireLogin is a simple middleware which checks to see if the user is currently logged in.
// If not, the function returns a 302 redirect to the login page.
func RequireLogin() gin.HandlerFunc {
    return func(c *gin.Context) {
        session := sessions.Default(c)
        user := session.Get("user")

        if user == nil {
            fmt.Println("No user cookie")
        } else {
            fmt.Println("user haz cookie")
        }

        c.Next()
    }
}

Why am I seeing:

http: panic serving 127.0.0.1:42855: Key "github.com/gin-gonic/contrib/sessions" does not exist

Seems that whatever is happening in InitSession is not readable in RequireLogin.

Thanks

@robvdl
Copy link

robvdl commented Feb 16, 2016

Just a question, why would you want to load a config file each time in a middleware and setup the session, you only want to do this once at app startup, not for each request in a middlware, imagine it reading a config file each request... that would be slow.

Your InitSessions middleware returns a handler func though, you could load the config file in the outer function, not the inner one which runs for each request.

@paulm17
Copy link
Author

paulm17 commented Feb 16, 2016

Just a question, why would you want to load a config file each time in a middleware and setup the session, you only want to do this once at app startup, not for each request in a middlware, imagine it reading a config file each request... that would be slow.

You are exactly right. But I haven't yet been able to do this. This is my ultimate goal.

@robvdl
Copy link

robvdl commented Feb 16, 2016

Are you saving the session, contrib sessions uses gorilla sessions behinds the scenes, and you have to first put something into, then save the session or there won't be anything there (no cookies, or session will be created). It looks like you are trying to retrieve "user" from the session, but I see nowhere where user is being put into the session and the session being saved first.

@paulm17
Copy link
Author

paulm17 commented Feb 16, 2016

It code isn't even getting to the "user" part.

I'm doing the basic example from here: https://github.com/gin-gonic/contrib/tree/master/sessions

It actually fails here: https://github.com/gin-gonic/contrib/blob/master/sessions/sessions.go#L146

http: panic serving 127.0.0.1:42856: Key "github.com/gin-gonic/contrib/sessions" does not exist
goroutine 7 [running]:
net/http.(*conn).serve.func1(0xc82015f4a0, 0x7f8f63c5fda0, 0xc820026290)
    /usr/lib/golang/src/net/http/server.go:1287 +0xb5
github.com/gin-gonic/gin.(*Context).MustGet(0xc82015f600, 0x97eee0, 0x25, 0x0, 0x0)
    /srv/www/www.trackingdev.com/public_html/golang/src/github.com/gin-gonic/gin/context.go:179 +0x1a8
github.com/gin-gonic/contrib/sessions.Default(0xc82015f600, 0x0, 0x0)
    /srv/www/www.trackingdev.com/public_html/golang/src/github.com/gin-gonic/contrib/sessions/sessions.go:146 +0x46

@paulm17
Copy link
Author

paulm17 commented Feb 16, 2016

Resolved. Refactored this and took consideration of your advice. Now the config, session and database are loaded and then placed into the context.

Thanks

@paulm17 paulm17 closed this as completed Feb 16, 2016
@Firiyuu
Copy link

Firiyuu commented Jul 2, 2018

paulm17, I have the same problem with you, can you share how you did it? what did you change in your code?

@delphinus
Copy link

@Firiyuu this thread is based on some old code. Now gin-contrib/sessions master has different syntax to achieve that. see this and README there.

https://github.com/gin-contrib/sessions/blob/master/example/cookie/main.go

(btw, maybe you forget to call middleware over handlers with r.Use(...), I think)

@somebin
Copy link

somebin commented Jan 11, 2019

You shlould call

r := gin.Default()
store := sessions.NewStore([]byte("secret"))
r.Use(sessions.Sessions("mysession", store))

before you call

r.GET("/incr", func(...))

check it !

@jackbaron
Copy link

@paulm17
when i initialized sessions.Default(c) errors appear

Cannot use c (type *"github.com/project/vendor/github.com/gin-gonic/gin".Context) as type *"github.com/gin-gonic/gin".Context in argument to "github.com/gin-gonic/contrib/sessions".Default
Can anyone help me?

@paulm17
Copy link
Author

paulm17 commented Jul 4, 2019

sorry everyone. I moved over to Echo a year after and then to Vue after that. My go usage is strictly API now.

@WesleyBatista
Copy link

This helped me: denisbakhtin/ginblog#1 (comment) 🤦

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants