简体   繁体   English

如何使用Devise in Rails通过api登录?

[英]How can I sign in via api using Devise in Rails?

I use Devise gem for auth and gem 'devise_token_auth' for api auth. 我将Devise gem用于身份验证,将gem'devise_token_auth'用于api身份验证。 In my routes I defined the following 在我的路线中,我定义了以下内容

  devise_for :users

  # token auth routes available at /api/v1/auth
  namespace :api do
    scope :v1 do
      mount_devise_token_auth_for 'User', at: 'auth'
    end

But now I trying to sign in using curl and get an error: My request for simple devise auth 但是现在我尝试使用curl登录并得到一个错误:我对简单设计验证的请求

curl -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST http://localhost:3000/users/sign_in -d "{'user' : { 'email' : 'admin@mail.ru', 'password' : 'r465ee'}}" -c cookie

Error 错误

ActionDispatch::ParamsParser::Parse Error in Devise::SessionsController#create

The same for the auth scope route. 对于auth作用域路由也是如此。

mount_devise_token_auth_for 'User', at: 'auth' 

means you mounted the engine on /auth path. 表示您将引擎安装在/auth路径上。 /users/sign_in is the original devise(session/cookies based) login path. /users/sign_in是原始设计(基于会话/ cookies)的登录路径。 Also you don't need to use -c cookie anymore because it's token based. 同样,您不再需要使用-c cookie ,因为它是基于令牌的。

Try 尝试

curl -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST http://localhost:3000/auth/sign_in -d "{'user' : { 'email' : 'admin@mail.ru', 'password' : 'r465ee'}}" 

or including namespace and scope 或包括名称空间和范围

curl -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST http://localhost:3000/api/v1/auth/sign_in -d "{'user' : { 'email' : 'admin@mail.ru', 'password' : 'r465ee'}}" 

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

相关问题 使用Devise与Rails 4通过API进行注册/登录 - Use Devise with Rails 4 to sign up/in via an API Ruby on Rails:如何在测试内部使用devise登录? - Ruby on Rails: How can I sign in with devise inside of my tests? 如何从 Rails 控制台注销设计用户? - How can I sign out a devise user from the Rails console? Rails 和 Devise 使用 POST 请求退出。 如何将其更改为 DELETE 请求? - Rails & Devise using POST request for sign out. How can I change this to a DELETE request? 如何使用Devise Token Auth通过API更改密码? 滑轨5 - How to change password via API using Devise Token Auth? Rails 5 如何在“登录”和“注册”页面上使用Rails Devise设置seo标题标签? - How can I set seo title tags with Rails Devise on the Sign In and Sign Up pages? 如何通过API对Rails和Devise进行身份验证? - How to authenticate with Rails and Devise via API? 将Devise与Ruby on Rails一起使用,如果用户使用无效的auth_token,如何将用户发送到sign_in路径? - Using Devise with Ruby on Rails, how can I send the user to sign_in path if they use an invalid auth_token? 我如何在葡萄中登录设计用户? - How can I sign in user of devise in grape? 如何在rails-devise网站上注明特定用户(不是current_user)? - How can I make specific user(not current_user) sign out on rails-devise website?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM