X-Git-Url: https://code.octet-stream.net/broadcaster/blobdiff_plain/c94fef11f43279165f39680fa0b0922c86702687..a098a4fcbfe0649cfd9d171e59e822a4bd1b2ec4:/server/session.go?ds=inline diff --git a/server/session.go b/server/session.go index 4b4c445..a097989 100644 --- a/server/session.go +++ b/server/session.go @@ -17,26 +17,21 @@ func generateSession() string { return hex.EncodeToString(b) } -func currentUser(w http.ResponseWriter, r *http.Request) (User, error) { - // todo: check if user actually exists and is allowed to log in +func currentUser(_ http.ResponseWriter, r *http.Request) (User, error) { cookie, e := r.Cookie("broadcast_session") if e != nil { return User{}, e } - username, e := db.GetUserForSession(cookie.Value) - if e != nil { - return User{}, e - } - return User{username: username}, nil + return users.GetUserForSession(cookie.Value) } -func createSessionCookie(w http.ResponseWriter) { +func createSessionCookie(w http.ResponseWriter, username string) { sess := generateSession() log.Println("Generated a random session", sess) expiration := time.Now().Add(365 * 24 * time.Hour) cookie := http.Cookie{Name: "broadcast_session", Value: sess, Expires: expiration, SameSite: http.SameSiteLaxMode} - db.InsertSession("admin", sess, expiration) + db.InsertSession(username, sess, expiration) http.SetCookie(w, &cookie) }