自定义令牌响应

时间:2019-11-02 09:59:21

标签: php laravel token laravel-passport

我当前的登录响应是这样的,

{
    "token_type": "Bearer",
    "expires_in": 31536000,
    "access_token": "eyJ0eWq4Pt2B7M8W5zWwepDNpWqL0FOzdA8sPNiPhfxJcaktCq7GqQJDNLfPCuN5_Cl4",
    "refresh_token": "def50200278e5e6e658f2149489a6d1578ce4cdef1abb80e21df5efa4bbbc7003840df34768a091a57e88c0"
}

但是我需要将此响应作为JSON

{  
   "success":"true",
   "message":"Login Successful",
   "status":"200",
   "data":
   {  
     "token_type": "Bearer",
     "expires_in": 31536000,
     "access_token": "eyJ0eWq4Pt2B7M8W5zWwepDNpWqL0FOzdA8sPNiPhfxJcaktCq7GqQJDNLfPCuN5_Cl4",
     "refresh_token": "def50200278e5e6e658f2149489a6d1578ce4cdef1abb80e21df5efa4bbbc7003840df34768a091a57e88c0"
   }
}

1 个答案:

答案 0 :(得分:0)

假设您有一个登录控制器,则将以此方式设计该方法。

要返回您所说的回复,请使用下面的代码

public static function LoginUser(Request $request) {
    $http = new guzzleclient;

    $response = $http->post(env('APP_URL').'/oauth/token', [
        'form_params' => [
            'grant_type' => 'password',
            'client_id' => env('CLIENT_ID'),
            'client_secret' => env('CLIENT_SECRET'),
            'username' => $request->email,
            'password' => $request->password,
            'scope' => '*',    
        ],
    ]); 

    $response = json_decode((string) $response->getBody(), true);
    $final_resp = [
        'success' => "true", 
        'message' => "Login Successful", 
        'data' => $response, 
        'status' => "200"
    ];

    return response()->json($final_resp, 200);
}
  

请注意,此示例使用密码授予方法。