samedi 3 juin 2017

Laravel Middleware not working in laravel 5.4

I have following code

<?php

namespace App\Http\Middleware;

use Closure;
use Session;

class CheckLogin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

     if(!Session::has('userAuthDetail')){
     // return redirect('login');
         // return redirect()->guest('/login');
        } else {
return $next($request);     
    }
    }
}

in web.php

Route::get('/login', ['as' => 'login', 'uses' => 'Auth\LoginController@login']);

Route::group(['middleware' => ['login']], function () {
Route::post('/add','MyController@submitData');
}

Login Controller

 public function postLogin(Request $request) {

       //    print_r($request->all());

           $data['email']=$request->email;
           $data['password']=$request->password;
        $response=  ApiModel::userLogin($data);


        if($response->msg=='success'){
         session(['userAuthDetail' => $response->data[0]]);
               return redirect('/');
        } else if($response->msg=='failed'){
               return redirect('/');
        }


    }

if i add if else condition in middleware then website throwing error

ErrorException in VerifyCsrfToken.php line 156: Trying to get property of non-object

if i remove if else and add just

public function handle($request, Closure $next) {

 if(!Session::has('userAuthDetail')){

    } 

return $next($request);

}

then working fine but if credentials failed then its redirecting to dashboard.

Note: i cant use laravel auth since i am communicating with the api which is written in core php



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2soRJsS
via IFTTT

Aucun commentaire:

Enregistrer un commentaire