本文介绍了未确认用户的Rails Devise抛出302错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在ROR网站中整合了教练登​​录的设计。它工作正常,但是当我尝试登录通过未确认的用户,它抛出302错误。以下是我使用的代码的详细信息:

  1)数据库模式:

create_table教练,:force => true do | t |
t.stringfirst_name,:limit => 20
t.stringlast_name,:limit => 20
t.integerstatus,:limit => 2,:default =>
t.datetimecreated_at
t.datetimeupdated_at
t.stringemail,:default => ,:null => false
t.stringencrypted_pa​​ssword,:default => ,:null => false
t.stringreset_password_token
t.datetimereset_password_sent_at
t.datetimeremember_created_at
t.integersign_in_count,,default => 0,:null => false
t.datetimecurrent_sign_in_at
t.datetimelast_sign_in_at
t.stringcurrent_sign_in_ip
t.stringlast_sign_in_ip
t.stringconfirmation_token
t.datetimeconfirm_at
t.datetimeconfirm_sent_at
t.stringunconfirmed_email
end

2)会话控制器文件:

  class Coaches :: SessionsController < Devise :: SessionsController 
layout:apply_devise_layout

def create
coach = Coach.where(:email => params [:coach] [:email])[0]
如果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_or_scope
sign_in(scope,resource)除非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
如果coach.present? &安培;&安培; !coach.confirmed?
return render:json => {:success => false,:errors => t('devise.failure.unconfirmed')}
end
如果coach.present?
case coach.status
当Coach :: COACH_ACCOUNT_STATUS [:TRIAL_EXPIRED]
return render:json => {:success => false,:errors => 您的试用期已经过期,请< a href ='/ contact'>点击此处< / a>与支持小组联系。}
当Coach :: COACH_ACCOUNT_STATUS [:PREMIUM_EXPIRED]
返回render:json => {:success => false,:errors => 您的子句已经过期,请< a href ='/ contact'>点击此处< / a>。与支持小组联系。}
当Coach :: COACH_ACCOUNT_STATUS [:DELETED]
返回render:json => {:success => false,:errors => 此帐户已被删除,请< a href ='/ contact'>点击此处< / a>与支持小组联系。}
当Coach :: COACH_ACCOUNT_STATUS [:BLOCKED]
返回render:json => {:success => false,:errors => 此帐户已被暂停,请< a href ='/ contact'>点击此处< / a>与支持小组联系。
end
end
resource = warden。验证!(: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 =设计: :mapping.find_scope!(resource_or_scope)
resource || = resource_or_scope
sign_in(scope,resource)除非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


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
解决方案
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

这篇关于未确认用户的Rails Devise抛出302错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 20:32