忘记了Laravel中的Auth会话

时间:2018-08-14 19:21:03

标签: laravel

我正在尝试Auth尝试登录Laravel。并成功登录。但不记得Auth会话。

我的kernel.php文件:

<?php

     namespace App\Http;

     use Illuminate\Foundation\Http\Kernel as HttpKernel;

     class Kernel extends HttpKernel
     {
        /**
       * The application's global HTTP middleware stack.
 *
 * These middleware are run during every request to your application.
 *
 * @var array
 */
protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
    \App\Http\Middleware\TrimStrings::class,
    \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    \App\Http\Middleware\TrustProxies::class,
];

/**
 * The 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\Session\Middleware\AuthenticateSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],

    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];

/**
 * The application's route middleware.
 *
 * These middleware may be assigned to 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,
    'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];

}

Auth.php文件为:

<?php

返回[

/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/

'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],

/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],
],

/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],

    // 'users' => [
    //     'driver' => 'database',
    //     'table' => 'users',
    // ],
],

/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| You may specify multiple password reset configurations if you have more
| than one user table or model in the application and you want to have
| separate password reset settings based on the specific user types.
|
| The expire time is the number of minutes that the reset token should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/

'passwords' => [
    'users' => [
        'provider' => 'users',
        'table' => 'password_resets',
        'expire' => 60,
    ],
],

];

HomeController.php Code is :

 public function login(Request $request){        
    if($request->isMethod('post')){
        $data = $request->all(); 
        $any = array('email'=>$data['email'],'password'=>$data['password']);
        if(auth()->attempt($any)){
            dd("logged"); //This code is running when i try to login
                          //but after when i try to get Auth::check() then it return false
        }
    }
}

我的web.php文件:

<?php
    Route::group(['middleware'=>'web'],function(){
        Route::match(['get','post'],'/', 'HomeController@index');
        Route::match(['get','post'],'/login', 'HomeController@login');
        Route::group(['middleware'=>'auth'],function(){    
      });
   });
?>

2 个答案:

答案 0 :(得分:0)

您是否尝试通过调用以下方式对用户进行身份验证:

Auth::attempt($user_credentails);

代替auth()

答案 1 :(得分:0)

我解决了这个问题。让我们检查一下我的HomeController代码

public function login(Request $request){        
if($request->isMethod('post')){
    $data = $request->all(); 
    $any = array('email'=>$data['email'],'password'=>$data['password']);
    if(auth()->attempt($any)){
        dd("logged"); //This code is running when i try to login
                      //but after when i try to get Auth::check() then it return false
    }
}

}

当身份验证尝试成功时,我将使用dd("Logged");停止代码,如果我停止了代码,则身份验证尝试尚未完全完成。我删除了dd("Logged");,然后重定向到了信息中心。

        if(auth()->attempt($any)){
           return Redirect('/dashboard');
        }

然后我用Auth::check()检查auth并返回true。谢谢你们发表宝贵的评论和答案。对此,我真的非常感激。