ruby on rails - devise token auth + Save Facebook info -
i using devise auth token gem , ng-token auth authenticating single page app. besides registering email there option sign-up via facebook. works.
my problem fb request scope set user_friends, email , public_info, when request don't see auth_hash request.env['omniauth.auth']. described in omniauth-facebook gem documentation.
basically want save fb gives user in database. how it?
i use end-point ionic2 app, facebook login requests on rails 5 api, using koala , devise token auth should able replicate on rails 5 app:
def facebook @graph = koala::facebook::api.new(oauth_params[:token], env["facebook_secret"]) @profile = @graph.get_object("me?fields=email,first_name,last_name,picture.type(large)") unless @user = user.where(email: @profile["email"])&.first @user = user.new(email: @profile["email"]) end # # here make logic saving facebook's data on user model, # customize accordingly # if @user.new_record? p = securerandom.urlsafe_base64(nil, false) @user.password = p @user.password_confirmation = p # # "passwordless" login, user must set password after registering, # or forever haunted indecipherable securerandom value # @user.name = @profile["first_name"]+ " " + @profile["last_name"] @user.remote_avatar_url = @profile["picture"]["data"]["url"] @user.confirmed_at = time.now @user.uid = @profile["id"] @user.provider = 'facebook' end @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 response header response.headers.merge!(auth_header) @user.save! if sign_in(:user, @user, store: false, bypass: false) render json: { data: @user.token_validation_response } end end
point post route , pass authresponse.accesstoken
facebook request params[:token]
. voilà, authentication magic!
Comments
Post a Comment