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
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 |
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
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 |
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
.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; | |
} |
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
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 |
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
// 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 }) | |
} | |
}) | |
} | |
}) | |
}; |
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
<% 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" %> | |
</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" %> | |
</a> | |
--> | |
<div class="clear"></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment