Skip to content

Commit

Permalink
Set auth middleware depending on Authorization header
Browse files Browse the repository at this point in the history
  • Loading branch information
oschettler committed May 24, 2018
1 parent 372f36a commit da18821
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/Http/Controllers/ApiController.php
Expand Up @@ -8,6 +8,11 @@

class ApiController extends Controller
{
public function __construct(Request $request)
{
$this->setAuthMiddleware($request);
}

public function show(Concept $concept, Request $request)
{
$this->authorize('view', $concept);
Expand Down
11 changes: 11 additions & 0 deletions app/Http/Controllers/Controller.php
Expand Up @@ -19,6 +19,7 @@
namespace Knowfox\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
Expand All @@ -37,4 +38,14 @@ protected function fromToken($token)

return $email_login->user;
}

protected function setAuthMiddleWare(Request $request)
{
if ($request->hasHeader('authorization')) {
$this->middleware('auth:api');
}
else {
$this->middleware('web');
}
}
}
5 changes: 5 additions & 0 deletions app/Http/Controllers/UserController.php
Expand Up @@ -25,6 +25,11 @@

class UserController extends Controller
{
public function __construct(Request $request)
{
$this->setAuthMiddleware($request);
}

/**
* Display a listing of the resource.
*
Expand Down
6 changes: 6 additions & 0 deletions public/ui/styles.css
Expand Up @@ -6,6 +6,8 @@ body {
}

#toolbar {
display: flex;
justify-content: space-between;
position: fixed;
width: 100%;
top: 0;
Expand All @@ -15,6 +17,10 @@ body {
color: #FFF;
}

#toolbar > div:last-child {
margin-right: 18px;
}

main {
padding-top: 30px;
display: flex;
Expand Down
11 changes: 10 additions & 1 deletion resources/views/ui.blade.php
Expand Up @@ -7,7 +7,16 @@
<link rel="stylesheet" href="/ui/styles.css">
</head>
<body>
<nav id="toolbar">toolbar</nav>
<nav id="toolbar">
<div>
left
</div>
<div>
@if (Auth::check())
{{ Auth::user()->name }}
@endif
</div>
</nav>
<main>
<section id="tree"></section>
<section id="editor">
Expand Down
2 changes: 1 addition & 1 deletion routes/api.php
Expand Up @@ -13,7 +13,7 @@
|
*/

Route::group(['middleware' => 'auth:api'], function () {
Route::group([], function () {

Route::get('user', function (Request $request) {
return $request->user();
Expand Down
2 changes: 2 additions & 0 deletions routes/web.php
Expand Up @@ -97,6 +97,8 @@
'uses' => 'ConceptController@store',
]);

// Route::resource('api/concept', 'ApiController');

Route::get('/{concept}', function ($concept) {
return redirect()->route('concept.show', [$concept]);
})->where('concept', '[0-9]+')->name('concept.short');
Expand Down

0 comments on commit da18821

Please sign in to comment.