2016-03-31 38 views
1

我使用devise_token_auth(https://github.com/lynndylanhurley/devise_token_auth)對移動設備進行身份驗證,並且還需要爲我的web應用程序使用devise。將devise_token_auth庫用於webapp和api

問題是用戶可以通過devise_token_auth註冊,但是如果用戶登錄並嘗試調用某個包含「before_action :authenticate_user!」的控制器中的某個函數,則會出現一條錯誤消息Authorized Users Only

我想知道是否有辦法一起使用devisedevise_token_auth

代碼:

class RestrictedController < ApplicationController 
    before_action :authenticate_user! 

    def stuff 
    head :ok 
    end 
end 

回答

0

首先,你應該知道,devise不再管理用戶的會話。這就是爲什麼建議使用devise_token_auth

每當你指定一個控制器before_action :authenticate_user!,所有的動作(如RestrictedController#stuff)將要求用戶在簽署。devise_token_auth指望得到下列查詢頭4個PARAMS。

以下是你需要做的:

  1. 登錄使用devise_token_auth提供的路線。
  2. 如果登錄成功,您將在標題中收到access-token,clienttoken-typeuid
  3. 每當你想運行一個控制器動作,其中用戶必須登錄指定,在查詢標題中,access-tokenclienttoken-typeuid