Get user data using access token in laravel passport client app

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Retrieving User Data with Access Token in Laravel Passport Client App Introduction: In this comprehensive guide, we will explore how to use the access token generated by Laravel Passport to obtain user data from a server application (server.app) within a client-side application (client.app). We will discuss various aspects of implementing this process and provide code examples for each step. 1. Getting Started with Laravel Passport: To begin, we need to create the server and client applications using the Laravel Passport documentation. Firstly, follow the instructions provided in https://laravelcompany.com/docs/5.6/passport to set up your server app (server.app) and client app (client.app). 2. Server Application Route: In the server application's route file, create a 'callback' route that will process the token exchange using Guzzle HTTP client. Here is an example code snippet for this route: ```php Route::get('callback', function (Request $request) { $http = new GuzzleHttp\Client; $response = $http->post('http://server.app/oauth/token', [ 'form_params' => [ 'grant_type' => 'authorization_code', 'client_id' => 3, 'client_secret' => 'secret', 'redirect_uri' => 'http://client.app/callback', 'code' => $request->code ] ]); return json_decode((string) $response->getBody(), true)['access_token']; }); ``` 3. Getting the Access Token: With the server application's callback route, we can generate an access token using the client application that redirects to the callback route. The token will be available in the URL of the callback page: http://client.app/callback?code=access_token 4. Making a Request with the Access Token: Now that you have an access token, you are ready to make a request for user data from the server application using the Laravel Passport-generated API endpoints. For instance, let's suppose you want to retrieve users' emails. Here is how it can be done in your client application: ```php // Retrieve users' emails with access token $token = 'YOUR_ACCESS_TOKEN'; $http = new GuzzleHttp\Client; $response = $http->get('http://server.app/api/user', [ 'headers' => [ 'Authorization' => 'Bearer ' . $token ] ]); $usersData = json_decode((string) $response->getBody(), true); foreach ($usersData as $user) { echo $user['email'] . "\n"; } ``` 5. Conclusion: In this blog post, we have covered the process of obtaining user data using access tokens generated by Laravel Passport in a client application (client.app). We started with setting up your server and client applications, then accessed the callback route to obtain an access token. Finally, we demonstrated how to use this token in your client application for retrieving user data through API endpoints. Make sure you follow these steps carefully to ensure the successful implementation of user authentication and data management in Laravel Passport-based client applications.