Go implementation of persistent login cookies. The implementation idea is described here
The library uses gorilla sessions for saving cookies on user site. Part of the persistant login information is stored on a server side in a dedicated Store. There are two store implementations available out of the box:
- SQL database store
- RethinkDB store
go get -u github.com/janekolszak/rememberme
// Create the store
dbCookieStore, err := sqlstore.New("sqlite3", "/tmp/database.db3")
if err != nil {
panic(err)
}
// Create Rememberme
auth = &rememberme.New{
Store: dbCookieStore,
MaxAge: time.Second * 30,
}
func(w http.ResponseWriter, r *http.Request) {
selector, userid, err := auth.Check(r)
if err == nil {
// Authenticated with a RememberMe Cookie
err = auth.UpdateCookie(w, r, selector, userid)
if err != nil {
panic(err)
}
} else {
// Can't authenticate with "Remember Me" cookie,
// so try with another provider.
// userid = Authenticated User ID
// Save the RememberMe cookie
err = auth.SetCookie(w, r, userid)
if err != nil {
panic(err)
}
}
// User authenticated, cookie saved
// ...
}