簡體   English   中英

Rails 5 中經過身份驗證和未經身份驗證的路由

[英]authenticated and unauthenticated routes in rails 5

我已將 rails 4 應用程序更新為 Rails 5,但無法在 rails 5 上設置經過身份驗證和未經身份驗證的路由。

在軌道 4 中:

unauthenticated do
   root to: "home#index", as: :unauthenticated_root
end

authenticated :user do
  root :to => "home#dashboard"
end

但是如何設置 Rails 5 應用程序

我認為您找到了解決方案,即如果user_signed_in然后重定向到home#dashboard else "home#index" ,對嗎? 那么

首先,請參閱此解決方案,其中說明如何:如果不起作用,請為已登錄用戶定義不同的根路由,然后轉到下面。


路由文件

root to: "home#index", as: :unauthenticated_root
get 'dashboard', to: "home#dashboard" #=> dashboard_path

在您的索引操作中使用如下所示

def index
    redirect_to dashboard_path if user_signed_in?
end

使用控制器的這個頂部

before_action :authenticate_user!, except: [:index]

它將為dashboard和除index以外的其他操作設置需要身份驗證,您也可以這樣使用

before_action :authenticate_user!, only: [:dashboard]

它將設置為僅dashboard操作需要身份驗證。

user_signed_in? authenticate_user! 是回調方法,如果您使用設計,那么它默認存在,如果您不使用設計,那么我相信您有自己的回調方法,只需替換它即可。

評論后完全格式化

class HomeController < ApplicationController
    before_action :authenticate_user!, except: [:index]

    def index 
        redirect_to dashboard_path if user_signed_in?
    end

    def dashboard 
    end

    def other_action 
    end

    .....
end

后耙路線

unauthenticated_root GET    /                       home#index
dashboard GET               /dashboard(.:format)    home#dashboard

評論后更新

如果您有 ajax 登錄,請按照此SO 問題進行操作

您應該覆蓋 devise create 方法並通過正確呈現您的視圖來管理那里的情況,否則,正如您在 devise 的默認創建方法中看到的那樣,如果不執行會話創建,它只會重定向您而不呈現創建。 js

您可以覆蓋控制器文件夾中的設計控制器並命名為registrations_controller.rb ,如下所示

class RegistrationsController < Devise::RegistrationsController
    # POST /resource
    def create
        super
    end
end

路線看起來像

devise_for :users, :controllers => {:registrations => 'registrations'}

希望它有幫助

這是使用路由約束的一個很好的應用程序。 請考慮以下事項:

  # routes.rb

  scope module: 'authenticated', constraints: AuthConstraint.new { |user| user.present? } do
    # Management dashboard
    root 'dashboards#index'
  end

  root 'home#index'

約束在app/constraints/auth_constraint.rb文件中定義,可以根據需要進行配置。 例如。

# app/constraints/auth_constraint.rb

class AuthConstraint
  def initialize(&block)
    @block = block || ->(_) { true }
  end

  def matches?(req)
    user = current_user(req)
    user.present? && @block.call(user)
  end

  def current_user(req)
    User.find_by_id(session[:user_id])
  end
end

這是一種基於任何所需變量(角色、身份驗證等)定義路由訪問的靈活方法

這是 Rails 文檔中約束的鏈接: https : //guides.rubyonrails.org/routing.html#specifying-constraints

保護拆分儀表板的示例(A/B 測試的寶石):

# config/routes.rb
require "split/dashboard"

class AuthConstraint
  def initialize(&block)
    @block = block
  end

  def matches?(request)
    @block.call(current_user(request))
  end

  def current_user(request)
    return if request.session[:user_id].blank?
    User.find_by(id: request.session[:user_id])
  end
end

Rails.application.routes.draw do
  scope constraints: AuthConstraint.new { |user| user&.dorian? } do
    mount Split::Dashboard, at: "split"
  end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM