在控制器中使用来自简单身份验证令牌的令牌

Using token from Simple authentication Token in controller

我在 Rails 中为我的 API 使用简单身份验证令牌。

如文档中所述:https://github.com/gonzalo-bulnes/simple_token_authentication

在我的用户模型中,我添加了以下行: acts_as_token_authenticatable

每当我登录或注销时,我都能够按预期获取和更改认证令牌。

我不明白的是,下面这行有什么用。

acts_as_token_authentication_handler_for 用户

我有一个名为 ProfilesController 的控制器,我在其中添加了这一行。我是否在控制器中添加这一行没有区别。我能够以与我使用的方式类似的方式调用方法,而无需添加它。

你能解释一下这是做什么的吗?

那是因为 gem 说这种不正确或没有凭据的行为是 configurable

What happens when a request is provided with no credentials or incorrect credentials is highly configurable

要拒绝访问,您必须设置 fallback

如果您有设计,请在 app/controllers/application_controller.rb

中将其设置为 fallback: :devisefallback: :exception