2016-10-05 15 views
6

पर https://github.com/tymondesigns/jwt-auth current user प्राप्त करने के लिए संभव है? क्योंकि अभी मैं केवल टोकन उत्पन्न कर सकता हूं (जब कोई उपयोगकर्ता साइन इन करता है)।Laravel JWT Auth उपयोगकर्ता को लॉग इन

public function login(Request $request) 
{ 
    $credentials = $request->only('email', 'password'); 

    try { 
     if (! $token = JWTAuth::attempt($credentials)) { 
      return response()->json(['error' => 'invalid_credentials'], 401); 
     } 
    } catch (Tymon\JWTAuth\Exceptions\JWTException $e) { 
     return response()->json(['error' => 'could_not_create_token'], 500); 
    } 

    return response()->json(compact('token')); 
} 
+0

आप जेडब्ल्यूटी – Weedoze

+0

ठीक दिलचस्प अंदर उपयोगकर्ता नाम डाल सकते: अधिक जानकारी के लिए

$user = JWTAuth::toUser($token); return response()->json(compact('token', 'user')); 

, इस toUser तरीका है। क्या आप एक उदाहरण दे सकते हैं? – Jamie

+0

मैंने कभी लार्वेल के साथ काम नहीं किया है लेकिन यह जेडब्ल्यूटी का मूल है। इसके बारे में कुछ जानकारी जांचें। आप देखेंगे कि आप जेडब्ल्यूटी के अंदर जो कुछ भी चाहते हैं उसे स्टोर कर सकते हैं। असल में आप उपयोगकर्ता नाम और भूमिका संग्रहित करते हैं। https://jwt.io/ – Weedoze

उत्तर

5

आप उपयोगकर्ता डेटा लॉग इन कर सकते हैं।

$credentials = $request->only('code', 'password', 'mobile'); 
try { 
    // verify the credentials and create a token for the user 
    if (! $token = JWTAuth::attempt($credentials)) { 
     return response()->json(['error' => 'invalid_credentials'], 401); 
    } 
} catch (JWTException $e) { 
    // something went wrong 
    return response()->json(['error' => 'could_not_create_token'], 500); 
} 

$currentUser = Auth::user(); 
print_r($currentUser);exit; 
5

आप Auth::user();

आप JWTAuth की toUser विधि का उपयोग कर सकते हैं की जरूरत नहीं है। बस पैरामीटर के रूप में टोकन गुजरती हैं और तुम वापस उपयोगकर्ता की जानकारी मिल जाएगा:

/** 
* Find a user using the user identifier in the subject claim. 
* 
* @param bool|string $token 
* 
* @return mixed 
*/ 

public function toUser($token = false) 
{ 
    $payload = $this->getPayload($token); 

    if (! $user = $this->user->getBy($this->identifier, $payload['sub'])) { 
     return false; 
    } 

    return $user; 
} 
संबंधित मुद्दे