php - Laravel's 5.3 authentication not working -
in laravel 5.3 authentication, i'm using middleware verify if user logged in access page...
the problem i'm facing que middleware redirect login page, when did log in...
my routes:
route::get('/', 'produtocontroller@lista'); route::get('/produtos', 'produtocontroller@lista'); route::get('/produtos/mostra/{id}', 'produtocontroller@mostra'); route::get('/produtos/novo', 'produtocontroller@novo'); route::post('/produtos/adiciona', 'produtocontroller@adiciona'); route::get('produtos/lista-json', 'produtocontroller@listajson'); route::get('/produtos/remove/{id}', 'produtocontroller@remove'); auth::routes(); route::get('/home', 'homecontroller@index'); route::get('/login', 'logincontroller@form'); route::post('/login', 'logincontroller@login');
my logincontroller
class logincontroller extends controller { public function form () { return view('form_login'); } public function login () { // credenciais (email e senha) $credenciais = request::only('email', 'password'); // login if (auth::attempt($credenciais)) { // caso credenciais sejam aceitas, retorna true return 'usuário ' . auth::user()->name . ' logado com sucesso'; } else { return 'usuário não existe'; } } }
my produtocontroller
class produtocontroller extends controller { public function __construct() { $this->middleware('autorizador'); } public function lista() { $produtos = produto::all(); return view('produto/listagem')->with('produtos', $produtos); } }
my kernel:
class kernel extends httpkernel { /** * application's global http middleware stack. * * these middleware run during every request application. * * @var array */ protected $middleware = [ \illuminate\foundation\http\middleware\checkformaintenancemode::class, \app\http\middleware\autorizador::class, ]; /** * application's route middleware groups. * * @var array */ 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, \illuminate\routing\middleware\substitutebindings::class, ], 'api' => [ 'throttle:60,1', 'bindings', ], ]; /** * application's route middleware. * * these middleware may assigned groups or used individually. * * @var array */ protected $routemiddleware = [ 'auth' => \illuminate\auth\middleware\authenticate::class, 'auth.basic' => \illuminate\auth\middleware\authenticatewithbasicauth::class, 'bindings' => \illuminate\routing\middleware\substitutebindings::class, 'can' => \illuminate\auth\middleware\authorize::class, 'guest' => \app\http\middleware\redirectifauthenticated::class, 'throttle' => \illuminate\routing\middleware\throttlerequests::class, ]; }
my middleware:
class autorizador { /** * handle incoming request. * * @param \illuminate\http\request $request * @param \closure $next * @return mixed */ public function handle($request, closure $next) // next é o código que será executado em seguida { if (!$request->is('login') && auth::guest()) { return redirect('/login'); } return $next($request); } }
when log in, receive message telling me log in successfull , infoming me name of logged user, then, when try access list of products, redirected login form again...
any suggestions?
i suggest in kernel.php
add middleware
in $routemiddleware
instead of $middleware
as:
kernel.php
class kernel extends httpkernel { /** * application's global http middleware stack. * * these middleware run during every request application. * * @var array */ protected $middleware = [ \illuminate\foundation\http\middleware\checkformaintenancemode::class, ]; /** * application's route middleware groups. * * @var array */ 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, \illuminate\routing\middleware\substitutebindings::class, ], 'api' => [ 'throttle:60,1', 'bindings', ], ]; /** * application's route middleware. * * these middleware may assigned groups or used individually. * * @var array */ protected $routemiddleware = [ 'auth' => \illuminate\auth\middleware\authenticate::class, 'auth.basic' => \illuminate\auth\middleware\authenticatewithbasicauth::class, 'bindings' => \illuminate\routing\middleware\substitutebindings::class, 'can' => \illuminate\auth\middleware\authorize::class, 'guest' => \app\http\middleware\redirectifauthenticated::class, 'throttle' => \illuminate\routing\middleware\throttlerequests::class, 'autorizador' => \app\http\middleware\autorizador::class, ]; }
and in produtocontroller.php
use as:
public function __construct() { $this->middleware('autorizador'); }
Comments
Post a Comment