Laravel API Authentication
Laravel API Authentication
Installation
<?php
namespace App;
use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
} <?php
namespace App\Providers;
use Laravel\Passport\Passport;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Passport::routes();
}
} Configuration
Issuing Access Tokens
Password Grant Tokens
Implicit Grant Tokens
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Passport::routes();
Passport::enableImplicitGrant();
} Route::get('/redirect', function () {
$query = http_build_query([
'client_id' => 'client-id',
'redirect_uri' => 'http://example.com/callback',
'response_type' => 'token',
'scope' => '',
]);
return redirect('http://your-app.com/oauth/authorize?'.$query);
}); Is most commonly used for JavaScript or mobile applications where the client credentials can't be securely stored.
Client Credentials Grant Tokens
Use Laravel\Passport\Http\Middleware\CheckClientCredentials;
protected $routeMiddleware = [
'client' => CheckClientCredentials::class,
]; Personal Access Tokens
Protecting Routes
Token Scopes
Allow your API clients to request a specific set of permissions when requesting authorization to access an account.
Consuming Your API With JavaScript
Events
Testing
Related concepts
→
Laravel API Authentication
→
- Laravel API Authentication: Installation
- Laravel API Authentication: Configuration
- Laravel API Authentication: Issuing Access Tokens
- Laravel API Authentication: Password Grant Tokens
- Laravel API Authentication: Implicit Grant Tokens
- Laravel API Authentication: Client Credentials Grant Tokens
- Laravel API Authentication: Personal Access Tokens
- Laravel API Authentication: Protecting Routes
- Laravel API Authentication: Token Scopes
- Laravel API Authentication: Consuming Your API With JavaScript
- Laravel API Authentication: Events
- Laravel API Authentication: Testing
Semantic portal