Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

wizjo's avatar
Level 26

Session loging out

Im using in my controllers code like below to show messages in views:

Session::flash('message', 'Success message...');
return Redirect::back();    

The problem is that recently messages doesn`t appear. I found that the reason was that I commented out the below line in my app/Http/Kernel.php:

    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            //\Illuminate\Session\Middleware\AuthenticateSession::class, //<- THIS LINE
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            // \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];

When I remove my comment, session messages start`s working again, but appears new problem: after reload page I am always logged out from application.

What can be the problem?

Here is my routes/web.php file:

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

//Authentication Routes
Route::get('/', 'Frontend\PageController@home')->name('login');
Route::post('/', 'Auth\LoginController@authenticate');
Route::post('wyloguj', 'Auth\LoginController@logout')->name('logout');

//Registration Routes
Route::get('rejestracja', 'Auth\RegisterController@showRegistrationForm')->name('register');
Route::post('rejestracja', 'Auth\RegisterController@register');
Route::get('rejestracja/potwierdz/{confirmationCode}', 'Auth\RegisterController@confirm');

//Password Reset Routes
Route::get('przypomnij-haslo', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
Route::get('przypomnij-haslo/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
Route::post('przypomnij-haslo', 'Auth\ResetPasswordController@reset');

//OAuth Routes
Route::get('auth/{driver}', ['as' => 'socialAuth', 'uses' => 'Auth\SocialController@redirectToProvider']);
Route::get('auth/{driver}/callback', ['as' => 'socialAuthCallback', 'uses' => 'Auth\SocialController@handleProviderCallback']);

//--- UNLOGGED ---
Route::group(['middleware' => ['web', 'isActive']], function () {
    Route::get('wspolpraca-ze-sprzedawcami', 'Frontend\PageController@cooperationSellers');
    Route::get('wspolpraca-z-wykonawcami', 'Frontend\PageController@cooperationContractors');
    
    Route::get('s/{slug}', 'Frontend\PageController@show');
    
    
    //TODO - odtad w dol przeniesc do powyzszej grupy- dla zalogowanych
    // Route::get('edycja-danych', 'Frontend\InvestorController@edit');
    // Route::post('edycja-danych', 'Frontend\InvestorController@update');
    
    // Route::get('pliki/{investment}', 'Frontend\FileController@index');
    // Route::post('pliki/{investment}/store', 'Frontend\FileController@store'); //store file
    // Route::post('pliki/{investment}', 'Frontend\FileController@destroy'); //delete files
    
    // Route::get('wiadomosci/{investment}', 'Frontend\MessageController@showLoginForm'); //TODO
    //odebrane
    //wyslane
    //archiwalne
    //wybrana wiadomosc 
    
    //TODO - odtad w gore przeniesc do powyzszej grupy- dla zalogowanych
});

//--- USER LOGGED IN ---
Route::group(['middleware' => ['web', 'auth', 'isActive']], function () {
    Route::get('inwestycje/dodaj', 'Frontend\InvestmentController@create');
    Route::get('inwestycje/{id}', 'Frontend\InvestmentController@edit');
    Route::patch('inwestycje/{id}', 'Frontend\InvestmentController@update');
    Route::delete('inwestycje/{id}', 'Frontend\InvestmentController@destroy');
    Route::post('inwestycje', 'Frontend\InvestmentController@store');
    
    Route::get('pliki/{id?}', 'Frontend\FileController@index');
    Route::post('pliki', 'Frontend\FileController@store');
    Route::get('pliki/pobierz/{id}', 'Frontend\FileController@download');       
    Route::post('pliki/usun', 'Frontend\FileController@destroy');       
});

//--- ADMIN PANEL ---
Route::group(['middleware' => ['web', 'auth', 'mustBeAdmin', 'isActive'], 'prefix' => 'admin'], function () {
    Route::resource('admins', 'Backend\AdminController');
    Route::resource('templates', 'Backend\TemplateController');
    Route::resource('materials', 'Backend\MaterialController');
    
    Route::get('stages/json', 'Backend\StageController@json');
    Route::resource('stages', 'Backend\StageController');

    Route::get('investors/search', 'Backend\InvestorController@search');
    Route::resource('investors', 'Backend\InvestorController');
    
    Route::resource('pages', 'Backend\PageController');
    
    Route::get('companies/search', 'Backend\CompanyController@search');
    Route::resource('companies', 'Backend\CompanyController');
});

Route::get('/cc', function() {
    $exitCode = Artisan::call('cache:clear');
    dd('Wyczyszczono cache');
})->middleware('auth.basic');
0 likes
4 replies
wizjo's avatar
Level 26

@rin4ik:

I do it this way, but after a few refreshes I'm still logged out.

Here is my authenticate method:

    public function authenticate(Request $request)
    {
        $user = User::where('email', $request->email)->first();
        
        if (!$user) {
            Session::flash('error', 'Podano nieprawidłowe dane logowania.');
            return Redirect::to('/')->withInput();          
        }
        
        if (!$user->active) {
            if (!$user->confirmation_code) {
                Session::flash('error', 'Twoje konto zostało zawieszone i do czasu odwieszenia nie możesz zalogować się do systemu.');              
            } else {
                Session::flash('error', 'Twoje konto nie zostało jeszcze aktywowane. Kliknij w link aktywacyjny, który wysłaliśmy na Twój adres e-mail.');
            }           
        }       
        
        if (Auth::attempt(['email' => $request->email, 'password' => $request->password, 'active' => 1], true)) {
            if ($user->type == 'admin') {
                return Redirect::to('admin/admins');
            }
            
            return Redirect::intended();
        }

        Session::flash('error', 'Podano nieprawidłowe dane logowania.');
        return Redirect::to('/')->withInput();              
    }

What else could be the problem?

jlrdw's avatar

a few refreshes

What is a few refreshes? And why?

Please or to participate in this conversation.