如何在 Sinatra 中设置身份验证?

How to set authentication in Sinatra?

我需要对博客进行简单的身份验证。对于一个人。只需登录网站

无法配置 sinatra_warden。写下行

require 'rubygems'
require 'sinatra'
require 'pry-byebug'
require "sinatra/activerecord"
require "carrierwave"
require "carrierwave/orm/activerecord"
require 'sinatra_warden'
require 'warden'

register Sinatra::Warden

use Rack::Session::Pool

app.rb中,但我得到一个错误

NoMethodError: undefined method `register' for main:Object

gem sinatra_warden 已安装。写得很好 require "warden" & require "sinatra_warden"

sinatra_warden 0.3.2

典狱长 1.2.6

当我在控制器中添加 authorize! 方法时,出现错误

undefined method `authorize!'

因为您没有使用 sinatra/base,所以您应该添加 sinatra/namespace。添加到您的 app.rb 这需要 require "sinatra/namespace".

Sinatra::Namespace is an extension that adds namespaces to an application. This namespaces will allow you to share a path prefix for the routes within the namespace, and define filters, conditions and error handlers exclusively for them. Besides that, you can also register helpers and extensions that will be used only within the namespace.

或者将您的应用程序更改为模块化样式:

require "sinatra/base"

class MyApp < Sinatra::Base
  register Sinatra::Warden

  # The rest of your modular application code goes here...
end