Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
OmniAuth login in popup
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 %>! &middot; <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>
@kennym
Copy link

kennym commented Jul 24, 2014

Thanks a lot! Helped me a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment