Module: Sinatra::BrowserID::Helpers

Defined in:
lib/sinatra/browserid.rb

Instance Method Summary collapse

Instance Method Details

#authorize!Object

If the current user is not logged in, redirects to a login page. Override the login page by setting the Sinatra option :browserid_login_url.



22
23
24
25
26
# File 'lib/sinatra/browserid.rb', line 22

def authorize!
  session[:authorize_redirect_url] = request.url
   = settings.
  redirect  unless authorized?
end

#authorized?Boolean

Returns true if the current user has logged in and presented a valid assertion.

Returns:

  • (Boolean)


15
16
17
# File 'lib/sinatra/browserid.rb', line 15

def authorized?
  ! session[:browserid_email].nil?
end

#authorized_emailObject

Returns the BrowserID verified email address, or nil if the user is not logged in.



35
36
37
# File 'lib/sinatra/browserid.rb', line 35

def authorized_email
  session[:browserid_email]
end

#logout!Object

Logs out the current user.



29
30
31
# File 'lib/sinatra/browserid.rb', line 29

def logout!
  session[:browserid_email] = nil
end

#render_login_button(redirect_url = nil) ⇒ Object

Returns the HTML to render the BrowserID login button. Optionally takes a URL parameter for where the user should be redirected to after the assert POST back. You can customize the button image by setting the Sinatra option :browserid_login_button to a color (:orange, :red, :blue, :green, :grey) or an actual URL.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sinatra/browserid.rb', line 45

def (redirect_url = nil)
  case settings.
  when :orange, :red, :blue, :green, :grey
    button_url = "#{settings.browserid_url}/i/sign_in_" \
                 "#{settings..to_s}.png"
  else
    button_url = settings.
  end

  if session[:authorize_redirect_url]
    redirect_url = session[:authorize_redirect_url]
    session[:authorize_redirect_url] = nil
  end
  redirect_url ||= request.url

  template = ERB.new(Templates::LOGIN_BUTTON)
  template.result(binding)
end