feat: login form or room info on index page

This commit is contained in:
efim
2023-11-18 12:18:56 +00:00
parent 0e804b21e5
commit c8f28bf0de
8 changed files with 113 additions and 93 deletions

View File

@@ -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