session

package module
v0.0.0-...-e3c0d19 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2023 License: BSD-3-Clause Imports: 6 Imported by: 0

README

session

Fast in-memory backend session storage middleware.

Usage

package main

import (
	"fmt"
	"math/rand"
	"net/http"
	"time"

	"github.com/Acebond/session"
)

type Session struct {
	Role     string
	Username string
}

var (
	ss session.SessionStore[Session]
)

func loginHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		html := `<html><head></head><form action="/login" method="post"><input type="text" name="username">
		<input type="password" name="password"><input type="submit" value="Submit"></form></head>`
		w.Write([]byte(html))
	} else if r.Method == "POST" {
		username := r.FormValue("username")
		password := r.FormValue("password")

		if username == "admin" && password == "admin" {
			sess := &Session{}
			sess.Role = "Administrator"
			sess.Username = "Admin"
			ss.PutSession(w, r, sess)
			http.Redirect(w, r, "/admin", http.StatusFound)
		} else {
			http.Error(w, "Incorrect username or password", http.StatusUnauthorized)
		}
	}
}

func logoutHandler(w http.ResponseWriter, r *http.Request) {
	ss.DeleteSession(r)
	http.Redirect(w, r, "/login", http.StatusFound)
}

func adminPanelHandler(w http.ResponseWriter, r *http.Request) {
	sess := ss.GetSessionFromCtx(r)
	if sess.Role != "Administrator" {
		http.Error(w, "", http.StatusForbidden)
		return
	}
	http.Error(w, fmt.Sprintf("Hello %s\n", sess.Username), http.StatusOK)
}

func init() {
	rand.Seed(time.Now().UTC().UnixNano())
}

func main() {
	ss.InitStore("SessionID", time.Duration(time.Hour*24*7)) // 1 week
	http.HandleFunc("/login", loginHandler)
	http.HandleFunc("/logout", logoutHandler)
	http.Handle("/admin", ss.LoadSession(http.HandlerFunc(adminPanelHandler)))
	http.ListenAndServe(":8090", nil)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SessionStore

type SessionStore[T any] struct {
	// contains filtered or unexported fields
}

SessionStore holds the session data and settings

func (*SessionStore[T]) DeleteSession

func (st *SessionStore[T]) DeleteSession(r *http.Request)

DeleteSession will delete the session from the SessionStore.

func (*SessionStore[T]) GetSessionFromCtx

func (st *SessionStore[T]) GetSessionFromCtx(r *http.Request) *T

GetSessionFromCtx retrieves the session from the http.Request context. The function will return nil if the session does not exist within the http.Request context.

func (*SessionStore[T]) GetSessionFromRequest

func (st *SessionStore[T]) GetSessionFromRequest(r *http.Request) *T

GetSessionFromRequest retrieves the session from the http.Request cookies. The function will return nil if the session does not exist within the http.Request cookies.

func (*SessionStore[T]) InitStore

func (st *SessionStore[T]) InitStore(name string, itemExpiry time.Duration)

Init will initialize the SessionStore object

func (*SessionStore[T]) LoadSession

func (st *SessionStore[T]) LoadSession(next http.Handler) http.Handler

LoadSession will load the session into the http.Request context. A http.StatusUnauthorized will be retuned to the client if no session can be found.

func (*SessionStore[T]) PutSession

func (st *SessionStore[T]) PutSession(w http.ResponseWriter, r *http.Request, sess *T)

PutSession will store the session in the SessionStore. The session will automatically expire after defined SessionStore.sessionExpiration.

Jump to

Keyboard shortcuts

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