ben

OMÜ , Bilgisayar Mühendisliği, 13'

19 Ağustos 2015 Çarşamba

rails,devise,omniouth, facebook

https://developers.facebook.com/

myApps=> Add a new App

  İnternet sitesini seçelim.


Gelen ekrandan kategori seçip CreateApp Id 'ye tıklıyoruz.

Genel ayarları skip ile geçip projemizin akranına geliyoruz. Burdan settings =>Contact email kısmını dolduralım. Bu alan bos kaldığında çalışmaz

aynı ekrandan app platform=> site Url=> http://localhost:3000/  =>save

sataus&review



   Görünümünü bu şekilde açtıktan sonra elde ettiğimiz Id ve secret Key ile Rila uygulamıza dönelim.

Google giriş yazısındaki * lı yerleri değiştiricez diğer işlemler burdada aynı.


gem 'omniauth-facebook'



initializers/devise.rb

require "omniauth-facebook"
   config.omniauth :facebook, "1478721079089377", "3907942b5ed0691652861ef68915a4a2",
                   scope: 'email, public_profile', info_fields: 'email, name',
                :client_options => {:ssl => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'}}





app/models/user.rb
 
    devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :omniauthable, :omniauth_providers => [:facebook]






app/controllers/users/omniauth_callbacks_controller.rb bu dosyayı oluşturalım

def facebook

        if request.env["omniauth.auth"].info.email.blank?
              redirect_to "/users/auth/facebook?auth_type=rerequest&scope=email"
        end

        @user = User.from_omniauth(request.env["omniauth.auth"])
        @user.image=request.env["omniauth.auth"].info.image


        if @user.persisted?
          sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
          set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
        else
          session["devise.facebook_data"] = request.env["omniauth.auth"]
          redirect_to new_user_registration_url
        end
    end





 app/models/user.rb

def self.from_omniauth(auth)
     
      where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
        user.email = auth.info.email
        user.password = Devise.friendly_token[0,20]
        user.name = auth.info.name   # assuming the user model has a name
        user.image = auth.info.image # assuming the user model has an image

      end
    end

    def self.new_with_session(params, session)
        super.tap do |user|
          if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
            data.merge!(session["devise.facebook_data"]["info"])
            data.merge!(session["devise.facebook_data"]["extra"]["raw_info"])
            user.email = data["email"] if user.email.blank?
            user.provider = data["provider"] if user.provider.blank?
            user.uid = data["uid"] if user.uid.blank?
            user.image = data["image"]
         end
        end
    end




Çalıştırıp bakalım:




urlde ki '#_=_' karakterleri sıfırlamak için application.erb dosyasına:

<script type="text/javascript">
 if (window.location.hash && window.location.hash == '#_=_') {
        if (window.history && history.pushState) {
            window.history.pushState("", document.title, window.location.pathname);
        } else {
            // Prevent scrolling by storing the page's current scroll offset
            var scroll = {
                top: document.body.scrollTop,
                left: document.body.scrollLeft
            };
            window.location.hash = '';
            // Restore the scroll offset, should be flicker free
            document.body.scrollTop = scroll.top;
            document.body.scrollLeft = scroll.left;
        }
    }
</script>




Twitter&Facebook&Google girişinin olduğu proje burda mevcuttur.