Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel login - After the login is successful, it does not remain and returns to the login page

I tried several ways to login, the one that works is Auth::login($user);, but he doesn't stay logged in, he goes back to the login page.

Login Controller:

public function validateCode(Request $request)
    {
        $email = $request->email;

        // Get user
        $user = User::where('email', $email)->first();

        
        if ($user->count() > 0) {
            $validateCode = $request->input('validateCode');
            
            // Check the codes
            if ($validateCode == $user['codConfirm']) {
                Auth::login($user, true);
                
                if(Auth::check())
                    return redirect()->route('home');
                else{
                    dd('else');
                    return redirect()->action('Auth\LoginController@index', ['validator' => ['Não foi possível fazer o login']]);
                }
            } else {
                return view('confirmar-usuario', ['email' => $email]);
            }
        } 
        else {
            flash('Usuário inválido')->error();

            return view('login');
        }
    }

I will answer my own question as I found out and the question I had thought about has been deleted. So I'm posting here to help someone.

like image 674
Gabriel Edu Avatar asked Sep 20 '25 08:09

Gabriel Edu


1 Answers

It's all right, the issue is only the .env. There are two important pieces of information that must be filled in correctly. 'SESSION_DOMAIN' and 'APP_URL'. The url must be without the protocol and without the port, if any. Ex:

APP_URL=127.0.0.1
SESSION_DOMAIN=127.0.0.1
like image 117
Gabriel Edu Avatar answered Sep 23 '25 05:09

Gabriel Edu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!