Add option for protected router with statuscode
This commit is contained in:
parent
7a1644b9e3
commit
65699b2d46
|
@ -47,18 +47,39 @@ func (m *Middleware) AddUserInfo(next http.Handler) http.Handler {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ProtectedRoute Checks if session and token are present, if not return a 401 response
|
||||||
func (m *Middleware) ProtectedRoute(next http.Handler) http.Handler {
|
func (m *Middleware) ProtectedRoute(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
session, err := m.s.Get(r)
|
session, err := m.s.Get(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Redirect(w, r, "/signin", http.StatusSeeOther)
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
token := session.Values["access_token"]
|
token := session.Values["access_token"]
|
||||||
|
|
||||||
if token == "" || token == nil {
|
if token == "" || token == nil {
|
||||||
http.Redirect(w, r, "/signin", http.StatusSeeOther)
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ProtectedRouteWithRedirect Checks if session and token are present, if not return a redirect to /login
|
||||||
|
func (m *Middleware) ProtectedRouteWithRedirect(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
session, err := m.s.Get(r)
|
||||||
|
if err != nil {
|
||||||
|
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
token := session.Values["access_token"]
|
||||||
|
|
||||||
|
if token == "" || token == nil {
|
||||||
|
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue