Skip to content

Commit 09deb52

Browse files
committed
Enable Auth:API & Handle Error
1 parent cd0c387 commit 09deb52

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

app/Exceptions/Handler.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exception;
66
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
7+
use Illuminate\Auth\AuthenticationException;
78

89
class Handler extends ExceptionHandler
910
{
@@ -50,4 +51,14 @@ public function render($request, Exception $exception)
5051
{
5152
return parent::render($request, $exception);
5253
}
54+
55+
protected function unauthenticated($request, AuthenticationException $exception)
56+
{
57+
if ($request->expectsJson()) {
58+
return response()->json(['error' => 'Unauthenticated.'], 401);
59+
}
60+
61+
return redirect()->guest(route('login'));
62+
}
63+
5364
}

app/Http/Controllers/API/ItemAPIController.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use App\Http\Controllers\AppBaseController;
1010
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
1111
use Prettus\Repository\Criteria\RequestCriteria;
12+
use Auth;
1213
use Response;
1314

1415
/**
@@ -24,6 +25,11 @@ class ItemAPIController extends AppBaseController
2425

2526
public function __construct(ItemRepository $itemRepo)
2627
{
28+
$this->middleware('auth:api');
29+
// if (! Auth::check()) {
30+
// return $this->sendError('Not Authenticated', 500);
31+
// }
32+
2733
$this->itemRepository = $itemRepo;
2834
}
2935

@@ -34,6 +40,8 @@ public function __construct(ItemRepository $itemRepo)
3440
*/
3541
public function index(Request $request)
3642
{
43+
$user = Auth::user();
44+
3745
$this->itemRepository->pushCriteria(new RequestCriteria($request));
3846
$this->itemRepository->pushCriteria(new LimitOffsetCriteria($request));
3947
$items = $this->itemRepository->all();

routes/api.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,4 @@
1717
return $request->user();
1818
});
1919

20-
21-
Route::resource('items', 'ItemAPIController');
20+
Route::resource('items', 'ItemAPIController');

0 commit comments

Comments
 (0)