簡體   English   中英

無法弄清楚如何使用devise_token_auth gem更新用戶

[英]Can't figure out how to update users with devise_token_auth gem

我正在使用旨在與VUE前端一起使用的Rails API。 這是我第一次僅將Rails作為API使用。

我熟悉在其他Rails應用程序中使用devise進行用戶身份驗證。

我們的登錄有效,與注銷相同。 我不知道如何編輯和刪除用戶。

我的耙路顯示如下:

        new_api_user_session GET      /api/v1/auth/sign_in(.:format)                            devise_token_auth/sessions#new
            api_user_session POST     /api/v1/auth/sign_in(.:format)                            devise_token_auth/sessions#create
    destroy_api_user_session DELETE   /api/v1/auth/sign_out(.:format)                           devise_token_auth/sessions#destroy
       new_api_user_password GET      /api/v1/auth/password/new(.:format)                       devise_token_auth/passwords#new
      edit_api_user_password GET      /api/v1/auth/password/edit(.:format)                      devise_token_auth/passwords#edit
           api_user_password PATCH    /api/v1/auth/password(.:format)                           devise_token_auth/passwords#update
cancel_api_user_registration GET      /api/v1/auth/cancel(.:format)                             devise_token_auth/registrations#cancel
   new_api_user_registration GET      /api/v1/auth/sign_up(.:format)                            devise_token_auth/registrations#new
  edit_api_user_registration GET      /api/v1/auth/edit(.:format)                               devise_token_auth/registrations#edit
       api_user_registration PATCH    /api/v1/auth(.:format)                                    devise_token_auth/registrations#update

我的routes.rb就像這樣:

Rails.application.routes.draw do

  root 'home#index'

  namespace :api do
    scope :v1 do
      resource :profile, only: [:show]
      mount_devise_token_auth_for "User", at: 'auth'

     ## other code
    end
  end

end

我正在使用CURL測試api。 以下命令可以成功登錄:

curl -X POST -v -H 'Content-Type: application/json' http://localhost:3000/api/v1/auth/sign_in -d '{"email": "test@example.com", "password": "password"}'

沒問題。

更新用戶的路由應為http:// localhost:3000 / api / v1 / auth /

登錄后會給我客戶端和訪問令牌的值。 當我將它們彈出到以下curl命令中時,我希望用戶進行更新。 我要更新的屬性是“ sortOrder”。

curl -X PATCH -v -H 'Content-Type: application/json' -H 'access-token: XUbhcAgWlVnARf9Ps4rjR' -H 'client: Lxg5us7MpUyAuM5xQGNuqg'  -H 'uid: test@example.com' http://localhost:3000/api/v1/auth/ -d '{"sortOrder": "DESC"}'

通過該命令,我的rails日志顯示:

Started PATCH "/api/v1/auth/" for 127.0.0.1 at 2018-12-26 12:10:53 -0800
Processing by DeviseTokenAuth::RegistrationsController#update as */*
  Parameters: {"sortOrder"=>"DESC", "registration"=>{"sortOrder"=>"DESC"}}
  User Load (0.6ms)  SELECT  `users`.* FROM `users` WHERE `users`.`uid` = 'test@example.com' LIMIT 1
Unpermitted parameter: registration
Completed 404 Not Found in 101ms (Views: 0.3ms | ActiveRecord: 0.6ms)

curl命令的輸出為:

*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> PATCH /api/v1/auth/ HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.47.0
> Accept: */*
> Content-Type: application/json
> access-token: XUbhcAgWlVnARf9Ps4rjR
> client: Lxg5us7MpUyAuM5xQGNuqg
> uid: test@example.com
> Content-Length: 50
> 
* upload completely sent off: 50 out of 50 bytes
< HTTP/1.1 404 Not Found
< X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< Content-Type: application/json; charset=utf-8
< Cache-Control: no-cache
< X-Request-Id: 1e5e61f6-1226-43dc-b8b3-30a576b0c03a
< X-Runtime: 0.104965
< Vary: Origin
< Transfer-Encoding: chunked
< 
* Connection #0 to host localhost left intact
{"success":false,"errors":["User not found."],"status":"error"}

我不確定我在這里缺少什么。 有了這個裝置后,我將無法使用裝置/注冊控制器。 我可能可以自己創建一個,但是應該可以。 我無法找到有關devise_token_auth gem的文檔,該文檔可解決用戶更新和刪除問題。

我得到的結果似乎是“找不到用戶”。 我嘗試了幾種方法來在請求中獲取用戶UID,電子郵件和ID,但似乎無濟於事。 有誰之前經歷過這個嗎?

看起來用戶沒有以某種方式被保存。 我會嘗試將:create添加到resource :profile, only: [:show, :create]在route.rb中。 我只是在學習RoR a Devise,所以我不能說這是100%的治愈方法,但值得一試。 按照相同的邏輯,您可以添加:edit:destroy操作來編輯/刪除配置文件/用戶。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM