Reputation: 300
when I login with wrong credentials I got the right response. when I login with the right credentials the login page reload with 302 request but it never redirect to statistics page. when I debug it I found that the code goes to this authinticate.php in the middleware folder, it redirect to the guest login state
if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('login');
}
}
see the code:-
Route.php
Route::get('login', 'LoginController@index');
Route::post('signin', 'LoginController@signin');
Route::get('signout', 'LoginController@signout');
Route::group(['prefix' => 'api'], function() {
Route::resource('authenticate', 'AuthenticateController', ['only' => ['index']]);
Route::post('authenticate', 'AuthenticateController@authenticate');
});
Route::group(['middleware' => ['web']], function () {
Route::auth();
Route::get('/', 'StatisticsController@index');
Route::get('/statistics', 'StatisticsController@statistics');
});
Login Controller
public function index() {
return view('login');
}
public function signin(Request $request) {
$errors = [];
$email=$request['email'];
$password= $request['password'];
$credentials = array('email' => $email, 'password' => $password);
if(Auth::attempt($credentials))
{
return redirect('/statistics');
}
return "bad request";
}
public function signout()
{
Auth::logout();
return redirect('/login'); }
}
Statistics Controller
class StatisticsController extends Controller {
public function __construct()
{
$this->middleware('auth');
}
public function index() {
return view('statistics')->with($data);
}
public function statistics() {
return view('statistics');
}
}
Kernal.php note that there is JWT auth library I use it for restful authentication with the mobile app only.
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
];
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
'api' => [
'throttle:60,1',
],
];
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'jwt.auth' => 'Tymon\JWTAuth\Middleware\GetUserFromToken',
'jwt.refresh' => 'Tymon\JWTAuth\Middleware\RefreshToken'
];
middleware/authenticate.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class Authenticate
{
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);
}
}
Upvotes: 8
Views: 1795
Reputation: 521
Check your cache.I had a similar problem, where I lost a couple of hours, so these where some of the steps I've made:
Upvotes: 3
Reputation: 7371
You are redirecting to StatisticsController@statistics but there is no statistics function defined in your StatisticsController.
Upvotes: 1
Reputation: 396
Quick Analysis:
There's no problem with your Authentication method, or your controllers.
The problem lies with the fact that you don't have a route for "/statistics"
And with Laravel at-least starting version 5, you have to be explicit about your routes "PS: they deprecated Route::Controller()"
By the way
Route::get('/', 'StatisticsController@index');
Refers to your application base route
Solution Add the statistics route
Route::get('/statistics', 'StatisticsController@statistics');
For example.
Upvotes: 1