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

- 1 answer

Ad

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

Ad

Answer

Ad

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', '[email protected]');
});
Ad
source: stackoverflow.com
Ad