Reputation: 73
I've been trying to get the new release (5.2) of Laravel to work with a simple web app. However, I'm having a problem with authentication.
All pages of the app include a navigation view partial which uses Auth::user()->name to display the username in the nav if they are logged in.
In order to do this, I created a pages controller which loads the auth middleware in the constructor:
public function __construct()
{
$this->middleware('auth');
}
This works perfectly if the user is logged in. However, if the user is not logged in, they are requested to login on every page. Even pages like "contact" or "about" which clearly should not require authentication to view.
How can I make pages like "about" always accessible while still being able to access Auth in the nav?
EDIT:
Routes
Route::group(['middleware' => ['web']], function () {
Route::get('/home', 'StaticController@home');
Route::get('/about', 'StaticController@about');
Route::get('/contact', 'StaticController@contact');
});
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/', 'HomeController@index');
});
StaticController
class StaticController extends Controller
{
public function home()
{
return view('static.home');
}
public function about()
{
return view('static.about');
}
public function contact()
{
return view('static.contact');
}
}
Navigation
<ul class="nav navbar-nav">
@if (Auth::guest())
<li><a href="{{ url('/login') }}">Login</a></li>
<li><a href="{{ url('/register') }}">Register</a></li>
@else
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li><a href="{{ url('/logout') }}"><i class="fa fa-btn fa-sign-out"></i>Logout</a></li>
</ul>
</li>
@endif
</ul>
Upvotes: 1
Views: 340
Reputation: 73
After deleting everything and installing a fresh copy of Laravel the problem disappeared.
Upvotes: 1
Reputation: 577
you can do something like this
$this->middleware('auth', ['only' => 'update'])
the only will be set on the specified method for example
Upvotes: 0
Reputation: 58652
In /app/Http/Kernel.php
check to see if you have
'auth' => \App\Http\Middleware\Authenticate::class,
Example
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
....
];
Now, you may need to re-architect your route to something like this
in your routes.php
**//Routes (Not Require Log-in)**
Route::get('/home', 'StaticController@home');
Route::get('/about', 'StaticController@about');
Route::get('/contact', 'StaticController@contact');
**//Authentication Routes**
Route::group(['middleware' => ['auth']], function () {
Route::get('/', 'HomeController@index');
//................................
// More Auth Routes Go in HERE
//................................
});
Hope it helps !
Upvotes: 0
Reputation:
You have two possible solutions:
Within your view partial you could simply have a conditional statement to check or create a method on the Auth facade. This would mean you wouldn't need the conditional. Conditional solution below:
Auth::check() ? Auth::user()->name : ''
Upvotes: 0