Reputation: 488
I am a new to Laravel. I'm trying to write a login example but it shows "302 Found" error.
web.php ( Route file ) :
Route::post('login', array(
'uses' => 'UserController@doLogin'
));
Route::get('logout', array('uses' => 'UserController@doLogout'));
Route::group(['middleware' => 'AuthMiddleWare'], function () {
Route::get('/form', function () {
return view('form');
});
Route::get('/createUser', function () {
return view('users');
});
Route::get('logout', array(
'uses' => 'UserController@doLogout'
));
});
"doLogin" function codes which located in UserController:
public function doLogin()
{
$rules = array(
'name' => 'required', // make sure the email is an actual email
'password' => 'required|alphaNum|min:3' // password can only be alphanumeric and has to be greater than 3 characters
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('login')
->withErrors($validator) // send back all errors to the login form
->withInput(Input::except('password')); // send back the input (not the password) so that we can repopulate the form
}
else {
// create our user data for the authentication
$userdata = array(
'name' => Input::get('name'),
'password' => Input::get('password')
);
$auth = DB::table('users')->where('name', '=', Input::get('name'))->where('password', '=', Input::get('password'))->get()->first();
// attempt to do the login
if (Auth::attempt($userdata)) {
// validation successful!
// redirect them to the secure section or whatever
// return Redirect::to('secure');
// for now we'll just echo success (even though echoing in a controller is bad)
echo 'SUCCESS!';
} else {
// validation not successful, send back to form
return Redirect::to('login');
}
}
}
login form:
<html>
<head>
<title>LOGIN</title>
</head>
<body>
<form action="{{ route('login') }}" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="text" name="name"><br/><br/>
<input type="text" name="surname"><br/><br/>
<input type="submit" value="LOGIN">
</form>
</body>
</html>
When I look to the login request from Network status code of request appearing "302 Found".When I trying to use $auth instead of "Auth::attempt($userdata)" still it shows same error.Maybe it related with another reason. How I can find out what's wrong ? Thanks in advance.
Upvotes: 0
Views: 10962
Reputation: 1
Check also and make sure the middle ware you have set @the function _construct is auth and not guest
Upvotes: 0
Reputation: 449
The possible cause of this error is that login credentials are not sent through request (Use chrome network inspector to confirm it is sent), or that db table is missing field for that credential email/password or whatever. In both cases Laravel is redirecting with code 302.
To debug deeper @dump($errors) inside twig and in paralel turn on logging and monitor logs.
Upvotes: 0
Reputation: 15971
You are getting this error because you are using route()
helper function which picks the routes by name and you dont have any name associated with your /login
route.
You can achieve this by two ways.
either you have to change the
Route::post('login', array(
'as' => 'login',
'uses' => 'UserController@doLogin'
));
OR
action="{{ url('login') }}"
Hope this helps
Upvotes: 0