I have a simple form in my site and I want to check if the user is currently logged in before he can post the form. I have put auth middleware in the post route...
Route::post('test','SellController@test_mid')->middleware('auth');
My auth middleware...
<?php
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('login');
}
}
return $next($request);
}
Here 'login' redirects to my login page.
The problem is after I hit the login button, the error is...
MethodNotAllowedHttpException in RouteCollection.php line 219: at RouteCollection->methodNotAllowed(array('POST')) in RouteCollection.php line 206
Form...
{{ Form::open(array('url'=>"/test")) }}=
<input type="submit" value="submit">
{{ Form::close() }}
But if I set the auth middleware on a GET route, it works fine. In case of a GET, if I am logged out, it leads me to the login page. After I provide login credentials and hit the login button it leads me to the desired GET route.