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

No access_token through api when signing in by finding user. #1087

Closed
sparshsamir1993 opened this issue Feb 10, 2018 · 3 comments
Closed

No access_token through api when signing in by finding user. #1087

sparshsamir1993 opened this issue Feb 10, 2018 · 3 comments

Comments

@sparshsamir1993
Copy link

sparshsamir1993 commented Feb 10, 2018

So I have a running ionic app that works perfectly when signing in through email and password by the user using this gem on my rails app.

In the app, i use native cordova facebook plugin to get the 'access-token' , then I pass it on to my users controller inside the api namespace. Use the koala gem to get the user details and search user by email address and sign them in.

Here is my users controller.
`class Api::V1::UsersController < Api::V1::BaseController

skip_before_filter :verify_authenticity_token
require 'koala'
include DeviseTokenAuth::Concerns::SetUserByToken

def index
   users = User.all

   render(
     json: ActiveModel::ArraySerializer.new(
       users,
       each_serializer: Api::V1::UserSerializer,
       root: 'users',
     )
   )
 end
def show
  @user = User.find(params[:id])
  render(json: Api::V1::UserSerializer.new(@user).to_json)
end

def authenticatFacebookToken
  @graph = Koala::Facebook::API.new(params[:'access-token'])
  profile = @graph.get_object('me', fields:'email,first_name,last_name')
  @user = User.find_or_create_by(email: profile['email'])
  if @user.update('access-token': params[:'access-token'])
    puts "------------"
    sign_in (@user)
    session[:user_id] = @user.id
    byebug   #(If i stop here and check user_signed_in? it return false)
    render(json: Api::V1::UserSerializer.new(@user).to_json)
    # redirect_to api_v1_user_session_path(:email=>@user.email, :password=>Devise.friendly_token[0,20]), :method=>:post
    # render(json: Api::V1::UserSerializer.new(@user).to_json)
  end
end

end
`

and here are my routes

` namespace :api do

        namespace :v1 do
    
          resources :users, only:[:show]
    mount_devise_token_auth_for 'User', at: 'auth', skip: [:omniauth_callbacks],
        controllers:{
            omniauth_callbacks: 'api/v1/users'
        }
    devise_scope :users do
           post '/auth/authenticatFacebookToken' => 'users#authenticatFacebookToken', as: :fbtokenauth
    end
end

end
`

the only things I receive in headers are
"{"content-type":["application/json; charset=utf-8"],"cache-control":["max-age=0"," private"," must-revalidate"]}"
Any better way of signing in ?

@sparshsamir1993
Copy link
Author

Solved this issue by generating my own headers. :/

@DonGiulio
Copy link

Could you please post your solution?

@sparshsamir1993
Copy link
Author

sure..! Here's the function.

def authenticatFacebookToken
      @graph = Koala::Facebook::API.new(params[:'access-token'])
      profile = @graph.get_object('me', fields:'email,first_name,last_name')
      @user = User.find_or_create_by(email: profile['email'])
      if @user.update('access-token': params[:'access-token'])
        puts "------------"
        sign_in @user
        @client_id = SecureRandom.urlsafe_base64(nil, false)
        @token     = SecureRandom.urlsafe_base64(nil, false)
        @user.tokens[@client_id] = {
          token: BCrypt::Password.create(@token),
          expiry: (Time.now + DeviseTokenAuth.token_lifespan).to_i
        } 
        auth_header = @user.build_auth_header(@token, @client_id)
        # update the response header
        response.headers.merge!(auth_header)
        
        render(json: Api::V1::UserSerializer.new(@user).to_json)
      
        # sign_in_and_redirect (@user)
        # session[:user_id] = @user.id
        # current_user = @user
        
        # redirect_to api_v1_user_session_path(:email=>@user.email, :password=>Devise.friendly_token[0,20]), :method=>:post
        # render(json: Api::V1::UserSerializer.new(@user).to_json)
      end
      # if @user.present?
        
      #   render(json: Api::V1::UserSerializer.new(@user).to_json)
      #   set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
      # end
      
    end

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

2 participants