-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth.go
36 lines (30 loc) · 877 Bytes
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package session
import (
"net/http"
gsess "github.com/gorilla/sessions"
"github.com/adh-partnership/ids/backend/pkg/logger"
"github.com/adh-partnership/ids/backend/pkg/response"
)
func IsAuthenticated(r *http.Request) bool {
if r.Context().Value("session") == nil {
logger.ZL.Debug().Msg("session is nil")
return false
}
session := r.Context().Value("session").(*gsess.Session)
logger.ZL.Debug().Msgf("session=%+v", session)
if session.Values["user"] != nil {
logger.ZL.Debug().Msgf("user=%+v", session.Values["user"])
return true
}
logger.ZL.Debug().Msg("user is nil")
return false
}
func AuthenticatedMiddleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !IsAuthenticated(r) {
response.Respond(w, r, "Unauthorized", http.StatusUnauthorized)
return
}
h.ServeHTTP(w, r)
})
}