samedi 5 décembre 2020

Can't post to route '/'

Running Laravel 5.4 (though also happens with 7.28), I have a login page at route '/' so a GET request to that route loads the login form and a POST request should attempt to authenticate it. Unfortunately Laravel seems to see the POST requests as GET requests (I've confirmed by dump()ing the request variables) and so reloads the blank login page upon form submission.

If I change the route (URL) to '/login' (or anything else) it works fine, so it's a specific problem for '/'.

Login form (login.blade.php) has:

<form id="loginform" class="contact form-horizontal" name="loginform" method="post" action="">
...
    <button type="submit" class="btn btn-lg btn-primary">Log In</button>
</form>

web.php:

Route::get('/', 'MainController@login')->name('login');
Route::post('/', 'MainController@authenticate');

MainController.php:

// Show login page
public function login() { return view('login', array('page' => 'login', 'error' => rand())); }

// Authenticate login:
public function authenticate(Request $request)
        {
        // Attempt a login with the email and password provided:
        if(Auth::attempt(['email' => $request->input('email'), 'password' => $request->input('password')]))
            {
            Log::info('LOGGED IN');
            return redirect()->route('main'); // Show main page
            }
        else return view('login', array('page' => 'login', 'error' => 'Sorry, wrong email or password, please try again')); // Login failed, show login form again
        }

My overall objective is to have the site present the login page at the domain name, so user doesn't have to add '/login' at the end just to login.

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire