A
A
Anton Misyagin2016-04-21 23:31:02
Ruby on Rails
Anton Misyagin, 2016-04-21 23:31:02

Yandex OAuth login not working?

Rails 4.2.5+Devise 3.5.1
Gemfile: https://rubygems.org/gems/omniauth-yandex/versions... config/secrets.yml:
gem 'omniauth-yandex'

development:
  yandex_auth_app_id: xxxx
  yandex_auth_secret: yyyy

initializers/devise.rb:
config.omniauth	:twitter,
  					Rails.application.secrets.twitter_app_id,
  					Rails.application.secrets.twitter_app_secret
  config.omniauth	:facebook,
  					Rails.application.secrets.facebook_app_id,
  					Rails.application.secrets.facebook_app_secret
  config.omniauth	:vkontakte,
  					Rails.application.secrets.vkontakte_app_id,
  					Rails.application.secrets.vkontakte_app_secret
  config.omniauth :yandex,
            Rails.application.secrets.yandex_auth_app_id,
            Rails.application.secrets.yandex_auth_secret

controllers/users/omniauth_callback_controller.rb:
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  include SearchLoadFromSession
  def vkontakte
    provider_login 'ВКонтакте'
  end

  def twitter
    provider_login 'Twitter'
  end

  def facebook
    provider_login 'Facebook'
  end

  def yandex
    provider_login 'Яндекс'
  end

private
  def provider_login kind
    auth = request.env['omniauth.auth']
    @user = User.from_omniauth auth
    if @user.persisted?
      sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
      set_flash_message(:notice, :success, :kind => kind) if is_navigational_format?
    else
      flash[:error] = 'Ошибка авторизации.'
      redirect_to root_path
    end
  end
end

config/routes.rb:
Rails.application.routes.draw do
  devise_for :users, :controllers => { :omniauth_callbacks => 'users/omniauth_callbacks', 
    :registrations => 'users/registrations', 
    :confirmations => 'users/confirmations',
    :sessions=> 'users/sessions' },
    :path => '',
    :path_names => { :sign_in => 'вход', :sign_out => 'выход', :sign_up => 'регистрация'}

...
end

models/user.rb
class User < ActiveRecord::Base
  devise 	:database_authenticatable, :registerable,
      :recoverable, :trackable, :validatable, :confirmable,
      :omniauthable,
      :omniauth_providers => [:twitter, :facebook, :vkontakte, :yandex]
.......

VK, Twitter, Facebook work. Only Yandex does not work
62ac9622471249b39954c219d39accef.pngc9f2287518db43628a2b4b05ea8d26f1.png96d71358b42b4f14a94d8b57ac984e82.png
After confirming access by the user, the following should happen (from the Yandex documentation):
Yandex.OAuth redirects the user to the address specified in the Callback URL field when registering the application. The confirmation code (or description of the error) is passed in the redirect URL parameter.
The application receives the redirect address and retrieves the verification code.
The application sends a POST request with a code.
Yandex.OAuth returns a token in the response body.

694e5e331f6e48c498a9ffca033cb0e7.pngb81a8c411a954c2194a9fb780c94cf8d.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question