Skip to content

Instantly share code, notes, and snippets.

@IsmailM
Forked from nu7hatch/app.rb
Created March 15, 2016 23:58
Show Gist options
  • Save IsmailM/3e29827dd128330061da to your computer and use it in GitHub Desktop.
Save IsmailM/3e29827dd128330061da to your computer and use it in GitHub Desktop.
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment