Skip to content

This is a simple session manager for a go application. For example, you can use it to manage the user's login status. πŸ“‚

License

Notifications You must be signed in to change notification settings

solrac97gr/session-manager

Repository files navigation

Session Manager for Go πŸ“‚

GoDoc Go Report Card Build workflow codecov License: MIT

This is a simple session manager for a go application. Helps you to store information of user and manage it, set a expiration time and set a default session of a group of different sessions created.

Documentation

Visit the GoDoc page for the full documentation.

Usage

Install the package using the go get command:

go get github.com/solrac97gr/session-manager

Import the package in your code:

import "github.com/solrac97gr/session-manager"

And use it:

Example: Create a new session

package main

import (
    "fmt"
    "net/http"

    "github.com/solrac97gr/session-manager"
)

func main() {
    user := struct{
        Name string
        Age int
    }{
        Name: "Solrac",
        Age: 20,
    }{}

    sm := sessionmanager.NewSessionManager()

    // Create a new session
    s,err := sm.CreateSession()
    if err != nil {
        panic(err)
    }

    s.Set("user", user)

    fmt.Println(s.Get("user"))
}

Example: Get a session

package main

import (
    "fmt"
    "net/http"

    "github.com/solrac97gr/session-manager"
)

func main() {
    sm := sessionmanager.NewSessionManager()

    // Get a session
    s,err := sm.GetSession("session-id")
    if err != nil {
        panic(err)
    }

    fmt.Println(s.Get("user"))
}

Example: Delete a session

package main

import (
    "fmt"
    "net/http"

    "github.com/solrac97gr/session-manager"
)

func main() {
    sm := sessionmanager.NewSessionManager()

    // Destroy a session
    err := sm.DestroySession("session-id")
    if err != nil {
        panic(err)
    }
}

Example: Set a default session and get it

package main

import (
    "fmt"
    "net/http"

    "github.com/solrac97gr/session-manager"
)

func main() {
    user := struct{
        Name string
        Age int
    }{
        Name: "Solrac",
        Age: 20,
    }


    sm := sessionmanager.NewSessionManager()
    s,_:= sm.CreateSession()

    s.Set("user", user)

    // Set a default session
    sm.SetAsDefaultSession(s.SessionId())

    // Get a session
    s,err := sm.GetDefaultSession()
    if err != nil {
        panic(err)
    }

    fmt.Println(s.Get("user"))
}

Example: Activated avoid expired sessions and set a expiration time in a session

By default, the session manager does not avoid expired sessions, so if you want to avoid expired sessions, you must activate it.

The defatul expiration time is 5 minutes, so if you want to change expiration time, you must set it.

package main

import (
    "fmt"
    "net/http"

    "github.com/solrac97gr/session-manager"
)

func main() {
    user := struct{
        Name string
        Age int
    }{
        Name: "Solrac",
        Age: 20,
    }

    sm := sessionmanager.NewSessionManager()

    // Activate avoid expired sessions
    sm.SetAvoidExpiredSessions(true)

    s,_:= sm.CreateSession()

    s.Set("user", user)
    s.SetExpirationTime(time.Now().Add(1440 * time.Minute))

    // Set a default session
    sm.SetAsDefaultSession(s.SessionId())

    // Get a session
    s,err := sm.GetDefaultSession()
    if err != nil {
        panic(err)
    }

    fmt.Println(s.Get("user"))
}

Work in progress and completed

  • Create a new session
  • Get a session
  • Delete a session
  • Use concurrent map
  • Add a session expiration time
  • Add a active indicator

License

MIT License

About

This is a simple session manager for a go application. For example, you can use it to manage the user's login status. πŸ“‚

Topics

Resources

License

Stars

Watchers

Forks

Packages