https://github.com/go-stuff/mongostore
An implementation of Store using the Gorilla web toolkit sessions and the MongoDB Go Driver.
https://github.com/go-stuff/mongostore
go golang gorilla gorilla-sessions mongo mongodb
Last synced: 3 months ago
JSON representation
An implementation of Store using the Gorilla web toolkit sessions and the MongoDB Go Driver.
- Host: GitHub
- URL: https://github.com/go-stuff/mongostore
- Owner: go-stuff
- License: mit
- Created: 2019-05-17T19:20:16.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-07-22T16:37:36.000Z (almost 5 years ago)
- Last Synced: 2023-08-11T22:28:03.386Z (almost 3 years ago)
- Topics: go, golang, gorilla, gorilla-sessions, mongo, mongodb
- Language: Go
- Homepage:
- Size: 61.5 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mongostore
[](https://godoc.org/github.com/go-stuff/mongostore)
[](https://cloud.drone.io/go-stuff/mongostore)
[](https://goreportcard.com/report/github.com/go-stuff/mongostore)
[](https://codecov.io/gh/go-stuff/mongostore)
[](https://opensource.org/licenses/MIT)

An implementation of Store using [Gorilla web toolkit](https://github.com/gorilla) and the [MongoDB Go Driver](https://github.com/mongodb/mongo-go-driver)
## Packages Imported
- Gorilla web tookit [github.com/gorilla](https://github.com/gorilla)
- MongoDB Go Driver [github.com/mongodb/mongo-go-driver](https://github.com/mongodb/mongo-go-driver)
## Installation
The recommended way to get started using [github.com/go-stuff/mongostore](https://github.com/go-stuff/mongostore) is by using 'go get' to install the dependency in your project.
```bash
go get "github.com/go-stuff/mongostore"
```
## Usage
The [github.com/go-stuff/web](https://github.com/go-stuff/web) repository uses [github.com/go-stuff/mongostore](https://github.com/go-stuff/mongostore) you can browse this example code to see how it is used.
```go
import "github.com/go-stuff/mongostore"
```
```go
// create a session store using rotating keys
mongoStore, err := mongostore.NewStore(
client.Database(DatabaseName).Collection("sessions"),
http.Cookie{
Path: "/",
Domain: "",
MaxAge: 20 * 60, // 20 mins
Secure: false,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
},
[]byte("new-authentication-key"),
[]byte("new-encryption-key"),
[]byte("old-authentication-key"),
[]byte("old-encryption-key"),
)
if err != nil {
return fmt.Errorf("[ERROR] creating mongo store: %w", err)
}
```
```go
const CookieName = "session-id"
// new sessions
session, err := s.Store.New(r, CookieName)
if err != nil {
log.Printf("[ERROR] new session: %s", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// add values to the session
session.Values["username"] = r.FormValue("username")
// save session
err = session.Save(r, w)
if err != nil {
log.Printf("[ERROR] saving session: %s", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
```
```go
const CookieName = "session-id"
// existing sessions
session, err := s.Store.Get(r, CookieName)
if err != nil {
log.Printf("[ERROR] getting session: %s", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// add values to the session
session.Values["username"] = r.FormValue("username")
// save session
err = session.Save(r, w)
if err != nil {
log.Printf("[ERROR] saving session: %s", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
```
## Example MongoDB Entries
```bash
> db.sessions.find().pretty()
{
"_id" : ObjectId("5db388c21256d9f65e6ccf7e"),
"data" : {
"username" : "test"
},
"modified_at" : ISODate("2019-10-25T23:44:03.558Z"),
"expires_at" : ISODate("2019-10-26T00:04:03.558Z"),
"ttl" : ISODate("2019-10-25T23:44:03.558Z")
}
{
"_id" : ObjectId("5db388cb1256d9f65e6ccf7f"),
"data" : {
"username" : "user1"
},
"modified_at" : ISODate("2019-10-25T23:44:11.485Z"),
"expires_at" : ISODate("2019-10-26T00:04:11.485Z"),
"ttl" : ISODate("2019-10-25T23:44:11.485Z")
}
```
## License
[MIT License](LICENSE)