【问题标题】:Auth::attempt passing but failing middleware('auth'); and auth()->user() is nullAuth::尝试通过但失败的中间件('auth');并且 auth()->user() 为空
【发布时间】:2020-07-12 18:11:43
【问题描述】:

我的中间件没有通过,我的 auth()->user() 为空,但尝试通过

这是我的模型,我没有使用 users 表,我使用的是 customer 而不是那个。此外,我的列名对于密码和电子邮件也是不同的。

<?php

namespace App\Users;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class Customer extends Authenticatable
{
    use Notifiable;

    protected $table = 'customer';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'FirstName', 'LastName', 'EmailAddress', 'PasswordSalt', 'DateOfBirth', 'AccountNumber', 'AddressLine1',
        'AddressLatitude', 'AddressLongitude', 'AdressLine2', 'City', 'Country', 'What3WordsString', 'PrimaryPhoneNumber',
        'AlternatePhoneNumber', 'CompanyName', 'AccountTypeId', 'RouteId', 'IdentificationType', 'CreditLimit', 'IsSuspended',
        'SuspensionReason', 'HasInsurance', 'AcceptCompanyCheques', 'IsCreditCardCustomer', 'IsOnlinePurchaseTaxExempted',
        'IsEnabled', 'IsApproved', 'NeedsVerification', 'IsAccountFlagged', 'AccountFlaggedReason', 'AccountFlaggedDate',
        'FlaggedBy', 'RewardPoints', 'RewardCardNumber', 'CreationSource', 'LastActive', 'LastUpdateSource'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'PasswordSalt',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function getAuthPassword()
    {
        return $this->PasswordSalt;
    }
}

LoginController:这个pass但是当get into account失败,因为它没有通过中间件。 (不知道为什么)

   public function checkLogin(LoginRequest $request)
    {
        $userData = [
            'EmailAddress' => $request->get('log'),
            'password' => $request->get('pwd'),
        ];
        if(Auth::attempt($userData)) {

            return redirect()->action('AccountController@get');
        }
        return back()->with('error', 'Wrong Login Details');
    }



Account Controller:

 public function get()
   {
       $userId = auth()->user()->id;
       $account = $this->getAccountQuery($userId);
       $accountData = $this->formatAccount($account, $userId);
       return  view('account')->with($accountData);
   }




Auth.php

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | 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' => 'customer',
    ],

    /*
    |--------------------------------------------------------------------------
    | 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' => 'customer',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'customer',
            'hash' => false,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | 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' => [
        'customer' => [
            'driver' => 'eloquent',
            'model' => App\Users\Customer::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' => [
        'customer' => [
            'provider' => 'customer',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Password Confirmation Timeout
    |--------------------------------------------------------------------------
    |
    | Here you may define the amount of seconds before a password confirmation
    | times out and the user is prompted to re-enter their password via the
    | confirmation screen. By default, the timeout lasts for three hours.
    |
    */

    'password_timeout' => 10800,

];

我的路线::

/* |------------------------------------------------- ------------------------- |网络路由 |------------------------------------------------- ------------------------- | |您可以在此处为您的应用程序注册网络路由。这些 |路由由 RouteServiceProvider 在一个组内加载 |包含“web”中间件组。现在创造一些伟大的东西! | */

Route::get('/', function () {
    return view('index.welcome');
});
Route::get("'\'", function () {
    return view('index.welcome');
});
Route::get('home', function () {
    return view('index.welcome');
});
Route::get('home#sc_tab_1', function () {
    return view('index.welcome');
});
Route::get('home#user-popUp', function () {
    return view('index.welcome');
})->name('login');
Route::post('log', 'Auth\LoginController@checkLogin');
Route::get('logOut', 'Auth\LoginController@logout')->middleware('auth');
Route::get('home#calculator', function () {
    return view('index.welcome');
});
Route::get('contacts', function () {
    return view('contacts');
});
Route::get('signUp', function () {
    return view('signUp');
});
Route::get('ourServices', function () {
    return view('services.ourServices');
});
Route::get('air', function () {
    return view('services.air');
});
Route::get('ocean', function () {
    return view('services.ocean');
});
Route::get('shopper', function () {
    return view('services.shopper');
});
Route::get('delivery', function () {
    return view('services.delivery');
});
Route::get('customs', function () {
    return view('customs');
});
Route::get('restricted', function () {
    return view('restricted');
});
Route::get('insurance', function () {
    return view('insurance');
});
Route::get('rates', function () {
    return view('rates');
});
Route::get('faqs', function () {
    return view('faqs');
});
Route::get('terms', function () {
    return view('terms');
});
Route::get('privacy', function () {
    return view('privacy');
});
Route::post('register', 'Auth\RegisterController@create');
Route::post('newsletter', 'NewsletterController@insert');
Route::get('registerConfirmation', function () {
    return view('registerConfirmation');
});
Route::get('account', 'AccountController@get')->middleware('auth');
Route::post('account', 'AccountController@update')->middleware('auth');
Route::post('authUser', 'ContactController@update')->middleware('auth');
Route::get('prealert', function () {
    return view('prealert');
})->middleware('auth');
Route::post('prealert','PreAlert\PreAlertController@insert')->middleware('auth');
Route::get('packages', 'Packages\PackageController@get')->middleware('auth');
Route::get('invoice', 'Invoices\InvoiceController@get')->middleware('auth');
Route::get('checkOut', function () {
    return view('checkout');
})->middleware('auth');
Route::get('checkOut2', function () {
    return view('checkout2');
})->middleware('auth');
Route::get('checkOut3', function () {
    return view('checkout3');
})->middleware('auth');

【问题讨论】:

    标签: php laravel middleware laravel-6 laravel-6.2


    【解决方案1】:

    用户密码是否存储在名为“PasswordSalt”的 db 列中?如果不是就是“密码”?

    我会看看 getAuthPassword() 方法。也许当您在 Customer 模型中覆盖它时,您无法对用户进行身份验证。

    【讨论】:

    • 我将 PasswordSalt 存储在我的数据库中。这是我的 getAuthPassword 方法: public function getAuthPassword() { return $this->PasswordSalt; }
    • 我推测您正在覆盖父类中的方法,这可能是一个问题。
    • 所以,我不应该那样做吗?正确的做法是什么?
    • 您的数据库中是否也只有一个密码列?还是只是 PasswordSalt?
    • 不,我只有 PasswordSalt
    猜你喜欢
    • 1970-01-01
    • 2017-04-30
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 2021-07-29
    • 2016-04-02
    • 2017-03-29
    相关资源
    最近更新 更多