Sunday 15 August 2010

Extra Attributes while login with facebook using OAuth and Devise Rails 4 app -



Extra Attributes while login with facebook using OAuth and Devise Rails 4 app -

i've been searching way next , haven't succeded yet.

i have implemented facebook login app, looking way inquire user attributes not provided facebook (like home address example).

i tried redirecting user edit_user_registration page 1 time logged in first time, can't add together new attributes because user won't know password due fact provided facebook , encripted.

thanks in advance answers!

first override devise::omniauthcallbackscontroller controller. add together method facebook connection in have 2 things. first if user on system, method should log user in directly.

second if user new, should build user object, extract parameters need facebook , update user object these parameters. after should render page user can input own password , add together info need.

omniauth_callbacks_controller.rb

class omniauthcallbackscontroller < devise::omniauthcallbackscontroller def facebook user = user.find_by_fb_token(token) if user.present? sign_in user, event: :authentication else @user = user.new(**facebook parameters**) render 'devise/registrations/after_social_connection' end end end

in after_social_connection view, add together form @user have attributes facebook prepopulated. not forget add together :password , :password_confirmation fields user able have password on own application. in view can add together whatever attributes user input.

since using rails 4, need override devise parameters sanitizer (strong parameters) able input fields in form. can done in application controller (the lazy way)

application_controller.rb

class applicationcontroller < actioncontroller::base before_action :configure_permitted_parameters, if: :devise_controller? def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:password, :password_confirmation, :email. :name, :biography, :profile_picture) } end end

for farther reading https://github.com/plataformatec/devise/wiki/omniauth:-overview

ruby-on-rails-4 devise omniauth

No comments:

Post a Comment