bring back yahoo pipes!
1package auth
2
3import (
4 "context"
5 "net/http"
6
7 "github.com/kierank/pipes/store"
8)
9
10type contextKey string
11
12const userContextKey contextKey = "user"
13
14func (sm *SessionManager) RequireAuth(next http.HandlerFunc) http.HandlerFunc {
15 return func(w http.ResponseWriter, r *http.Request) {
16 user, err := sm.GetCurrentUser(r)
17 if err != nil || user == nil {
18 http.Redirect(w, r, "/auth/login", http.StatusSeeOther)
19 return
20 }
21
22 // Add user to context
23 ctx := context.WithValue(r.Context(), userContextKey, user)
24 next(w, r.WithContext(ctx))
25 }
26}
27
28func GetUserFromContext(ctx context.Context) *store.User {
29 user, _ := ctx.Value(userContextKey).(*store.User)
30 return user
31}