I had implemented devise gem and omniauth-facebook before few months into my app and everything was worked as usual, until now when I deleted my fb user from db.
Now I am trying to sign in again as nonexisting user and all the time I am redirected to Devise Sign Up form.
So I cant log in/ register my FB user into my app.
What can cause this type of problem?
routes.rb
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
omniouth_callback_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
#user = User.from_omniauth(request.env["omniauth.auth"])
if #user.persisted?
sign_in_and_redirect #user, :event => :authentication #this will throw if #user is not activated
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def failure
redirect_to root_path
end
end
user.rb model
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:facebook]
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
user.email = data["email"] if user.email.blank?
end
end
end
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.name = auth.info.name # assuming the user model has a name
user.image = auth.info.image # assuming the user model has an image
end
end
rails routes
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_facebook_omniauth_authorize GET|POST /users/auth/facebook(.:format) users/omniauth_callbacks#passthru
user_facebook_omniauth_callback GET|POST /users/auth/facebook/callback(.:format) users/omniauth_callbacks#facebook
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
user_registration PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
POST /users(.:format) devise/registrations#create
I found the reason in user.rb model I had one association belongs_to:somemodel and that I was put there for testing some model relations.
So commenting this line I am logged in by facebook!
Related
I am using devise_invitable gem in my rails 5 application. Every time after submitting the "Send an Invitation" button, I get the error RuntimeError in Devise::InvitationsController#create, Could not find a valid mapping for nil
Here is the code of application_controller.rb file:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
def authenticate_inviter!
unless current_user.role=='Manager'
if current_user.role == "Developer"
redirect_to root_path, :alert => "Access Denied! Only Manager Can Add New Developer"
end
super
end
end
protected
def configure_permitted_parameters
added_attrs = [ :email, :password, :password_confirmation ]
devise_parameter_sanitizer.permit :sign_up, keys: added_attrs
devise_parameter_sanitizer.permit :account_update, keys: added_attrs
devise_parameter_sanitizer.permit :accept_invitation, keys: [:email]
end
end
Here is the Code of User.rb:
class User < ApplicationRecord
include DeviseInvitable::Inviter
devise :invitable, :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
end
Here is Route.rb
Rails.application.routes.draw do
devise_for :users
root 'dashboard#index'
end
Here is all routes:
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) devise_invitable/registrations#cancel
new_user_registration GET /users/sign_up(.:format) devise_invitable/registrations#new
edit_user_registration GET /users/edit(.:format) devise_invitable/registrations#edit
user_registration PATCH /users(.:format) devise_invitable/registrations#update
PUT /users(.:format) devise_invitable/registrations#update
DELETE /users(.:format) devise_invitable/registrations#destroy
POST /users(.:format) devise_invitable/registrations#create
accept_user_invitation GET /users/invitation/accept(.:format) devise/invitations#edit
remove_user_invitation GET /users/invitation/remove(.:format) devise/invitations#destroy
new_user_invitation GET /users/invitation/new(.:format) devise/invitations#new
user_invitation PATCH /users/invitation(.:format) devise/invitations#update
PUT /users/invitation(.:format) devise/invitations#update
POST /users/invitation(.:format) devise/invitations#create
root GET / dashboard#index
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
I haven't created any custom invitations controller.
I just run the following to setup and configure devise_invitable
gem 'devise_invitable'
bundle install
rails generate devise_invitable:install
rails generate devise_invitable User
rails db:migrate
rails generate devise_invitable:views
restart the rails server
Here is the picture of the complete error:
see the picture of the complete error
How can I get rid of this problem. Please help me
Finally I have solved this issue by the help of Scambra
I have just changed the logic of my Application Controller
From:
def authenticate_inviter!
unless current_user.role=='Manager'
if current_user.role == "Developer"
redirect_to root_path, :alert => "Access Denied! Only Manager Can Add New Developer"
end
super
end
end
To:
def authenticate_inviter!
if current_user.role == "Developer"
redirect_to root_path, :alert => "Access Denied! Only Manager Can Add New Developer"
end
super
end
I am working on devise omniauth to authenticate user using facebook. I have implemented it using the documentation https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
It is not working fine. When I login using my facebook account then it takes me to the "failure" method in the callback controller.
But the interesting thing is that when I open facebook it open without any problem means it is authenticating my account.
I have checked it so many times by login and logout from facebook. But the problem is that If it is authenticating facebook account then why it takes me to "failure method".
here is my code
callback controller.
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
#user = User.from_omniauth(request.env["omniauth.auth"])
if #user.persisted?
sign_in_and_redirect #user, :event => :authentication #this will throw if #user is not activated
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def failure
render plain: params.inspect
# redirect_to root_path
end
end
User model
class User < ApplicationRecord
# Include default devise modules. Others available are:
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable
devise :omniauthable, :omniauth_providers => [:facebook]
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.name = auth.info.name # assuming the user model has a name
user.image = auth.info.image # assuming the user model has an image
# If you are using confirmable and the provider(s) you use validate emails,
# uncomment the line below to skip the confirmation emails.
# user.skip_confirmation!
end
end
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
user.email = data["email"] if user.email.blank?
end
end
end
end
route code
devise_for :users, controllers: {confirmations: 'confirmations',registrations: 'users/registrations',omniauth_callbacks: 'users/omniauth_callbacks' } do
#put "confirm_user", to: "confirmations#confirm_user"
get "confirmation", to: "confirmations#after_confirmation_path_for"
end
Facebook app setting screenshot is
https://www.dropbox.com/s/vn8cjpr5wyagkdp/Screenshot%202017-11-27%2017.04.28.png?dl=0
development logs after sign in are
Started GET "/users/auth/facebook" for 10.0.2.2 at 2017-11-28 14:13:37 +0000
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Started GET "/users/auth/facebook/callback?code=AQA8oLlPsMLSvZf5NqZfOA0oJxDVKczwqqMIxWPD2dJoucpJl9T7MLTmf0mMDlOhgQPAhKa2f6My4vWGZmTWhVo6S5TbSQ3ELup1iBhDHTb869pMMo-ksa1Kh1gJDG65ZJxUj5vAe8jH-DX0eBRpf_ygZACCoFYFTiZdAIzCXQI7jfLaeqH70CqAffkGZczYzhjThM_NLol3Lzo18ZX_6_5n2-p7nMC3IKhmzDEyo_toyaI1telD3QMwa0re7GIu-UXKV4DQp-ClLT452Bigp9Fhs50wYm-Kl08E7195R2mpBESpB7Gu0moDbCgi61dEEk5u8GGfmm0Cxbu9Fcw1_Eu8" for 10.0.2.2 at 2017-11-28 14:13:40 +0000
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by Users::OmniauthCallbacksController#failure as HTML
Parameters: {"code"=>"AQA8oLlPsMLSvZf5NqZfOA0oJxDVKczwqqMIxWPD2dJoucpJl9T7MLTmf0mMDlOhgQPAhKa2f6My4vWGZmTWhVo6S5TbSQ3ELup1iBhDHTb869pMMo-ksa1Kh1gJDG65ZJxUj5vAe8jH-DX0eBRpf_ygZACCoFYFTiZdAIzCXQI7jfLaeqH70CqAffkGZczYzhjThM_NLol3Lzo18ZX_6_5n2-p7nMC3IKhmzDEyo_toyaI1telD3QMwa0re7GIu-UXKV4DQp-ClLT452Bigp9Fhs50wYm-Kl08E7195R2mpBESpB7Gu0moDbCgi61dEEk5u8GGfmm0Cxbu9Fcw1_Eu8"}
Rendering text template
Rendered text template (0.0ms)
Completed 200 OK in 20ms (Views: 10.6ms | ActiveRecord: 0.0ms)
The result of params.inspect is as follows
<ActionController::Parameters {"code"=>"AQA8oLlPsMLSvZf5NqZfOA0oJxDVKczwqqMIxWPD2dJoucpJl9T7MLTmf0mMDlOhgQPAhKa2f6My4vWGZmTWhVo6S5TbSQ3ELup1iBhDHTb869pMMo-ksa1Kh1gJDG65ZJxUj5vAe8jH-DX0eBRpf_ygZACCoFYFTiZdAIzCXQI7jfLaeqH70CqAffkGZczYzhjThM_NLol3Lzo18ZX_6_5n2-p7nMC3IKhmzDEyo_toyaI1telD3QMwa0re7GIu-UXKV4DQp-ClLT452Bigp9Fhs50wYm-Kl08E7195R2mpBESpB7Gu0moDbCgi61dEEk5u8GGfmm0Cxbu9Fcw1_Eu8"} permitted: false>
The output of rake routes is
rake routes
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_facebook_omniauth_authorize GET|POST /users/auth/facebook(.:format) users/omniauth_callbacks#passthru
user_facebook_omniauth_callback GET|POST /users/auth/facebook/callback(.:format) users/omniauth_callbacks#facebook
user_twitter_omniauth_authorize GET|POST /users/auth/twitter(.:format) users/omniauth_callbacks#passthru
user_twitter_omniauth_callback GET|POST /users/auth/twitter/callback(.:format) users/omniauth_callbacks#twitter
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel
new_user_registration GET /users/sign_up(.:format) users/registrations#new
edit_user_registration GET /users/edit(.:format) users/registrations#edit
user_registration PATCH /users(.:format) users/registrations#update
PUT /users(.:format) users/registrations#update
DELETE /users(.:format) users/registrations#destroy
POST /users(.:format) users/registrations#create
new_user_confirmation GET /users/confirmation/new(.:format) confirmations#new
user_confirmation GET /users/confirmation(.:format) confirmations#show
POST /users/confirmation(.:format) confirmations#create
new_model_session GET /models/sign_in(.:format) devise/sessions#new
model_session POST /models/sign_in(.:format) devise/sessions#create
destroy_model_session DELETE /models/sign_out(.:format) devise/sessions#destroy
new_model_password GET /models/password/new(.:format) devise/passwords#new
edit_model_password GET /models/password/edit(.:format) devise/passwords#edit
model_password PATCH /models/password(.:format) devise/passwords#update
PUT /models/password(.:format) devise/passwords#update
POST /models/password(.:format) devise/passwords#create
cancel_model_registration GET /models/cancel(.:format) devise/registrations#cancel
new_model_registration GET /models/sign_up(.:format) devise/registrations#new
edit_model_registration GET /models/edit(.:format) devise/registrations#edit
model_registration PATCH /models(.:format) devise/registrations#update
PUT /models(.:format) devise/registrations#update
DELETE /models(.:format) devise/registrations#destroy
POST /models(.:format) devise/registrations#create
new_model_confirmation GET /models/confirmation/new(.:format) devise/confirmations#new
model_confirmation GET /models/confirmation(.:format) devise/confirmations#show
POST /models/confirmation(.:format) devise/confirmations#create
donations_donor_history GET /donations/donor_history(.:format) donations#donor_history
donations_donor_signup GET /donations/donor_signup(.:format) donations#donor_signup
POST /donations/donor_signup(.:format) donations#donor_signup
donations_sms_service POST /donations/sms_service(.:format) donations#sms_service
donations_create_user_account POST /donations/create_user_account(.:format) donations#create_user_account
donations_add_user_payroll POST /donations/add_user_payroll(.:format) donations#add_user_payroll
donations GET /donations(.:format) donations#index
edit_donation GET /donations/:id/edit(.:format) donations#edit
donation GET /donations/:id(.:format) donations#show
PATCH /donations/:id(.:format) donations#update
PUT /donations/:id(.:format) donations#update
DELETE /donations/:id(.:format) donations#destroy
campaign_donations POST /campaigns/:campaign_id/donations(.:format) donations#create
new_campaign_donation GET /campaigns/:campaign_id/donations/new(.:format) donations#new
campaign_donations_create_user_account GET /campaigns/:campaign_id/donations/create_user_account(.:format) donations#create_user_account
campaigns GET /campaigns(.:format) campaigns#index
POST /campaigns(.:format) campaigns#create
new_campaign GET /campaigns/new(.:format) campaigns#new
edit_campaign GET /campaigns/:id/edit(.:format) campaigns#edit
campaign GET /campaigns/:id(.:format) campaigns#show
PATCH /campaigns/:id(.:format) campaigns#update
PUT /campaigns/:id(.:format) campaigns#update
DELETE /campaigns/:id(.:format) campaigns#destroy
organizations GET /organizations(.:format) organizations#index
POST /organizations(.:format) organizations#create
new_organization GET /organizations/new(.:format) organizations#new
edit_organization GET /organizations/:id/edit(.:format) organizations#edit
organization GET /organizations/:id(.:format) organizations#show
PATCH /organizations/:id(.:format) organizations#update
PUT /organizations/:id(.:format) organizations#update
DELETE /organizations/:id(.:format) organizations#destroy
admins_social_sharing_switch POST /admins/social_sharing_switch(.:format) admins#social_sharing_switch
admins_error_detail GET /admins/error_detail(.:format) admins#error_detail
GET /admins/generate_report/:id(.:format) admins#generate_report
admins_create_company GET /admins/create_company(.:format) admins#create_company
POST /admins/create_company(.:format) admins#create_company
admins_revenue_detail GET /admins/revenue_detail(.:format) admins#revenue_detail
admins_create_account GET /admins/create_account(.:format) admins#create_account
admins_view_account GET /admins/view_account(.:format) admins#view_account
GET /admins/view_company/:id(.:format) admins#view_company
admins_donation_analysis GET /admins/donation_analysis(.:format) admins#donation_analysis
admins_link_expiry GET /admins/link_expiry(.:format) admins#link_expiry
admins_edit_profile GET /admins/edit_profile(.:format) admins#edit_profile
admins_update_profile POST /admins/update_profile(.:format) admins#update_profile
POST /admins/create_account(.:format) admins#create_account
admin_destroy GET /admins/:id(.:format) admins#destroy
admins GET /admins(.:format) admins#index
POST /admins(.:format) admins#create
new_admin GET /admins/new(.:format) admins#new
edit_admin GET /admins/:id/edit(.:format) admins#edit
admin GET /admins/:id(.:format) admins#show
PATCH /admins/:id(.:format) admins#update
PUT /admins/:id(.:format) admins#update
DELETE /admins/:id(.:format) admins#destroy
crons_expirylink_alert GET /crons/expirylink_alert(.:format) crons#expirylink_alert
users_sign_out GET /users/sign_out(.:format) devise/sessions#destroy
root GET / campaigns#latest
After hardwork of 2 days I have finally solved it myself. Every thing was fine except the gem itself. If you just write the gem omniauth-facebook it would install version 1.4.0 which is very old. I update it to gem 'omniauth-facebook', '~> 4.0' and it is working like a charm. So if anyone face the same issue he should update the gem.
I'm now using Devise gem in a Ruby application.
It seems a password method doesn't work because in somehow, devise controller is a bit modified. When I typed /users/password/new, it goes back to a root.
I followed a tutorial but now I'm stuck at this really important method that I have to provide to the people.
The passwords_controller.rb file is located in controllers/users/password_controller.rb.
And the code is
class Users::PasswordsController < Devise::PasswordsController
prepend_before_filter :require_no_authentication
# Render the #edit only if coming from a reset password email link
append_before_filter :assert_reset_token_passed, only: :edit
# GET /resource/password/new
def new
self.resource = resource_class.new
end
# POST /resource/password
def create
self.resource = resource_class.send_reset_password_instructions(resource_params)
yield resource if block_given?
if successfully_sent?(resource)
respond_with({}, location: after_sending_reset_password_instructions_path_for(resource_name))
else
respond_with(resource)
end
end
( which is a typical devise controller content )
My user.rb model goes as follow.
...
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:google_oauth2] #, :confirmable
...
I have view files in this directory.
views/users/passwords/create.html.erb, edit.html.erb, new.html.erb
views/users/sessions/new.html.erb
views/users/registrations/edit.html.erb, create.html.erb
Routes file goes like this
devise_for :users, controllers: { passwords: "users/passwords", sessions: "users/sessions", registrations: "users/registrations", omniauth_callbacks: "users/omniauth_callbacks" }
Whenever I type "http://localhost:3000/users/password/new", it goes back to root.
This is my route file.
new_user_session GET /users/sign_in(.:format) users/sessions#new
user_session POST /users/sign_in(.:format) users/sessions#create
destroy_user_session GET /users/sign_out(.:format) users/sessions#destroy
user_omniauth_authorize GET|POST /users/auth/:provider(.:format) users/omniauth_callbacks#passthru {:provider=>/google_oauth2/}
user_omniauth_callback GET|POST /users/auth/:action/callback(.:format) users/omniauth_callbacks#:action
user_password POST /users/password(.:format) users/passwords#create
new_user_password GET /users/password/new(.:format) users/passwords#new
edit_user_password GET /users/password/edit(.:format) users/passwords#edit
PATCH /users/password(.:format) users/passwords#update
PUT /users/password(.:format) users/passwords#update
cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel
user_registration POST /users(.:format) users/registrations#create
new_user_registration GET /users/sign_up(.:format) users/registrations#new
edit_user_registration GET /users/edit(.:format) users/registrations#edit
PATCH /users(.:format) users/registrations#update
PUT /users(.:format) users/registrations#update
DELETE /users(.:format) users/registrations#destroy
POST|GET /:controller(/:action(/:id))(.:format) :controller#:action
root GET / landings#index
The log I got when I go to localhost:3000/users/password/new goes like this.
Started GET "/users/password/new" for ::1 at 2015-10-06 04:11:58 +0900 Processing by Users::PasswordsController#new as HTML Redirected to localhost:3000 Filter chain halted as :is_login rendered or redirected Completed 302 Found in 2ms (ActiveRecord: 0.0ms) This is a log that I got right after I posted the address
If you guys have any hint, please let me know!!
Best
I fix it by append /users/password/edit?reset_password_token=aaaaaa
maybe it is the devise bug .
Environment: RAILS 3.2 + DEVISE for auth + Invitable + Confirmable add-ons.
Using devise (2.2.3)
Using devise-i18n (0.6.5)
Using devise_invitable (1.0.3)
I am trying to redirect to a specific location after ACCEPT (TO SIGN UP), but only after_sign_in_path_for seems to be called after SIGN IN and ACCEPT.
I haven't been able to have after_accept_path_for working.
It continues to redirect to the "after sign in" location.
HERE THE CODE
In my routes.rb:
devise_for :users,
:controllers => { :registrations => 'registrations', :invitations => 'invitations' }
rake routes give me this:
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
accept_user_invitation GET /users/invitation/accept(.:format) devise/invitations#edit
user_invitation POST /users/invitation(.:format) devise/invitations#create
new_user_invitation GET /users/invitation/new(.:format) devise/invitations#new
PUT /users/invitation(.:format) devise/invitations#update
In my registration controller:
class RegistrationsController < Devise::RegistrationsController
# clear session
def create
super
session[:omniauth] = nil unless #user.new_record?
end
#protected
# after_sign_up_path_for doesn't seem to be called when using Confirmable module
# def after_inactive_sign_up_path_for(resource)
# #me_path
# session[:user_return_to].nil? ? me_path : session[:user_return_to]
# end
private
def build_resource(*args)
super
if session[:omniauth]
#user.apply_omniauth(session[:omniauth])
#user.valid?
end
end
end
Also
class Users::InvitationsController < Devise::InvitationsController
protected
def after_accept_path_for
session[:user_return_to].nil? ? me_path : session[:user_return_to]
end
end
In my application controller, (I left intentionally some commented code I tried to make it work):
def store_location
session[:user_return_to] = request.fullpath
end
# def after_sign_up_path_for
# me_path
# end
#
# def after_inactive_sign_up_path_for(resource)
# me_path
# #session[:user_return_to].nil? ? me_path : session[:user_return_to]
# end
# https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-up-(registration)
def after_sign_in_path_for(resource)
me_path
#dashboard_path
#session[:user_return_to].nil? ? dashboard_path : session[:user_return_to]
end
Any suggestions?
ADDED DEBUGGIN REDIRECTS
Add this to my application_controller
def redirect_to_with_logging(*args)
logger.debug "Redirect: #{args.inspect} from #{caller[0]}"
redirect_to_without_logging *args
end
alias_method_chain :redirect_to, :logging
After Sign in, works like a charm
Started POST "/users/sign_in" for 127.0.0.1 at 2013-04-25 14:20:04 +0200
Processing by Devise::SessionsController#create as HTML
[... I removed some of the Session creation info ...]
Redirect: ["/dashboard"] from /Users/joel/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.11/lib/action_controller/metal/responder.rb:135:in `redirect_to'
Redirected to http://localhost:3000/dashboard
Completed 302 Found in 968ms (ActiveRecord: 0.0ms)
DOCS:
After sign in
After sign up
After Accept <====
Override after_accept_path_for in Invitations controllers
class Users::InvitationsController < Devise::InvitationsController
protected
def after_accept_path_for(resource)
me_path
end
end
IMPORTANT:
put this file in 'controllers/users' directory
Fix the routes.rb to use the Users::InvitationsController
devise_for :users, :controllers => { :registrations => 'registrations', :invitations => 'users/invitations' }
You may put redirect_to my_specific_url at the end of sign_up controller's method, to redirect after signing up
I'm following along with a Railscast http://railscasts.com/episodes/235-devise-and-omniauth-revised?view=comments about implementing omniauth into devise, specifically adding Twitter signin. When I add the link to sign into Twitter to my app, I'm getting this error visiting the homepage
<li><%= link_to "Sign In With Twitter", user_omniauth_authorize_path(:provider) %></li>
No route matches {:controller=>"omniauth_callbacks", :action=>"passthru", :provider=>:provider}
If I do rake routes (see below), it shows that I have 'user_omniauth_callback' route, but it doesn't specify a request type (GET, POST) etc, which I'm not sure is significant or not.
I searched this error on SO, and one person had a similar problem that resulted from not including the following code in routes.rb, but I have it included.
devise_for :users, path_names: {sign_in: "login", sign_out: "logout"},
controllers: {omniauth_callbacks: "omniauth_callbacks"}
The error arose before getting to the part of the episode where he adds the omniauth controller; I added it just to be sure but I'm still getting the same error:
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def all
request.env["omniauth.auth"]
end
alias_method :twitter, :all
end
I'm not really sure what else I can look at. In the devise initializer, I set up config for Twitter
config.omniauth :twitter, ENV["TWITTER_KEY"], ENV["TWITTER_SECRET"]
This is my user model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :omniauthable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :image, :provider, :uid
validates_uniqueness_of :name, :email, :case_sensitive => false
end
Can you make any suggestions about what I might do to fix this problem?
Rake Routes
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_omniauth_authorize /users/auth/:provider(.:format) devise/omniauth_callbacks#passthru {:provider=>/twitter/}
user_omniauth_callback /users/auth/:action/callback(.:format) devise/omniauth_callbacks#(?-mix:twitter)
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
The mistake was that I was passing :provider rather than :twitter as an argument in the link.
Incorrect
<%= link_to "Sign In With Twitter", user_omniauth_authorize_path(:provider) %>
Correct:
<%= link_to "Sign In With Twitter", user_omniauth_authorize_path(:twitter) %>