From 2490833b6b844ff6dfd5bf8484ae563119422e5d Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Thu, 22 Mar 2018 12:44:36 -0500 Subject: [PATCH] Add logout --- src/invidious.cr | 25 ++++++++++++++++++++++++- src/views/layout.ecr | 4 ++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/invidious.cr b/src/invidious.cr index 73fb339f8..969d22473 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -164,6 +164,14 @@ spawn do end end +before_all do |env| + if env.request.cookies["SID"]? + env.set "authorized", true + else + env.set "authorized", false + end +end + get "/" do |env| templated "index" end @@ -409,9 +417,15 @@ post "/login" do |env| login = client.get(login.headers["Location"], headers) headers = login.cookies.add_request_headers(headers) - # We are now logged in + login.cookies.each do |cookie| + host = URI.parse(env.request.headers["Host"]).host + cookie.extension = cookie.extension.not_nil!.gsub(".youtube.com", host) + end + + login.cookies.add_response_headers(env.response.headers) + env.redirect "/" rescue ex error_message = "Login failed" @@ -419,6 +433,15 @@ post "/login" do |env| end end +get "/logout" do |env| + env.request.cookies.each do |cookie| + cookie.expires = Time.new(1990, 1, 1) + end + + env.request.cookies.add_response_headers(env.response.headers) + env.redirect "/" +end + get "/redirect" do |env| if env.params.query["q"]? env.redirect env.params.query["q"] diff --git a/src/views/layout.ecr b/src/views/layout.ecr index f419ec587..f0ce5a3fe 100644 --- a/src/views/layout.ecr +++ b/src/views/layout.ecr @@ -25,7 +25,11 @@
+ <% if env.get "authorized" %> + Logout + <% else %> Login + <% end %>
<%= content %>