如何将ngAA集成到Laravel jwt-auth

时间:2016-02-16 07:31:17

标签: angularjs laravel authentication jwt

我使用ngAA来集成Angularjs和Laravel jwt-auth。在Laravel的AuthenticateController中,我使用以下代码:

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

但是angularjs中的ngAA需要这种格式的响应:

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

如何在Laravel中生成这种格式的令牌和用户对象?

1 个答案:

答案 0 :(得分:0)

我解决问题替换

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

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

并且不要忘记use App\User;