Add logout

This commit is contained in:
Omar Roth 2018-03-22 12:44:36 -05:00
parent 1d42e4465c
commit 2490833b6b
2 changed files with 28 additions and 1 deletions

View file

@ -164,6 +164,14 @@ spawn do
end end
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| get "/" do |env|
templated "index" templated "index"
end end
@ -409,9 +417,15 @@ post "/login" do |env|
login = client.get(login.headers["Location"], headers) login = client.get(login.headers["Location"], headers)
headers = login.cookies.add_request_headers(headers) headers = login.cookies.add_request_headers(headers)
# We are now logged in # 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 "/" env.redirect "/"
rescue ex rescue ex
error_message = "Login failed" error_message = "Login failed"
@ -419,6 +433,15 @@ post "/login" do |env|
end end
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| get "/redirect" do |env|
if env.params.query["q"]? if env.params.query["q"]?
env.redirect env.params.query["q"] env.redirect env.params.query["q"]

View file

@ -25,7 +25,11 @@
</form> </form>
</div> </div>
<div class="pure-u-1 pure-u-md-1-5"> <div class="pure-u-1 pure-u-md-1-5">
<% if env.get "authorized" %>
<a href="/logout" class="pure-menu-heading">Logout</a>
<% else %>
<a href="/login" class="pure-menu-heading">Login</a> <a href="/login" class="pure-menu-heading">Login</a>
<% end %>
</div> </div>
</div> </div>
<%= content %> <%= content %>