2015-12-10 76 views
0

在我的Rails應用程序中,在成功使用設計登錄後,我想調用返回令牌的外部服務。我如何將它傳遞給客戶端以保持它(例如在localStorage中)?在設計身份驗證後在客戶端存儲數據

class Users::SessionsController < Devise::SessionsController 
    def create 
    super 
    login_to_chat #successful login 
    end 

    def login_to_chat 
    uri = URI('https://xxxxxx.ws/authenticate') 

    https = Net::HTTP.new(uri.host,uri.port) 
    https.use_ssl = true 

    req = Net::HTTP::Post.new(uri) 
    req['Content-Type'] = 'application/json' 
    req.body = params[:user].slice(:username,:password).to_json 

    res = https.request(req) 

    if res.kind_of? Net::HTTPSuccess 
     json = JSON.parse(res.body) 

     # how can I pass json['public_token'] to the client? 

    end 
    end 
end 

回答

0

我不確定哪種解決方案最適合您的情況。我的想法是:

  1. 寫令牌在控制器(其中用戶可以輕鬆地訪問)

    cookies[key] = value 
    
  2. 到cookie此是設計出一種 '創造' 的方法:: SessionsController

    def create 
        self.resource = warden.authenticate!(auth_options) 
        set_flash_message(:notice, :signed_in) if is_flashing_format? 
        sign_in(resource_name, resource) 
        yield resource if block_given? 
        respond_with resource, location: after_sign_in_path_for(resource) 
    end 
    

    所以你可以用你的令牌作爲參數覆蓋'after_sign_in_path_for'方法,客戶端知道這個參數,你可以做任何你想做的事

  3. 在情況下,「login_to_chat」需要很長的時間,我們可以通過WebSocket的做背景,然後通知客戶

+0

TNX HIEU。即使我沒有指定這是我需要的:多種選擇來更好地評估安全問題。 – masciugo