session

package module
v1.2.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2020 License: MIT Imports: 11 Imported by: 3 modules, 6 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Session

Release Discord Test Security Linter

This session middleware is build on top of fasthttp/session by @savsgio MIT. Special thanks to @thomasvvugt for helping with this middleware.

Install
go get -u github.com/gofiber/fiber
go get -u github.com/gofiber/session
Signature
session.New(config ...session.Config) *Session
Config
Property Type Description Default
Lookup string Where to look for the session id <source>:<name>, possible values: cookie:key, header:key, query:key "cookie:session_id"
Domain string Cookie domain ""
Expiration time.Duration Session expiration time, possible values : 0 means no expiry (24 years), -1 means when the browser closes, >0 is the time.Duration which the session cookies should expire. 12 * time.Hour
Secure bool If the cookie should only be send over HTTPS false
Provider Provider Holds the provider interface memory.Provider
Generator func() []byte Generator is a function that generates an unique id uuid
GCInterval time.Duration Interval for the garbage collector uuid
Default Example
package main

import (
  "fmt"

  "github.com/gofiber/fiber"
  "github.com/gofiber/session"
)

func main() {
  app := fiber.New()

  // create session handler
  sessions := session.New()

  app.Get("/", func(c *fiber.Ctx) {
    store := sessions.Get(c)    // get/create new session
    defer store.Save()

    store.ID()                   // returns session id
    store.Destroy()              // delete storage + cookie
    store.Get("john")            // get from storage
    store.Regenerate()           // generate new session id
    store.Delete("john")         // delete from storage
    store.Set("john", "doe")     // save to storage
  })
  
  app.Listen(3000)
}
Provider Example
package main

import (
  "fmt"

  "github.com/gofiber/fiber"
  "github.com/gofiber/session"
  "github.com/gofiber/session/provider/memcache"
  // "github.com/gofiber/session/provider/mysql"
  // "github.com/gofiber/session/provider/postgres"
  // "github.com/gofiber/session/provider/redis"
  // "github.com/gofiber/session/provider/sqlite3"
)

func main() {
  app := fiber.New()

  provider := memcache.New(memcache.Config{
    KeyPrefix:  "session",
    ServerList: []string{
      "0.0.0.0:11211",
    },
    MaxIdleConns: 8,
  })
  // provider := mysql.New(mysql.Config{
  //   Host:       "session",
  //   Port:       3306,
  //   Username:   "root",
  //   Password:   "",
  //   Database:   "test",
  //   TableName:  "session",
  // })
  // provider := postgres.New(postgres.Config{
  //   Host:       "session",
  //   Port:       5432,
  //   Username:   "root",
  //   Password:   "",
  //   Database:   "test",
  //   TableName:  "session",
  // })
  // provider := redis.New(redis.Config{
  //   KeyPrefix:   "session",
  //   Addr:        "127.0.0.1:6379",
  //   PoolSize:    8,
  //   IdleTimeout: 30 * time.Second,
  // })
  // provider := sqlite3.New(sqlite3.Config{
  //   DBPath:     "test.db",
  //   TableName:  "session",
  // })

  sessions := session.New(session.Config{
    Provider: provider,
  })

  app.Get("/", func(c *fiber.Ctx) {
    store := sessions.Get(c)    // get/create new session
    defer store.Save()

    store.ID()                   // returns session id
    store.Destroy()              // delete storage + cookie
    store.Get("john")            // get from storage
    store.Regenerate()           // generate new session id
    store.Delete("john")         // delete from storage
    store.Set("john", "doe")     // save to storage
  })
  
  app.Listen(3000)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Lookup is a string in the form of "<source>:<name>" that is used
	// to extract session id from the request.
	// Possible values: "header:<name>", "query:<name>" or "cookie:<name>"
	// Optional. Default value "cookie:session_id".
	Lookup string

	// Secure attribute for cookie
	// Optional. Default: false
	Secure bool

	// Domain attribute for cookie
	// Optional. Default: ""
	Domain string

	// SameSite attribute for cookie
	// Possible values: "Lax", "Strict", "None"
	// Optional. Default: "Lax"
	SameSite string

	//  0 means no expire, (24 years)
	// -1 means when browser closes
	// >0 is the time.Duration which the session cookies should expire.
	// Optional. Default: 12 hours
	Expiration time.Duration

	// Holds the provider interface
	// Optional. Default: memory.New()
	Provider fsession.Provider

	// Generator is a function that generates an unique id
	// Optional.
	Generator func() []byte

	// gc life time to execute it
	// Optional. 1 minute
	GCInterval time.Duration
	// contains filtered or unexported fields
}

Config defines the config for RequestID middleware

type Session

type Session struct {
	// contains filtered or unexported fields
}

Session ...

func New

func New(config ...Config) *Session

New ...

func (*Session) Get

func (s *Session) Get(ctx *fiber.Ctx) *Store

Get store

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is a wrapper arround the session.Store

func (*Store) Delete

func (s *Store) Delete(key string)

Delete delete data by key

func (*Store) Destroy

func (s *Store) Destroy()

Destroy session and cookies

func (*Store) Get

func (s *Store) Get(key string) interface{}

Get get data by key

func (*Store) ID

func (s *Store) ID() string

ID returns session id

func (*Store) Regenerate

func (s *Store) Regenerate() error

Regenerate session id

func (*Store) Save

func (s *Store) Save() error

Save storage before response

func (*Store) Set

func (s *Store) Set(key string, value interface{})

Set get data by key

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址