我已经整合了ROR网站中教练登录的设计。它工作正常,但当我尝试通过尚未确认的用户登录时,它会抛出302错误。以下是我使用过的代码的详细信息:
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)会话控制器文件:
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
答案 0 :(得分: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