Rails 路由代码示例:使用 authenticateauthenticated 方法进行用户认证

Rails的路由代码如下:

authenticate :user do
  ...
  ...
  ...

  authenticated :user, ->(u) { u.email_verified? } do
    ...
    ...
    ...
    
    post 'products/:product/favorite', to: 'products#favor', as: 'product_favor'
    delete 'products/:product/favorite', to: 'products#disfavor', as: 'product_disfavor'
  end
end

在Rails中,使用authenticate方法指定需要认证的用户。authenticated方法指定需要认证且通过额外条件的用户。

注意,这里使用了命名路由来为收藏和取消收藏的路由命名,以方便在其他地方引用。

Rails 路由代码示例:使用 `authenticate` 和 `authenticated` 方法进行用户认证

原文地址: https://www.cveoy.top/t/topic/pXHY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录