Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cant login after signing in via oauth #1010

Closed
sushant12 opened this issue Nov 9, 2017 · 4 comments
Closed

Cant login after signing in via oauth #1010

sushant12 opened this issue Nov 9, 2017 · 4 comments

Comments

@sushant12
Copy link

I have a rails-api app and I added the gem 'omniauth-google-oauth2' . Everything is working fine up to showing an oauth screen and then clicking next to signup and the user is also saved into the db as well. But after that, I get an err saying 'Either signup or log in'. This is happening because I have a before_action :authenticate_api_employee! on my controllers.

I did some research and found out that the current_api_employee is nil as well. Any idea how this can be fixed?

I have overridden the omniauth_callbacks_controller and added a debugger there and current_api_employee have a value but it s value is set to nil after I am signed in and redirected.

Do i need to add ?origin=http:/..... to the oauth url ? It looks like the omniauth_success needs that url to redirect properly but I am overriding the omniauth_success method.

Would really appreciate your help

My ApplicationController is extending from ActionController::API .

My Omniauth controller

module Overrides
  class OmniauthCallbacksController < DeviseTokenAuth::OmniauthCallbacksController

  	def omniauth_success
      super do |resource|
        if omniauth_window_type.present? # auth requested from angular
           sign_in resource
       else  # if backend standalone auth
       		redirect_to '/test' and return
       		# sign_in_and_redirect test_path and return
        end
      end
    end


    # break out provider attribute assignment for easy method extension
    def assign_provider_attrs(user, auth_hash)
      user.assign_attributes({
         email: auth_hash['info']['email'],
         name: auth_hash['info']['name'],
         admin: true,
         first_name: CustomRegex.japanese?(auth_hash['info']["first_name"]) ? '' : auth_hash['info']["first_name"],
         last_name: CustomRegex.japanese?(auth_hash['info']["last_name"]) ? '' : auth_hash['info']["last_name"],
         uqid: SecureRandom.uuid,
         company_id: 1,
         timeline: 0
     	})
    end
  end
end

My Gemfile

gem 'devise_token_auth'
gem 'omniauth'
gem 'omniauth-google-oauth2'
ruby '2.3.0'
ENV['NOKOGIRI_USE_SYSTEM_LIBRARIES'] = 'YES'
gem 'rails', '4.2.6'
gem 'rails-api', '~> 0.4.0'
@zachfeldman
Copy link
Contributor

@sushant12 are you going to explain how you fixed your problem in case anyone else has it? That would be helpful!

@sushant12
Copy link
Author

@zachfeldman i made a very rookie mistake. After I was redirected to auth_origin_url with tokens I used that token and sent it in the header to make a call to my apis.

@zachfeldman
Copy link
Contributor

Ok!

@rg-najera
Copy link

Just in case someone needed to see how this is done on the client side. Posted a work around on redux-token-auth kylecorbelli/redux-token-auth#28 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants