[英]Laravel: Custom Auth with 2 tables
我正在使用 Laravel 8,我需要使用 2 個表中的數據驗證用戶:用戶和客戶。 當用戶登錄時,他們將輸入 3 個值:email、密碼和帳戶。 “帳戶”字段來自客戶表。
所以我需要進行連接以從“客戶”表中訪問“帳戶”字段。
我看到的一個選擇是:
public function validateCredentials(UserContract $user, array $credentials) {
$plain = $credentials['password'];
//Custom Query
return $this->hasher->check($plain, $user->getAuthPassword());
}
在“自定義查詢”部分,我可以使用 $user->customer_id 進行查詢以獲取客戶數據並檢查是否匹配 $credentials['account'],但不確定這是否是最好的方法。
在此先感謝您的幫助。
我最終構建了一個自定義提供程序,並覆蓋了“retrieveByCredentials”方法,這就是訣竅。
public function retrieveByCredentials(array $credentials)
{
if (empty($credentials) ||
(count($credentials) === 1 &&
Str::contains($this->firstCredentialKey($credentials), 'password'))) {
return;
}
$query = User::select(['customer.identifier', 'users.*'])->join('customer', 'customer_id', '=', 'customer.id');
foreach ($credentials as $key => $value) {
if (Str::contains($key, 'password')) {
continue;
}
if($key == 'account') {
$query->where('customer.identifier', '=', $credentials['account']);
} else {
$query->where($key, $value);
}
}
return $query->first();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.