Last active
December 19, 2015 05:19
-
-
Save chocnut/5903444 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
attr_accessible :provider, :uid | |
def self.find_by_provider_and_uid(provider, uid) | |
where(provider: provider, uid: uid).first | |
end | |
end |
This file contains hidden or 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
F.... | |
Failures: | |
1) OmniauthCallbacksController#guest user facebook email doesn't exist creates authentication using facebook | |
Failure/Error: get :facebook | |
NoMethodError: | |
undefined method `[]' for nil:NilClass | |
# ./app/controllers/omniauth_callbacks_controller.rb:4:in `all' | |
# ./spec/controllers/omniauth_callbacks_controller_spec.rb:18:in `block (4 levels) in <top (required)>' | |
Finished in 0.53103 seconds | |
5 examples, 1 failure | |
Failed examples: | |
rspec ./spec/controllers/omniauth_callbacks_controller_spec.rb:17 # OmniauthCallbacksController#guest user facebook email doesn't exist creates authentication using facebook |
This file contains hidden or 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 OmniauthCallbacksController < Devise::OmniauthCallbacksController | |
def all | |
omniauth = request.env["omniauth.auth"] | |
authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid']) | |
if authentication | |
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => omniauth['provider'] | |
sign_in_and_redirect authentication.user | |
else | |
user = User.where(:email => omniauth['info']['email']).first | |
if user | |
user.name = omniauth['info']['name'] | |
user.authentications.create!(provider: omniauth['provider'], uid: omniauth['uid']) | |
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => omniauth['provider'] | |
sign_in_and_redirect user | |
else | |
flash[:alert] = "Registrations are not open yet, but please check back soon" | |
redirect_to root_path | |
end | |
end | |
end | |
# todo add other provider | |
alias_method :facebook, :all | |
end |
This file contains hidden or 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 'spec_helper' | |
describe OmniauthCallbacksController do | |
include Devise::TestHelpers | |
before do | |
request.env["devise.mapping"] = Devise.mappings[:user] | |
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook] | |
end | |
describe "#guest user" do | |
context "facebook email doesn't exist" do | |
before(:each) do | |
@user = Fabricate(:user) | |
omniauth_mock('facebook', @user.email, @user.name) | |
end | |
it "creates authentication with facebook" do | |
get :facebook | |
end | |
end | |
end | |
end |
This file contains hidden or 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
OmniAuth.config.test_mode = true | |
OmniAuth.config.add_mock(:facebook, { | |
:provider => "facebook", | |
:uid => "1234", | |
:info => { | |
:name => "Juan Tamad", | |
:email => "[email protected]" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment