# How to logout when using Rake::Auth::Basic in Sinatra

**URL:** <https://www.ruby-forum.com/t/how-to-logout-when-using-rake-basic-in-sinatra/173276>\
**Category:** Ruby\
**Created:** [August 6, 2009, 5:18am UTC](https://www.ruby-forum.com/t/how-to-logout-when-using-rake-basic-in-sinatra/173276 "2009-08-06T05:18:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [August 6, 2009, 5:18am UTC](https://www.ruby-forum.com/t/how-to-logout-when-using-rake-basic-in-sinatra/173276/1 "2009-08-06T05:18:34Z")

</div>

Hi all,

Sorry for the repost - I think I have the correct forum now!

I’m writting a small app that requires basic authentication in Sinatra.  
I followed the advice of the official Sinatra faq  
([Sinatra: Frequently Asked Questions](http://www.sinatrarb.com/faq.html#auth)) and have implemented this code  
with success:

```auto
require 'rubygems'
require 'sinatra'

helpers do

  def protected!
    response['WWW-Authenticate'] = %(Basic realm="Testing HTTP Auth")
and \
    throw(:halt, [401, "Not authorized\n"]) and \
    return unless authorized?
  end

  def authorized?
    @auth ||= Rack::Auth::Basic::Request.new(request.env)
    @auth.provided? && @auth.basic? && @auth.credentials &&
@auth.credentials == ['admin', 'admin']
  end

end

get '/' do
  "Everybody can see this page"
end

get '/protected' do
  protected!
  "Welcome, authenticated client"
end
```

So I get a window asking me to ender my credentials when going to  
/protected and it logs me in. Once logged in though, I would like to be  
able to log out. I know the solution to this must be super easy but I  
just can’t get it to work.

Any help is greatly appreciated. Thanks!

-Tony

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [August 6, 2009, 2:06pm UTC](https://www.ruby-forum.com/t/how-to-logout-when-using-rake-basic-in-sinatra/173276/2 "2009-08-06T14:06:13Z")

</div>

On Thu, Aug 6, 2009 at 4:18 AM, Tony T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So I get a window asking me to ender my credentials when going to  
> /protected and it logs me in. Once logged in though, I would like to be  
> able to log out. I know the solution to this must be super easy but I  
> just can’t get it to work.

Actually, this may be a limitation of HTTP basic authentication - there  
is  
no way to inform the browser that you wish for its credentials cache to  
expire.

I am not certain of this, but I have seen this complaint raised before.  
You  
might want to follow this up and see if the problem is a general one,  
before  
banging your head trying to find a Sinatra specific solution.

Richard

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [August 6, 2009, 2:57pm UTC](https://www.ruby-forum.com/t/how-to-logout-when-using-rake-basic-in-sinatra/173276/3 "2009-08-06T14:57:44Z")

</div>

Correct. HTTP Basic Authentication is done via the browser. The only  
real  
way to ‘log out’ of HTTP Basic Authentication is to clear ALL of your  
authenticated sessions via your browser settings. If you want an  
authentication scheme that you can truly log out of, you’ll have to use  
something like a session-based scheme.

–  
Bryan
