1. Go to this page and download the library: Download rinvex/laravel-oauth library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rinvex / laravel-oauth example snippets
namespace App\Models;
use Rinvex\Oauth\Traits\HasApiTokens;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasApiTokens;
}
use Rinvex\Oauth\Models\Client as BaseClient;
class Client extends BaseClient
{
/**
* Determine if the client should skip the authorization prompt.
*
* @return bool
*/
public function skipsAuthorization()
{
return $this->firstParty();
}
}
use Rinvex\Oauth\Repositories\AccessTokenRepository;
use Rinvex\Oauth\Repositories\RefreshTokenRepository;
// Revoke an access token...
$accessTokenRepository = app(AccessTokenRepository::class);
$accessTokenRepository->revokeAccessToken($tokenId);
namespace App\Models;
use Rinvex\Oauth\Traits\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
/**
* Find the user instance for the given username.
*
* @param string $username
* @return \App\Models\User
*/
public function findForOAuth($username)
{
return $this->where('username', $username)->first();
}
}
namespace App\Models;
use Illuminate\Support\Facades\Hash;
use Rinvex\Oauth\Traits\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
/**
* Validate the password of the user for the OAuth password grant.
*
* @param string $password
* @return bool
*/
public function validateForOAuthPasswordGrant($password)
{
return Hash::check($password, $this->password);
}
}
'grants' => [
'Password' => ['enabled' => true, 'expire_in' => new DateInterval('P1Y')],
'Implicit' => ['enabled' => false, 'expire_in' => new DateInterval('P1Y')],
'AuthCode' => ['enabled' => true, 'expire_in' => new DateInterval('P1Y')],
'RefreshToken' => ['enabled' => true, 'expire_in' => new DateInterval('P1Y')],
'PersonalAccess' => ['enabled' => true, 'expire_in' => new DateInterval('P1Y')],
'ClientCredentials' => ['enabled' => true, 'expire_in' => new DateInterval('P1Y')],
],
Route::get('orders', function () {
// Access token has both "scope-id-2" and "scope-id-1" scopes...
})->middleware(['auth:api', 'scopes:scope-id-2,scope-id-1']);
Route::get('orders', function () {
// Access token has either "scope-id-2" or "scope-id-1" scope...
})->middleware(['auth:api', 'scope:scope-id-2,scope-id-1']);
use Illuminate\Http\Request;
Route::get('orders', function (Request $request) {
$scope = 'scope-id-1';
if ($request->user()->token()->abilities->map->getRouteKey()->contains($scope)) {
//
}
});
'web' => [
// Other middleware...
\Rinvex\Oauth\Http\Middleware\CreateFreshApiToken::class,
],