Session variables missing after fail logging in.

Posted 8 months ago by silverxjohn

So, I have this middleware called CheckOneUser it has this code.

public function handle($request, Closure $next)
    {
        $response = $next($request);

        return $response;
    }

    public function terminate($request, $response)
    {
        if (! \Auth::check())
            return;

        $user = \Auth::user();

        if (!$user->session_id) {
            $user->session_id = \Session::getId();
            $user->last_activity = Carbon::now();
            $user->save();
        }
        else {
            $last_session = \Session::getHandler()->read($user->session_id);

            if ($last_session) {
                \Auth::logout();
                \Session::getHandler()->destroy(\Session::getId());
                \Session::flash('session', 'Someone already logged in.');
                return redirect()->route('login', ['session' => 'Someone already logged in.']);
            }
        }
    }

I want to happen is that when the user happens to login then someone is already logged in. I will return a message to my view. My view has this line where I can check if it has session.

                        {{session()->has('session') ? session()->get('session') : 'sadsa'}}

And my web.php looks like this.

Route::group(['middleware' => ['checkoneuser']], function () {
    Route::post('login', 'Auth\[email protected]');

    Route::get('login', 'Auth\[email protected]')->name('login');
});

I registered that middleware to my Kernel. It is working as intended user can't logged in but I can't seem to pass that session variable so that I can send it to my view.

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.