问题
I have integrated the devise for coaches login in ROR site. It is working fine but when I tried to login through a user who is not confirmed yet it thrown 302 error. Here are the details of the code I have used:
1) Database Schema:
create_table "coaches", :force => true do |t|
t.string "first_name", :limit => 20
t.string "last_name", :limit => 20
t.integer "status", :limit => 2, :default => 0
t.datetime "created_at"
t.datetime "updated_at"
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0, :null => false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
end
2) Session Controller file:
class Coaches::SessionsController < Devise::SessionsController
layout :apply_devise_layout
def create
coach = Coach.where(:email => params[:coach][:email])[0]
if coach.present? && !coach.confirmed?
return render :json => {:success => false, :errors => t('devise.failure.unconfirmed')}
end
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure")
sign_in_and_redirect(resource_name, resource)
end
def sign_in_and_redirect(resource_or_scope, resource=nil)
scope = Devise::Mapping.find_scope!(resource_or_scope)
resource ||= resource_or_scope
sign_in(scope, resource) unless warden.user(scope) == resource
sign_out :admin
sign_out :member
return render :json => {:success => true,:msg=>t('devise.sessions.signed_in'),:sent_to=>resource.site_url || ''}
end
def failure
return render :json => {:success => false, :errors => flash[:alert]}
end
end
回答1:
class Coaches::SessionsController < Devise::SessionsController
def create
coach = Coach.where(:email => params[:coach][:email]).first
if coach.present? && !coach.confirmed?
return render :json => {:success => false, :errors => t('devise.failure.unconfirmed')}
end
if coach.present?
case coach.status
when Coach::COACH_ACCOUNT_STATUS[:TRIAL_EXPIRED]
return render :json => {:success => false, :errors => "Your trial has been expired, please <a href='/contact'>click here</a> to contact support team."}
when Coach::COACH_ACCOUNT_STATUS[:PREMIUM_EXPIRED]
return render :json => {:success => false, :errors => "Your subcription has been expired, please <a href='/contact'>click here</a> to contact support team."}
when Coach::COACH_ACCOUNT_STATUS[:DELETED]
return render :json => {:success => false, :errors => "This account has been deleted, please <a href='/contact'>click here</a> to contact support team."}
when Coach::COACH_ACCOUNT_STATUS[:BLOCKED]
return render :json => {:success => false, :errors => "This account has been suspended, please <a href='/contact'>click here</a> to contact support team."}
end
end
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure")
sign_in_and_redirect(resource_name, resource)
end
def sign_in_and_redirect(resource_or_scope, resource=nil)
session["warden.user.member.key"]=nil
session["warden.user.admin.key"]=nil
scope = Devise::Mapping.find_scope!(resource_or_scope)
resource ||= resource_or_scope
sign_in(scope, resource) unless warden.user(scope) == resource
flash[:notice]=t('devise.sessions.signed_in');
return render :json => {:success => true,:msg=>t('devise.sessions.signed_in'),:sent_to=>resource.site_url || ''}
end
def failure
return render :json => {:success => false, :errors => flash[:alert]}
end
end
来源:https://stackoverflow.com/questions/23577738/rails-devise-thrown-302-error-for-non-confirmed-user