Search code examples
ruby-on-railsdeviseomniauthaccess-token

No route matches [POST] OmniAuth Steam Callback


I'm building Rails API for AngularJS app. I'm using devise_token_auth and omniauth-steam gems. When I try to authenticate user using the Steam there is an error:

ActionController::RoutingError (No route matches [POST] "/omniauth/steam/callback"

I've added devise_token_auth routes, but they don't create POST callbacks. I've tried to manually create POST route for callback, but it hasn't worked and I'm not sure if it is even correct solution. I'm trying to solve this problem from yesterday and I can't find anyone with the similar one.

config/routes.rb

Rails.application.routes.draw do
  namespace 'api' do
    namespace 'v1' do
      mount_devise_token_auth_for 'Api::V1::User', at: 'auth'
    end
  end
end

config/initializers/omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :steam, ENV['steam_api_key']
end

I use figaro gem and save steam_api_key in application.yml file.

rake routes task

                                Prefix Verb   URI Pattern                               Controller#Action
        new_api_v1_api_v1_user_session GET    /api/v1/auth/sign_in(.:format)            devise_token_auth/sessions#new
            api_v1_api_v1_user_session POST   /api/v1/auth/sign_in(.:format)            devise_token_auth/sessions#create
    destroy_api_v1_api_v1_user_session DELETE /api/v1/auth/sign_out(.:format)           devise_token_auth/sessions#destroy
           api_v1_api_v1_user_password POST   /api/v1/auth/password(.:format)           devise_token_auth/passwords#create
       new_api_v1_api_v1_user_password GET    /api/v1/auth/password/new(.:format)       devise_token_auth/passwords#new
      edit_api_v1_api_v1_user_password GET    /api/v1/auth/password/edit(.:format)      devise_token_auth/passwords#edit
                                       PATCH  /api/v1/auth/password(.:format)           devise_token_auth/passwords#update
                                       PUT    /api/v1/auth/password(.:format)           devise_token_auth/passwords#update
cancel_api_v1_api_v1_user_registration GET    /api/v1/auth/cancel(.:format)             devise_token_auth/registrations#cancel
       api_v1_api_v1_user_registration POST   /api/v1/auth(.:format)                    devise_token_auth/registrations#create
   new_api_v1_api_v1_user_registration GET    /api/v1/auth/sign_up(.:format)            devise_token_auth/registrations#new
  edit_api_v1_api_v1_user_registration GET    /api/v1/auth/edit(.:format)               devise_token_auth/registrations#edit
                                       PATCH  /api/v1/auth(.:format)                    devise_token_auth/registrations#update
                                       PUT    /api/v1/auth(.:format)                    devise_token_auth/registrations#update
                                       DELETE /api/v1/auth(.:format)                    devise_token_auth/registrations#destroy
       api_v1_api_v1_user_confirmation POST   /api/v1/auth/confirmation(.:format)       devise_token_auth/confirmations#create
   new_api_v1_api_v1_user_confirmation GET    /api/v1/auth/confirmation/new(.:format)   devise_token_auth/confirmations#new
                                       GET    /api/v1/auth/confirmation(.:format)       devise_token_auth/confirmations#show
            api_v1_auth_validate_token GET    /api/v1/auth/validate_token(.:format)     devise_token_auth/token_validations#validate_token
                   api_v1_auth_failure GET    /api/v1/auth/failure(.:format)            devise_token_auth/omniauth_callbacks#omniauth_failure
                                       GET    /api/v1/auth/:provider/callback(.:format) devise_token_auth/omniauth_callbacks#omniauth_success
                                       GET    /omniauth/:provider/callback(.:format)    devise_token_auth/omniauth_callbacks#redirect_callbacks
                      omniauth_failure GET    /omniauth/failure(.:format)               devise_token_auth/omniauth_callbacks#omniauth_failure
                                       GET    /api/v1/auth/:provider(.:format)          redirect(301)

I know that it's a bit messy because of namespaces, but it shouldn't cause this problem, right?

EDIT: I did some research and here is the link https://github.com/lynndylanhurley/devise_token_auth#usage-tldr it says that /:provider/callback url should have GET/POST action, but as we can see I don't have POST action for callback.


Solution

  • Finally I've solved this problem by adding below line to the routes.rb file.

    post '/omniauth/steam/callback', to: 'overrides/omniauth_callbacks#redirect_callbacks'
    

    I've created omniauth_callbacks_controller.rb file in controllers/overrides folder and removed below line.

    skip_before_action :set_user_by_token, raise: false
    

    The last step was editing line with redirect route. I've changed this:

    redirect_route = "#{request.protocol}#{request.host_with_port}/#{Devise.mappings[devise_mapping].fullpath}/#{params[:provider]}/callback"
    

    To hard-coded route.

    redirect_route = "/api/v1/auth/steam/callback"