mardi 29 décembre 2015

Laravel 5.2 - Auth::login not preserve when using new \App\User

I've used these code to authenticate username from external source, but laravel 5.2 not save the authentication, and request the external source every time.

class Authenticate
{
    public function handle($request, Closure $next, $guard = null)
        {
            if (Auth::guard($guard)->guest()) {
                if ($request->ajax()) {
                    return response('Unauthorized.', 401);
                } else {
                    $username = getFromExternalSource();
                    if($username==null){ redirect()->guest('auth/login'); }
                    $user = new \App\User(['username'=>'admin']);
                    Auth::login($user);
                }
            }

            return $next($request);
        }
}

But when I change Auth::login using $user model get from database it work, I don't know why:

$userDB = \App\User::where('username','=','admin')->first();
Auth::login($userDB);

My route (for example I want to access http://myApp/api)

Route::group(['middleware' => ['web']], function () {
    Route::group(['middleware' => 'auth'], function () {
        Route::get('api/', 'ApiController@index');
    });
    Route::group(['middleware' => ['csrf']], function () {
       Route::controllers([
           'auth' => 'Auth\AuthController',
           'password' => 'Auth\PasswordController'
       ]);
   });

});



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

Aucun commentaire:

Enregistrer un commentaire