Customising token response Laravel Passport

后端 未结 5 1485
被撕碎了的回忆
被撕碎了的回忆 2020-12-16 11:34

I am working on an API at the moment and have hit a brick wall. I am using Passport with the \'Password\' grant type.

I want to return the user information with the

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-16 12:05

    Two steps.

    1. Add a new route in your routes file.

    // routes/api.php
    
    Route::post('oauth/token', 'AuthController@auth');
    

    Keep in mind this will change the route for getting the token from /oauth/token to /api/oauth/token.

    2. Add the controller method.

    getContent();
    
                // $tokenInfo will contain the usual Laravel Passort token response.
                $tokenInfo = json_decode($token, true);
    
                // Then we just add the user to the response before returning it.
                $username = $request->getParsedBody()['username'];
                $user = User::whereEmail($username)->first();
                $tokenInfo = collect($tokenInfo);
                $tokenInfo->put('user', $user);
    
                return $tokenInfo;
        }
    }
    

提交回复
热议问题