Last active
April 13, 2019 23:15
-
-
Save ansargondal/e8bc1f77ce4a4a95c423f3df50d9ce7a to your computer and use it in GitHub Desktop.
Laravel Passport Setup and Installation Steps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
STEP 1: php artisn make:auth | |
STEP 2: composer require laravel/passport | |
STEP 3: php artisan migrate | |
STEP 4: php artisan passport:install | |
STEP 5: use HasApiTokens Trait in User Model. | |
STEP 6: Add Passport::routes(); to boot() method of RouteServiceProvider Class | |
STEP 7: Change api driver('token') to 'passport' in config => auth.php | |
STEP 8: Install a new Laravel instance for client-server(Server which which is asking for authorization from us) | |
STEP 9: Install the guzzel package on client-server ( composer require guzzlehttp/guzzle ) | |
STEP 10: Run php artisan passport:client command on your original application | |
- Enter user ID which you got by running passport:install command (in STEP 4) | |
- Enter the name of the client anything ( Client Server) | |
- Redirect after authorization (http://127.0.0.1:8001/callback) | |
STEP 11: Copy the generated Client ID and Client Secret | |
STEP 12: In client-server installation => web.php file put the following code & replace the client ID and Secret with the | |
values copied in STEP 11. | |
Route::get('/redirect', function () { | |
$query = http_build_query([ | |
'client_id' => '3', | |
'redirect_uri' => 'http://127.0.0.1:8001/callback', | |
'response_type' => 'code', | |
'scope' => '' | |
]); | |
return redirect('http://127.0.0.1:8000/oauth/authorize?'.$query); | |
}); | |
Route::get('/callback', function (Illuminate\Http\Request $request) { | |
$http = new \GuzzleHttp\Client; | |
$response = $http->post('http://127.0.0.1:8000/oauth/token', [ | |
'form_params' => [ | |
'client_id' => '3', | |
'client_secret' => 'fdFaiBUBjDmLoKiBmFpuTvLyAXyoCuusAvJljBQF', | |
'grant_type' => 'authorization_code', | |
'redirect_uri' => 'http://127.0.0.1:8001/callback', | |
'code' => $request->code, | |
], | |
]); | |
return json_decode((string) $response->getBody(), true); | |
}); | |
STEP 13: Go to your orignal application => Kernel.php => middlewaregroups => api => 'auth:api' | |
STEP 14: Run both the applications & visit https://127.0.0.1:8001/redirect | |
STEP 11: Client-server will redirect us to our orignal website from where we have to login and authorize the app. | |
STEP 12: After successfull authorization it will redirect back to client-server with 'access_token', 'token_type', 'refresh_token'. | |
client can use access_token to get access to the underlying data. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment