3

Laravel 5.2 Authentication

I created a new authentication scaffolding in Laravel 5.2 using

php artisan make:auth

everything worked perfectly except I get Login/Register links even after logging in when I'm in route

/

but it shows the user's name with a logout link (which is what I want to have in every page) when I'm in route

/home

How can I show logged in user's name and the logout link in every page?

What I don't want when logged in

What I want when logged-in in every page

1 Answer 1

10

This is because, you are not using the auth middleware on you / route. In your routes.php file the default for this is:

Route::get('/', function () {
    return view('welcome');
});

Try moving this closure into the web middleware that was added in when you generated the scaffolding. Your routes.php file should look something like this when complete:

<?php

/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

// Route::get('/', function () {
//     return view('welcome');
// });

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/

Route::group(['middleware' => ['web']], function () {
    //
});

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

    // Add this!
    Route::get('/', function () {
        return view('welcome');
    });

    Route::get('/home', 'HomeController@index');
});
Sign up to request clarification or add additional context in comments.

1 Comment

It works, and I see the importance of doing it this way so that I can exclude routes that doesn't have to deal with authentication :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.