我一直在努力解决这个问题,似乎无法弄清楚发生了什么.我正在使用devise和devise_token_auth.我有一个Ionic 2应用程序,我试图通过Facebook登录.现在,电子邮件/密码验证工作正常.这是我的一些代码.
# initializers/devise_token_auth.rb
config.enable_standard_devise_support = true
# initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, 'xxxxx', 'xxxxx', scope: 'email', info_fields: 'email,first_name,last_name'
end
# app/models/user.rb
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, :omniauth_providers => [:facebook, :google]
include DeviseTokenAuth::Concerns::User
devise :omniauthable
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :null_session, if: -> { request.format.json? }
include DeviseTokenAuth::Concerns::SetUserByToken
# Allow other fields to be passed up on registration.
before_action :configure_permitted_parameters, if: :devise_controller?
def after_sign_in_path_for(resource)
users_path
end
def authenticate_admin
unless current_user && current_user.role == …Run Code Online (Sandbox Code Playgroud) 我继承了一个Ionic应用程序,该应用程序使用ng-token-auth+ devise_token_auth来处理身份验证以及正面和背面之间的会话.
发生的事情很奇怪.有时(特别是连接速度慢)请求(或响应)丢失,之后我只得到401http错误.
我知道,每当我发送请求时,令牌都会过期,但是当xhr请求被取消时(由我认为是服务器,或者是浏览器,我不知道),令牌已过期而未被新生成的令牌替换通过devise_token_auth宝石.
我知道Rails,但我不熟悉Angular,既不是Ionic,也不知道到底在哪里.
在阅读了很多SO答案之后,似乎没有人出现我的问题(在本地和分期/生产中发生),我检查了以下内容
storage被设为localStorage.config.batch_request_buffer_throttle = 20.seconds 令我感到困惑的是,它有时只会发生,而不是总是发生.(并且后端没有错误)
我在devise_token_auth文档中找到的唯一解决方法是更改config.change_headers_on_each_request以false避免以这种方式重新生成令牌.
但我不喜欢这种解决方案,因为我认为它以不安全的方式隐藏真正的问题,而不是解决令牌丢失问题.有什么建议吗?
这是我的问题,我为User模型覆盖了控制器:
mount_devise_token_auth_for 'User', at: 'auth', controllers: {
registrations: 'v1/authentication/registrations'
sessions: 'v1/authentication/sessions'
token_validations: 'v1/authentication/token_validations'
}
Run Code Online (Sandbox Code Playgroud)
这样工作正常,没有问题。但是,当我使用相同的控制器添加新模型时:
mount_devise_token_auth_for 'Admin', 'admin_auth', controllers: {
sessions: 'v1/authentication/sessions',
token_validations: 'v1/authentication/token_validations'
}
mount_devise_token_auth_for 'User', at: 'auth', controllers: {
registrations: 'v1/authentication/registrations',
sessions: 'v1/authentication/sessions',
token_validations: 'v1/authentication/token_validations'
}
Run Code Online (Sandbox Code Playgroud)
他们的Admin模型的响应标头没有auth键。响应是ok(200),但不返回auth标头。但是,如果删除Admin模型的控制器部分(替代),则响应将返回auth密钥。顺便说一句,重写仅更改控制器的渲染方法。有什么可以帮助找到解决此问题的方法吗?
我正在编写一个主要通过API访问的应用程序,但还将具有通过Web应用程序进行编辑的视图。
我想在两个平台上创建一个具有身份验证和授权的用户模型。
我很难理解devise和devise_token_auth库之间的关系,除了大多数铁路应用建议使用前者之外,后者对于仅API身份验证非常有用。
就我而言,应该使用什么适当的库?还是应该同时使用两者?我应该通过devise生成User模型,然后向其添加令牌auth吗?两个系统是否使用不同的身份验证方案?我只是想了解为什么devise_token_auth与devise分开存在。
我还对基于令牌的身份验证的复杂性有些困惑。简单地通过设计来注册和管理用户,为他们生成一个API密钥,然后让他们以此来签名他们的API请求,那会有什么问题。为什么在API中需要基于令牌的身份验证?
authentication ruby-on-rails devise ruby-on-rails-5 devise-token-auth
我可以创建一个用户并从 sign_in 获得成功响应,但该登录不包含要在将来的请求中使用的访问令牌。
我还可以确认令牌正在创建并保存在数据库中。
注册请求
curl -XPOST -H 'Content-Type: application/json' -d '{"email":"test@gmail.com", "password":"12345678"}' http://localhost:3000/auth
Run Code Online (Sandbox Code Playgroud)
退货
{
"status": "success",
"data": {
"id": 3,
"provider": "email",
"uid": "test@gmail.com",
"allow_password_change": false,
"name": null,
"nickname": null,
"image": null,
"email": "test@gmail.com",
"created_at": "2020-08-04T17:42:08.252Z",
"updated_at": "2020-08-04T17:42:08.311Z"
}
}
Run Code Online (Sandbox Code Playgroud)
有服务器日志
Started POST "/auth" for ::1 at 2020-08-04 13:42:07 -0400
(0.4ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by DeviseTokenAuth::RegistrationsController#create as */*
Parameters: {"email"=>"test@gmail.com", "password"=>"[FILTERED]", "registration"=>{"email"=>"test@gmail.com", "password"=>"[FILTERED]"}}
Unpermitted parameter: :registration
Unpermitted parameter: …Run Code Online (Sandbox Code Playgroud) 我正在使用带有devise_token_auth的rails-api进行身份验证.我可以通过以下方式登录:
curl -i http://localhost:3000/api/v1/auth/sign_in -F email="user@nowhere.org" -F password="password"
Run Code Online (Sandbox Code Playgroud)
但是,注销失败并显示404,并显示错误"用户未找到或未登录".通过:
curl -X DELETE -i http://localhost:3000/api/v1/auth/sign_out
Run Code Online (Sandbox Code Playgroud)
我已尝试使用多个参数和标头值组合的此命令的变体,例如:
curl -X DELETE -i http://localhost:3000/api/v1/auth/sign_out -F email="user@nowhere.org" -H Access-Token="Ae1yaTYLkSAgdhz3LtPAZg" -H Client="9AmYF6NS8tP6EOD5nPSuxw" -H Expiry="1443073493" -H Uid="user@nowhere.org" -H Token-Type="Bearer"
Run Code Online (Sandbox Code Playgroud)
无济于事.类似构造的RSpec测试也会因相同的响应和错误而失败,并且日志表明请求是通过DeviseTokenAuth :: SessionsController#destroy作为JSON处理的.
当然,我实际上并没有使用curl进行身份验证; 在编写相关代码之前,只需验证请求结构.
我正在使用 REST API 创建一个新的 SPA,对于后端,我使用 Rails 和 devise_token_auth。我是令牌身份验证的新手,在搜索时我看到很多支持 JWT 的前端库,但我无法判断这是否与 devise_token_auth 兼容。
JWT 是网络令牌的标准吗?devise_token_auth 使用的是它吗?
使用默认的--api标签在rails 5中制作了一个新API,并使用command安装了devise_token_auth gem
rails generate devise_token_auth:install User auth。这样做时rake:db:migrate,我得到这个undefined method 'devise' for User (call 'User.connection' to establish a connection)奇怪的错误,因为devise_token_auth建立在devise之上。
因此,注释掉路由会向包含以下内容的user.rb文件抛出错误
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,:omniauthable
include DeviseTokenAuth::Concerns::User
在注释掉这些行之后,它将向包含以下内容的应用程序控制器引发错误:
include DeviseTokenAuth::Concerns::SetUserByToken
在注释掉这一行之后,我得到了devise:orm错误。
还尝试将devise添加到gemfile并为用户安装devise,但也失败了,再次抛出此错误 undefined method 'devise' for User (call 'User.connection' to establish a connection)
预期要进行的迁移,
出现设计错误
应该怎么做才能解决这个问题?
PS-这是我使用Devise_token_auth gem制作的第三个API,之前没有遇到过此问题
NoMethodError:#User:0x000055ce01dcf0a8的未定义方法`current_sign_in_at'
我认为这是某种会话方法错误
我有一个用于前端的angular6应用程序和用于后端的rails,所以对我来说最好的选择是选择devise_token_auth和ng_token_auth进行用户身份验证。
我安装了devise_token_auth gem,然后在终端中执行此行代码
"rails generate devise_token_auth:install User auth"
and on migration there was an error, I solved the issue by adding
"extend Devise::Models"
to the USER model and then migration had worked, then I created a user in the backend and tried to call sign_in using postman and the error "NoMethodError: undefined method `current_sign_in_at' for #User:0x000055ce01dcf0a8" came
I want the user to get authenticated using this gem or some other gem if they exist