Skip to content

Instantly share code, notes, and snippets.

@bigfleet
Last active December 26, 2015 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bigfleet/7088190 to your computer and use it in GitHub Desktop.
Save bigfleet/7088190 to your computer and use it in GitHub Desktop.
class CreateAuthentications < ActiveRecord::Migration
def change
create_table :authentications do |t|
t.integer :user_id
t.string :provider
t.string :uid
t.string :name
t.string :oauth_token
t.datetime :oauth_expires_at
t.timestamps
end
end
end
class Authentication < ActiveRecord::Base
belongs_to :user
def self.from_omniauth(user, auth)
where(auth.slice(:provider, :uid)).first_or_initialize.tap do |authentication|
authentication.user = user
authentication.provider = auth.provider
authentication.uid = auth.uid
authentication.name = auth.info.name
authentication.oauth_token = auth.credentials.token
authentication.oauth_expires_at = Time.at(auth.credentials.expires_at)
authentication.save!
end
end
end
.authentications {
margin-bottom: 30px;
}
.authentication {
width: 130px;
float: left;
background-color: #EEE;
border: solid 1px #999;
padding: 5px 10px;
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
position: relative;
margin-right: 10px;
}
.authentication .remove {
text-decoration: none;
position: absolute;
top: 3px;
right: 3px;
color: #333;
padding: 2px 4px;
font-size: 10px;
}
.authentication .remove:hover {
color: #CCC;
background-color: #777;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
}
.authentication img {
float: left;
margin-right: 10px;
}
.authentication .provider {
font-weight: bold;
}
.authentication .uid {
color: #666;
font-size: 11px;
}
.auth_provider img {
display: block;
}
.auth_provider {
float: left;
text-decoration: none;
margin-right: 20px;
text-align: center;
margin-bottom: 10px;
}
class AuthenticationsController < ApplicationController
def index
@authentications = current_user.authentications if current_user
end
def create
auth = Authentication.from_omniauth(current_user, env["omniauth.auth"])
flash[:notice] = "Authentication successful."
redirect_to authentications_url
end
def destroy
@authentication = current_user.authentications.find(params[:id])
@authentication.destroy
flash[:notice] = "Successfully destroyed authentication."
redirect_to authentications_url
end
end
// Additional JS functions here
window.fbAsyncInit = function() {
FB.init({
appId : 400659536727696, // App ID
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
function fblogin() {
FB.getLoginStatus(function(response) {
if(response.status == "connected") {
location.href =
'/auth/facebook/callback?' +
$.param({ signed_request: response.authResponse.signedRequest })
} else {
FB.login(function(response) {
if (response.authResponse) {
'/auth/facebook/callback?' +
$.param({ signed_request: response.authResponse.signedRequest })
}
})
}
})
};
<% if @authentications %>
<% unless @authentications.empty? %>
<p><strong>You can sign in to this account using:</strong></p>
<div class="authentications">
<% for authentication in @authentications %>
<div class="authentication">
<%= image_tag "#{authentication.provider}_32.png", :size => "32x32" %>
<div class="provider"><%= authentication.provider.titleize %></div>
<div class="uid"><%= authentication.uid %></div>
<%= link_to "X", authentication, :confirm => 'Are you sure you want to remove this authentication option?', :method => :delete, :class => "remove" %>
</div>
<% end %>
<div class="clear"></div>
</div>
<% end %>
<p><strong>Add another service to sign in with:</strong></p>
<% else %>
<p><strong>Sign in through one of these services:</strong></p>
<% end %>
<!--
<a href="/auth/twitter" class="auth_provider">
<%= image_tag "twitter_64.png", :size => "64x64", :alt => "Twitter" %>
Twitter
</a>
-->
<% unless @authentications.select{ |a| a.provider == "facebook" }.any? %>
<%= link_to_function image_tag("facebook_64.png", :size => "64x64", :alt => "Facebook"), 'fblogin()' %>
<% end %>
<!--
<a href="/auth/google_apps" class="auth_provider">
<%= image_tag "google_64.png", :size => "64x64", :alt => "Google" %>
Google
</a>
-->
<div class="clear"></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment