Devise omniauthable breaks Omniauth authentication with `Could not find a valid mapping for path`

后端 未结 2 1467
我寻月下人不归
我寻月下人不归 2021-01-14 11:02

In my project, I have two type of users: job seekers and hiring managers. Job seekers don\'t have a model, they are just able to apply for jobs using the data received from

2条回答
  •  滥情空心
    2021-01-14 11:28

    Answering my own question. So, final decision was to go with pure Omniauth implementation. I removed :omniauthable from User model, removed config.omniauth... from devise.rb, removed :omniauth_callbacks devise routes from routes.rb. So, all users (no matter what role) would use ame callback routes and hit sessions_controller#authenticate_jobseeker action (should consider renaming the action?):

    def authenticate_jobseeker
      auth_hash = request.env['omniauth.auth']
    
      unless auth_hash.present?
        redirect_to request.env['omniauth.origin'] || root_path, alert: "Sorry, we were not able to authenticate you" and return
      end
    
      @user = User.find_from_oauth(auth_hash)
      if @user.present?
        flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google"
        sign_in_and_redirect @user, :event => :authentication and return
      else
        session[:jobseeker] = auth_hash["info"]
        if valid_job_seeker?
          redirect_to new_job_application_path(...)
        end
      end
    end
    

    and User.find_from_oauth:

    def self.find_from_oauth(auth_hash)
      if auth_hash
        user = User.where(:email => auth_hash["info"]["email"]).first
      end
      user
    end
    

    This implementation satisfied all of the requirements.

提交回复
热议问题