0
votes

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="{{url('/')}}">
...
    <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

EDIT: Checking the browser POST reveals the actual problem, Laravel is responding with a 301 redirect from http://localhost/pp/public so the browser immediately does a GET request to http://localhost/pp/public/ (note / on the end). Hmm, how to avoid this?

1
You should really be using Laravel's built-in authentication, with the authentication middleware handling a redirection "so user doesn't have to add '/login' at the end just to login." - miken32
For your problem though, make sure your caches (route, view, config) are all cleared. How are you determining that "Laravel seems to see the POST requests as GET requests"? Have you confirmed your browser is sending a post? - miken32
@miken32 thanks for the suggestion, just edited post above, seems that's the real problem. - Alex Kerr
Your posted code makes no mention of pp/public anywhere. What does your form's HTML look like? Does the action attribute contain the expected value? Using named routes is generally better than relying on URLs, which can change. - miken32
Sorry, the rendered post URL in the login page is: action="localhost/pp/public" - Alex Kerr

1 Answers

1
votes

If you want to redirect your users to login from home page, one way would be directly at web.php:

Route::get('/', function () {
    return redirect('/login');
});

One other option would be to change the action of the form to another route, leading to that controller.

So at web.php you would have:

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

and the form tag would have to change the action

<form id="loginform" class="contact form-horizontal" name="loginform" method="post" action="{{route('loginAction')}}">

Just to be sure, try the command artisan route:list to check all routes.