feat: login form or room info on index page
This commit is contained in:
@@ -3,6 +3,7 @@ package routes
|
||||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
@@ -55,6 +56,27 @@ func getContextSession(ctx context.Context) (sessions.SessionData, bool) {
|
||||
}
|
||||
}
|
||||
|
||||
var ErrAuthCookieMissing = errors.New("auth cookie is missing")
|
||||
var ErrAuthCookieValueInvalid = errors.New("auth cookie value is not decoeable")
|
||||
var ErrAuthSessionNotFound = errors.New("session not found")
|
||||
|
||||
func getRequestSession(r *http.Request,
|
||||
sessionsM sessions.SessionManagement) (sessions.SessionData, error) {
|
||||
sessionCookie, err := r.Cookie(authCookieName)
|
||||
if err != nil {
|
||||
return sessions.SessionData{}, ErrAuthCookieMissing
|
||||
}
|
||||
sessionId, err := strconv.Atoi(sessionCookie.Value)
|
||||
if err != nil {
|
||||
return sessions.SessionData{}, ErrAuthCookieValueInvalid
|
||||
}
|
||||
session := sessionsM.Get(sessionId)
|
||||
if session == (sessions.SessionData{}) {
|
||||
return sessions.SessionData{}, ErrAuthSessionNotFound
|
||||
}
|
||||
return session, nil
|
||||
}
|
||||
|
||||
// checks sessionId from cookie
|
||||
// when non-zero session found - pass to next http.Hander
|
||||
// when no session available - render same as login page and redirect to /
|
||||
@@ -72,21 +94,10 @@ func authedPageMiddleware(
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
}
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
sessionCookie, err := r.Cookie(authCookieName)
|
||||
session, err := getRequestSession(r, sessionsM)
|
||||
if err != nil {
|
||||
returnNoAccess(w, r)
|
||||
return
|
||||
}
|
||||
// TODO log here, why i get 'error readin 0'
|
||||
sessionId, err := strconv.Atoi(sessionCookie.Value)
|
||||
if err != nil {
|
||||
returnNoAccess(w, r)
|
||||
return
|
||||
}
|
||||
session := sessionsM.Get(sessionId)
|
||||
if session == (sessions.SessionData{}) {
|
||||
returnNoAccess(w, r)
|
||||
return
|
||||
} else {
|
||||
rerturnSuccess(w, r, session)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user