2016-02-16 114 views
0

我使用ngAA來集成Angularjs和Laravel jwt-auth。在Laravel的AuthenticateController我用這個代碼:如何將ngAA集成到Laravel jwt-auth

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

     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); 
     } 

     // if no errors are encountered we can return a JWT 
     return response()->json(compact('token')); 
    } 

但ngAA在angularjs需要這種格式響應:

{ 
    token:'your jwt valid token', 
    user:{ 
     ....., 
     ..... 
    } 
} 

如何生成令牌和用戶對象的Laravel這種格式?

回答

0

我解決問題,更換

return response()->json(compact('token')); 

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

,不要忘了use App\User;