2017-07-31 104 views

回答

0

作爲一種解決方案,我從控制器向節點服務器提供令牌,並且在客戶端連接到節點服務器之後,客戶端被請求發送持有者令牌 - 並使用jsonwebtoken包從中提取jti,該包用於與令牌從控制器收到。

這裏去代碼:

控制器

$userTokenReceiver = $pm->receiver->tokens()->where('revoked', 'false') 
    ->select('id')->get()->sortBy('created_at', true)->first(); 
if ($userTokenReceiver && $userTokenReceiver->toArray()['id']) 
    $dataRedis->receiver_token = $userTokenReceiver->toArray()['id']; 

節點服務器

var jwt = require('jsonwebtoken'); 
... 
io.on('connection', function(socket) { 
    socket.waitingAuthorize = true; 
    // E> authentication_request: after connection asking client to provide token 
    socket.emit('authentication_request'); 

    // O> authorize: waiting for client's response to auth request 
    socket.on('authorize', function(token) { 
     var decoded = jwt.decode(token); 

     if (!decoded || !decoded.jti) { 
      socket.disconnect(true); 
      return; 
     } 

     socket.waitingAuthorize = false; 
     socket.token = decoded.jti; // this is compared to token received from controller 
    }); 
... 
相關問題