-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
171 additions
and
38 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace App\Middleware; | ||
|
||
use Closure; | ||
use Yuga\Http\Request; | ||
use Yuga\JWTAuth\UsesJWTTokens; | ||
use Yuga\Application\Application; | ||
use Yuga\Http\Middleware\IMiddleware; | ||
|
||
class ApiMiddleware implements IMiddleware | ||
{ | ||
use UsesJWTTokens; | ||
/** | ||
* @var \Yuga\Application\Application | null | ||
*/ | ||
protected $app; | ||
/** | ||
* ------------------------------------------------------------------------- | ||
* Inject any objects (in the contructor) you want to use in this middleware | ||
* We will worry about instantiating them for you | ||
* ------------------------------------------------------------------------- | ||
*/ | ||
public function __construct(Application $app) | ||
{ | ||
$this->app = $app; | ||
} | ||
|
||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Yuga\Http\Request $request | ||
* @param \Closure $next | ||
* @return mixed | ||
*/ | ||
public function run(Request $request, Closure $next) | ||
{ | ||
try { | ||
$token = $request->getBearerToken(); | ||
|
||
try { | ||
$this->verifyToken($token); | ||
return $next($request); | ||
} catch (\Exception $e) { | ||
return response()->json([ | ||
'app_status' => false, | ||
'message' => $e->getMessage(), | ||
'data' => null | ||
]); | ||
} | ||
|
||
} catch (\Exception $e) { | ||
return response()->json([ | ||
'app_status' => false, | ||
'message' => 'Unauthorized Access', | ||
'data' => null | ||
], null, 401); | ||
} | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace App\Middleware; | ||
|
||
use Yuga\Http\Middleware\BaseCsrfVerifier; | ||
|
||
class WebMiddleware extends BaseCsrfVerifier | ||
{ | ||
protected $except = []; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace App\Providers; | ||
|
||
use Yuga\Route\Route; | ||
use App\Middleware\WebMiddleware; | ||
use Yuga\Providers\ServiceProvider; | ||
use Yuga\Interfaces\Application\Application; | ||
use Yuga\Route\Exceptions\NotFoundHttpExceptionHandler; | ||
|
||
class RouteProvider extends ServiceProvider | ||
{ | ||
protected $users = []; | ||
protected $namespace = 'App\Controllers'; | ||
/** | ||
* Register a service to the application | ||
* | ||
* @param \Yuga\Interfaces\Application\Application | ||
* | ||
* @return mixed | ||
*/ | ||
public function load(Application $app) | ||
{ | ||
return $app; | ||
} | ||
|
||
public function boot(Route $router) | ||
{ | ||
$_ENV['ROUTER_BOOTED'] = true; | ||
$router->group(['prefix' => 'api', 'middleware' => 'api', 'namespace' => $this->namespace], function () { | ||
require path('routes/api.php'); | ||
}); | ||
|
||
$router->group(['middleware' => 'web', 'namespace' => $this->namespace, 'exceptionHandler' => NotFoundHttpExceptionHandler::class], function () use ($router) { | ||
$router->csrfVerifier(new WebMiddleware); | ||
require path('routes/web.php'); | ||
}); | ||
} | ||
} |
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
This file was deleted.
Oops, something went wrong.
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
Route::get('/', function (Response $response) { | ||
return $response->json([ | ||
'data' => 'api-data', | ||
]); | ||
}); |
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