website/pkg/middleware/session.go
Noah dd1e6c2918 Initial commit
* Initial codebase (lot of work!)
* Uses vanilla Go net/http and implements by hand: session cookies
  backed by Redis; log in/out; CSRF protection; email verification flow;
  initial database models (User table)
2022-08-09 22:32:19 -07:00

21 lines
431 B
Go

package middleware
import (
"context"
"net/http"
"git.kirsle.net/apps/gosocial/pkg/session"
)
// Session middleware.
func Session(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check for the session_id cookie.
sess := session.LoadOrNew(r)
ctx := context.WithValue(r.Context(), session.ContextKey, sess)
handler.ServeHTTP(w, r.WithContext(ctx))
})
}