简体   繁体   English

Rails Devise和devise_token_auth。 不能一起工作

[英]Rails Devise and devise_token_auth. Not working alongside

I have a Rails 4.1 Application running with Devise for authentication. 我有一个运行Devise进行身份验证的Rails 4.1应用程序。

For access via mobile apps i would like to implement token auth with the recommended devise_token_auth gem. 为了通过移动应用程序进行访问,我想使用推荐的devise_token_auth gem实现令牌认证。 I do not use Omniauth 我不使用Omniauth

The functionality of the existing app should not be altered. 现有应用程序的功能不应更改。

What i did: 我做了什么:

Installed devise_token_auth via gemfile. 通过gemfile安装了devise_token_auth。

Used the generator: rails g devise_token_auth:install User auth 使用了生成器:rails g devise_token_auth:install用户auth

Changed the migration to add the required fields. 更改了迁移以添加必填字段。 Migration failed due missing of Omniauth. 由于缺少Omniauth,迁移失败。 So i also installed it. 所以我也安装了它。

Changed routes.rb 更改了routes.rb

devise_for :users, :skip => [:sessions, :registrations, :omniauth_callbacks]
  as :user do
    get 'register' => 'users/registrations#new', :as => :new_user_registration
    post 'register' => 'users/registrations#create', :as => :user_registration
    get 'sign_in' => 'devise/sessions#new', :as => :new_user_session
    post 'sign_in' => 'devise/sessions#create', :as => :user_session
    delete '/' => 'users/sessions#destroy', :as => :destroy_user_session
  end

added: 添加:

namespace :api do
scope :v1 do
  mount_devise_token_auth_for 'User', at: 'auth', skip: [:omniauth_callbacks]
end

end 结束

In User Model i have: 在用户模型中,我有:

devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable,
     :confirmable, :timeoutable, :lockable
include DeviseTokenAuth::Concerns::User

Now when i try to sign_up a new user it gives me the validation error: 现在,当我尝试注册一个新用户时,它给了我验证错误:

Uid can't be blank Uid不能为空

Does someone had the same problem and resolved it? 有人有同样的问题并解决了吗?

What i find strange is that it needs to have Omniauth installed. 我发现奇怪的是,它需要安装Omniauth。

Update: 更新:

I overwrite the Devise registration controller create action: 我覆盖了Devise注册控制器的create动作:

build_resource(sign_up_params)
 resource.uid = resource.email
 resource.provider = ''

Now when i sign_in i get: 现在,当我登录时我得到:

{"errors":["Authorized users only."]} {“错误”:[“仅授权用户。”]}

in Browser. 在浏览器中。

Adding the following to app/models/user.rb: 将以下内容添加到app / models / user.rb:

before_validation do
  self.uid = email if uid.blank?  
end

did it for me. 为我做了。 Also make sure the provider is set to "email" for "provider". 还要确保将提供者设置为“电子邮件”。

Well I'm currently struggling with the same thing. 好吧,我目前正为同样的事情而苦苦挣扎。 Trying to add devise_token_auth to Devise, and it is not working so far for me. 试图将devise_token_auth添加到Devise,到目前为止,它对我来说还行不通。

As far as this goes, are you talking about "sign_up" for Devise, or devise_token_auth ? 就目前而言,您是在谈论Devise或devise_token_auth “ sign_up”吗? If it is for Devise, I supposed setting uid=email before creating the record would solve this. 如果用于Devise,我应该在创建记录之前设置uid=email解决此问题。

This error is raised by devise_token_auth, not by devise. 该错误是由devise_token_auth引起的,而不是由devise引起的。 So essentially, devise_token_auth is trying to authenticate your normal devise routes the same way it would normally authenticate an api request. 因此,从本质上讲,devise_token_auth尝试以与通常对api请求进行身份验证相同的方式对您的常规devise路由进行身份验证。 Your normal devise routes are authenticating via session, not via token, so you'll get this error: 您的正常设计路线是通过会话而不是通过令牌进行身份验证的,因此您会收到以下错误消息:

{"errors":["Authorized users only."]} 

There are a couple of things that could be happening here. 这里可能发生几件事。 First, make sure that you're only looking for token validation on the actions of your API controllers. 首先,请确保仅在对API控制器的操作进行令牌验证。 So make sure that this line is included in your BaseAPIController, and not in your ApplicationController. 因此,请确保此行包含在BaseAPIController中,而不包含在ApplicationController中。

include DeviseTokenAuth::Concerns::SetUserByToken

The other possibility is that you have some namespacing issues in your routes.rb. 另一种可能性是您的route.rb中存在一些命名空间问题。 Make sure that you have something like this. 确保您有这样的东西。 You need to have devise_for first, and the token_auth properly namespaced or it will cause validations issues on your other routes. 您需要首先拥有devise_for,并正确命名token_auth的名称空间,否则它将导致其他路由的验证问题。

Rails.application.routes.draw do

  devise_for :admins

  namespace :api do
    scope :v1 do
      mount_devise_token_auth_for 'user', at: 'auth'
    end
  end
end

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM