2016-07-01 46 views
8

Wiem, że to pytanie zostało zadane wiele razy, ale odpowiedzi nigdy nie są w pełni do przyjęcia.Szyny 5, Devise, Omniauth, Twitter

Więc podążam za na ten temat i mieszam to z oficjalnym Devise Omniauth guide (który jest oparty na FB), ale po prostu nie dostaję go tak, jak się spodziewam, więc chciałbym pomóc.

Mam Users::OmniauthCallbacksController, który wygląda tak:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController 
    def all 
    @user = User.from_omniauth(request.env["omniauth.auth"]) 

    if @user.persisted? 
     sign_in_and_redirect root_path, :event => :authentication #this will throw if @user is not activated 
     set_flash_message(:notice, :success, :kind => "Twitter") if is_navigational_format? 
    else 
     session["devise.twitter_data"] = request.env["omniauth.auth"].except("extra") 
     flash[:notice] = flash[:notice].to_a.concat resource.errors.full_messages 
     redirect_to new_user_registration_url 
    end 
    end 

    alias_method :twitter, :all 

    def failure 
    redirect_to root_path 
    end 
end 

Wtedy też mają dwie metody na moim User.rb

def self.from_omniauth(auth) 
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 
     user.update(
     email: auth.info.email, 
     password: Devise.friendly_token[0,20], 
     username: auth.info.nickname, 
     remote_avatar_url: auth.info.image, 
     token: auth.credentials.token, 
     secret: auth.credentials.secret 
    ) 
    end 
    end 

    def self.new_with_session(params, session) 
    super.tap do |user| 
     if data = session["devise.twitter_data"] 
     # user.attributes = params 
     user.update(
      email: params[:email], 
      password: Devise.friendly_token[0,20], 
      username: data["info"]["nickname"], 
      remote_avatar_url: data["info"]["image"], 
      token: data["credentials"]["token"], 
      secret: data["credentials"]["secret"] 
     ) 
     end 
    end 
    end 

biegnę do różnych problemów. Najprostsze jest to, że ustawiam hasło, użytkownik nie zna hasła przy próbie zalogowania się (i nie loguję się automatycznie po potwierdzeniu).

Ale jeśli nie ustawię hasła, to nie poprosi ich o ustawienie hasła ... więc to jest trochę dziwne.

Są to moje ustawienia opracować na moim User model:

devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable, 
     :confirmable, :omniauthable, :omniauth_providers => [:twitter] 

    validates :username, 
    presence: true, 
    uniqueness: { 
     case_sensitive: false 
    } 

    validate :validate_username 

    def validate_username 
    if User.where(email: username).exists? 
     errors.add(:username, :invalid) 
    end 
    end 

Więc moje pytanie jest takie, gdy ktoś zarejestruje się za pośrednictwem Twittera, czy muszą wprowadzić hasło? Automatycznie wysyłam je na numer registration/new.html.erb, ponieważ Twitter nie zwraca wartości e-mail. Ale próbuję po prostu uruchomić proces, zanim go zoptymalizuję.

Jak radzić sobie z problemem z hasłem?

Edycja 1

Dla większej jasności, będę mieć do czynienia z tym numerze password_required niezależnie od dostawcy OAuth.

Jak zatem zmienić to wymaganie dla wszystkich dostawców OAuth?

Odpowiedz

5

Należy dodać the following method do klasy User:

def password_required? 
    (provider.blank? || uid.blank?) && super 
end 

Od Twitter nie zwraca e-mail użytkownika, można też chcą tweak that email validation, ale przekierowanie użytkownika do registration/new.html.erb jak już robisz wydaje się właściwe podejście do mnie.