Skip to content

MJKWoolnough/sessions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sessions

CI Go Reference Go Report Card

-- import "vimagination.zapto.org/sessions"

Package sessions is used to store session information for a web server.

Highlights

  • Set and get session data in client cookies.
  • Session data is signed and dated to prevent tampering.

Usage

package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"time"

	"vimagination.zapto.org/sessions"
)

func main() {
	store, err := sessions.NewCookieStore([]byte("!THIS IS MY KEY!"), sessions.Expiry(time.Second))
	if err != nil {
		fmt.Println(err)

		return
	}

	w := httptest.NewRecorder()

	store.Set(w, []byte("MY_DATA"))

	r := httptest.NewRequest(http.MethodGet, "http://example.com", nil)

	for _, cookie := range w.Result().Cookies() {
		r.AddCookie(cookie)
	}

	fmt.Printf("%q\n", store.Get(r))

	time.Sleep(2 * time.Second)

	fmt.Printf("%q\n", store.Get(r))

	// Output:
	// "MY_DATA"
	// ""
}

Documentation

Full API docs can be found at:

https://pkg.go.dev/vimagination.zapto.org/sessions

About

Package sessions is used to store session information for a web server

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages