gpt4 book ai didi

ruby-on-rails - Rails Devise 为未确认的用户抛出 302 错误

转载 作者:行者123 更新时间:2023-12-03 08:33:25 27 4
gpt4 key购买 nike

我已经在 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) session Controller 文件:

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

最佳答案

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

关于ruby-on-rails - Rails Devise 为未确认的用户抛出 302 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23577738/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com