Created
April 21, 2011 03:05
-
-
Save nu7hatch/933619 to your computer and use it in GitHub Desktop.
OmniAuth login in popup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'sinatra' | |
require 'omniauth/oauth' | |
set :sessions, true | |
set :layout, true | |
use OmniAuth::Builder do | |
provider :twitter, 'key', 'secret' | |
end | |
get "/auth/twitter/callback" do | |
auth = request.env["omniauth.auth"] | |
session[:user] = auth['user_info']['name'] | |
erb :callback | |
end | |
get "/login" do | |
erb :login | |
end | |
get "/logout" do | |
session.delete(:user) | |
redirect "/" | |
end | |
get "/" do | |
@user = session[:user] | |
erb :home | |
end | |
__END__ | |
@@layout | |
<html> | |
<head><title>OmniAuth popups</title></head> | |
<body> | |
<%= yield %> | |
</body> | |
</html> | |
@@home | |
<script> | |
function openWindow(url, title, w, h) { | |
var popup = window.open(url, title, | |
'width='+w+', height='+h+', modal=no, resizable=no, toolbar=no, menubar=no,'+ | |
'scrollbars=no, alwaysRaise=yes' | |
); | |
popup.resizeBy(0, 50); | |
} | |
</script> | |
<% if @user %> | |
Hello <%= @user %>! · <a href="/logout">Logout</a> | |
<% else %> | |
<a href="javascript:openWindow('/auth/twitter', 'Login with Twitter', 880, 380);">Login with Twitter</a><br /> | |
<% end %> | |
@@callback | |
<script> | |
window.opener.location.href = "/"; | |
window.close(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot! Helped me a lot