Skip to content

Latest commit

 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-session-redis

Simple HTTP session management

Example middleware

func SessionAuthenticator(manager *session.Manager, next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        s, err := manager.GetSession(w, r)
        if errors.Is(err, session.ErrSessionNotFound) {
            http.Error(w, "Not authenticated", http.StatusUnauthorized)
            return
        }
        if err != nil {
            http.Error(w, err.Error(), http.StatusBadGateway)
            return
        }

        userID, ok := s.Get("user_id").(string)
        if !ok || userID == "" {
            http.Error(w, "Not authenticated", http.StatusUnauthorized)
            return
        }

        // GetSession refreshes the TTL.
        next.ServeHTTP(w, r)
    })
}

Creating a session

func CreateAuthenticatedSession(manager *session.Manager, w http.ResponseWriter, r *http.Request, userID string) error {
    s, err := manager.CreateSession(w, r)
    if err != nil {
        return err
    }
    return s.Set("user_id", userID)
}

About

Simple HTTP session management

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages