OAuth Using Laravel Passport
This commit is contained in:
parent
98198a73fa
commit
14997d6e97
|
|
@ -18,6 +18,7 @@ class Kernel extends HttpKernel
|
|||
\App\Http\Middleware\EncryptCookies::class,
|
||||
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
|
||||
\Illuminate\Session\Middleware\StartSession::class,
|
||||
// \App\Http\Middleware\PassportCustomProviderAccessToken::class
|
||||
];
|
||||
|
||||
/**
|
||||
|
|
@ -54,6 +55,7 @@ class Kernel extends HttpKernel
|
|||
'can' => \Illuminate\Auth\Middleware\Authorize::class,
|
||||
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
|
||||
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
|
||||
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class
|
||||
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
|
||||
// 'passport-admin' => \App\Http\Middleware\CustomPassportProvider::class,
|
||||
];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Laravel\Passport\Passport;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
|
||||
|
||||
|
|
@ -25,6 +27,9 @@ class AuthServiceProvider extends ServiceProvider
|
|||
{
|
||||
$this->registerPolicies();
|
||||
|
||||
//
|
||||
Passport::routes();
|
||||
// Route::group(['middleware' => 'passport-admin'], function () {
|
||||
// Passport::routes();
|
||||
// });
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@
|
|||
"kalnoy/nestedset": "^4.3",
|
||||
"konekt/concord": "^1.2",
|
||||
"laravel/framework": "5.6.*",
|
||||
"laravel/passport": "^8.0@dev",
|
||||
"laravel/tinker": "^1.0",
|
||||
"nwidart/laravel-modules": "^3.2",
|
||||
"prettus/l5-repository": "^2.6",
|
||||
|
|
@ -47,7 +48,8 @@
|
|||
"webkul/laravel-shipping": "v0.1.0",
|
||||
"webkul/laravel-payment": "v0.1.0",
|
||||
"webkul/laravel-sales": "v0.1.0",
|
||||
"webkul/laravel-tax": "v0.1.0"
|
||||
"webkul/laravel-tax": "v0.1.0",
|
||||
"webkul/laravel-api": "v0.1.0"
|
||||
},
|
||||
"autoload": {
|
||||
"classmap": [
|
||||
|
|
@ -72,7 +74,8 @@
|
|||
"Webkul\\Shipping\\": "packages/Webkul/Shipping/src",
|
||||
"Webkul\\Payment\\": "packages/Webkul/Payment/src",
|
||||
"Webkul\\Sales\\": "packages/Webkul/Sales/src",
|
||||
"Webkul\\Tax\\": "packages/Webkul/Tax/src"
|
||||
"Webkul\\Tax\\": "packages/Webkul/Tax/src",
|
||||
"Webkul\\API\\": "packages/Webkul/API"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
|
|
|
|||
|
|
@ -217,6 +217,7 @@ return [
|
|||
Webkul\Payment\Providers\PaymentServiceProvider::class,
|
||||
Webkul\Sales\Providers\SalesServiceProvider::class,
|
||||
Webkul\Tax\Providers\TaxServiceProvider::class,
|
||||
Webkul\API\Providers\APIServiceProvider::class,
|
||||
],
|
||||
|
||||
/*
|
||||
|
|
|
|||
|
|
@ -13,8 +13,8 @@ return [
|
|||
],
|
||||
|
||||
'api' => [
|
||||
'driver' => 'token',
|
||||
'provider' => 'admins',
|
||||
'driver' => 'passport',
|
||||
'provider' => 'customers',
|
||||
],
|
||||
|
||||
'customer' =>[
|
||||
|
|
@ -27,10 +27,10 @@ return [
|
|||
'provider' => 'admins'
|
||||
],
|
||||
|
||||
'admin-api' => [
|
||||
'driver' => 'token',
|
||||
'provider' => 'admins',
|
||||
]
|
||||
// 'admin-api' => [
|
||||
// 'driver' => 'token',
|
||||
// 'provider' => 'admins',
|
||||
// ]
|
||||
],
|
||||
|
||||
'providers' => [
|
||||
|
|
|
|||
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace Webkul\API\Http\Controllers\Admin;
|
||||
|
||||
use Webkul\API\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Webkul\User\Models\Admin;
|
||||
|
||||
/**
|
||||
* Session controller for the APIs of user admins
|
||||
*
|
||||
* @author Prashant Singh <prashant.singh852@webkul.com> @prashant-webkul
|
||||
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
|
||||
*/
|
||||
class AuthController extends Controller
|
||||
{
|
||||
public function create(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'email' => 'required|email',
|
||||
'password' => 'required'
|
||||
]);
|
||||
|
||||
if (!auth()->guard('admin')->attempt(request(['email', 'password']))) {
|
||||
return response()->json(false, 200);
|
||||
}
|
||||
|
||||
if(auth()->guard('admin')->check()) {
|
||||
$admin = auth()->guard('admin')->user();
|
||||
|
||||
$token = $admin->createToken('admin-token')->accessToken;
|
||||
|
||||
return response()->json($token, 200);
|
||||
} else {
|
||||
return response()->json(false, 200);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Webkul\API\Http\Controllers;
|
||||
|
||||
use Illuminate\Foundation\Bus\DispatchesJobs;
|
||||
use Illuminate\Routing\Controller as BaseController;
|
||||
use Illuminate\Foundation\Validation\ValidatesRequests;
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
|
||||
class Controller extends BaseController
|
||||
{
|
||||
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
|
||||
}
|
||||
|
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
namespace Webkul\API\Http\Controllers\Customer;
|
||||
|
||||
use Webkul\API\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Webkul\Customer\Models\Customer;
|
||||
use Webkul\Customer\Http\Listeners\CustomerEventsHandler;
|
||||
use Cart;
|
||||
|
||||
/**
|
||||
* Session controller for the APIs of user customer
|
||||
*
|
||||
* @author Prashant Singh <prashant.singh852@webkul.com> @prashant-webkul
|
||||
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
|
||||
*/
|
||||
class AuthController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
protected $_config;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
$this->middleware('customer')->except(['show','create']);
|
||||
$this->_config = request('_config');
|
||||
|
||||
$subscriber = new CustomerEventsHandler;
|
||||
|
||||
Event::subscribe($subscriber);
|
||||
}
|
||||
|
||||
public function create(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'email' => 'required|email',
|
||||
'password' => 'required'
|
||||
]);
|
||||
|
||||
if (!auth()->guard('customer')->attempt(request(['email', 'password']))) {
|
||||
return response()->json([false], 200);
|
||||
}
|
||||
|
||||
if(auth()->guard('customer')->check()) {
|
||||
$customer = auth()->guard('customer')->user();
|
||||
|
||||
$token = $customer->createToken('customer-token')->accessToken;
|
||||
|
||||
return response()->json([$token], 200);
|
||||
} else {
|
||||
return response()->json([false], 200);
|
||||
}
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
auth()->guard('customer')->logout();
|
||||
|
||||
Event::fire('customer.after.logout', $id);
|
||||
|
||||
return redirect()->route($this->_config['redirect']);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
Route::prefix('api')->group(function () {
|
||||
//customer APIs
|
||||
Route::prefix('customer')->group(function () {
|
||||
Route::post('login', 'Webkul\API\Http\Controllers\Customer\AuthController@create')->name('login');
|
||||
});
|
||||
|
||||
//Admin APIs
|
||||
Route::prefix('admin')->group(function () {
|
||||
Route::post('login', 'Webkul\API\Http\Controllers\Admin\AuthController@create');
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Webkul\API\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class APIServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Bootstrap services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
$this->loadRoutesFrom(__DIR__.'/../Http/api.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* Register services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
{
|
||||
"name": "webkul/laravel-api",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Prashant Singh",
|
||||
"email": "prashant.singh852@webkul.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"webkul/laravel-user": "dev-master",
|
||||
"webkul/laravel-core": "dev-master",
|
||||
"webkul/laravel-product": "dev-master",
|
||||
"webkul/laravel-shop": "dev-master",
|
||||
"webkul/laravel-category": "dev-master",
|
||||
"webkul/laravel-tax": "dev-master",
|
||||
"webkul/laravel-inventory": "dev-master",
|
||||
"webkul/laravel-checkout": "dev-master"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Webkul\\API\\": "/"
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"laravel": {
|
||||
"providers": [
|
||||
"Webkul\\API\\AdminServiceProvider"
|
||||
],
|
||||
"aliases": {}
|
||||
}
|
||||
},
|
||||
"minimum-stability": "dev"
|
||||
}
|
||||
|
|
@ -26,7 +26,6 @@ class SessionController extends Controller
|
|||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
$this->middleware('customer')->except(['show','create']);
|
||||
$this->_config = request('_config');
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Webkul\Customer\Models;
|
||||
|
||||
use Laravel\Passport\HasApiTokens;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Webkul\Customer\Models\CustomerGroup;
|
||||
|
|
@ -13,7 +14,7 @@ use Webkul\Customer\Notifications\CustomerResetPassword;
|
|||
|
||||
class Customer extends Authenticatable
|
||||
{
|
||||
use Notifiable;
|
||||
use HasApiTokens, Notifiable;
|
||||
|
||||
protected $table = 'customers';
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Webkul\User\Models;
|
||||
|
||||
use Laravel\Passport\HasApiTokens;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Webkul\User\Models\Role;
|
||||
|
|
@ -10,7 +11,7 @@ use Webkul\User\Notifications\AdminResetPassword;
|
|||
|
||||
class Admin extends Authenticatable
|
||||
{
|
||||
use Notifiable;
|
||||
use HasApiTokens, Notifiable;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
|
|
|
|||
Loading…
Reference in New Issue