البرمجة

تجاهل نطاق عالمي في المصادقة في Laravel

To achieve the desired functionality in Laravel authentication without altering the main Laravel code, you can use a custom query builder in your AuthController. This allows you to ignore the global scope “active” when querying the user for authentication. Here’s how you can modify your postLogin method:

php
use Illuminate\Support\Facades\Hash; use App\Models\User; use Illuminate\Database\Eloquent\Builder; public function postLogin(Request $request) { $user = User::query() ->withoutGlobalScope('active') ->where('username', $request->username) ->first(); if ($user !== null && Hash::check($request->password, $user->password)) { $user->update(['active' => 1]); // Activate the user return $this->login($request); // Proceed with regular login } // Handle invalid credentials return redirect()->back()->withErrors(['username' => 'Invalid username or password']); }

In this code, we use User::query() to create a new query builder instance and then call withoutGlobalScope('active') to ignore the global scope when querying for the user. If the user is found and the password is correct, we update the “active” column to 1 to activate the user before proceeding with the regular login process.

This approach allows you to achieve the desired functionality without modifying the main Laravel code.

المزيد من المعلومات

لتحقيق الوظائف المطلوبة في المصادقة بدون نطاق عالمي “active” في Laravel ، يمكنك استخدام موجه استعلام مخصص في AuthController الخاص بك. يسمح لك هذا بتجاهل النطاق العالمي “active” عند استعلام المستخدم للمصادقة. فيما يلي كيف يمكنك تعديل طريقة postLogin الخاصة بك:

استخدم Illuminate\Support\Facades\Hash;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;

public function postLogin(Request $request)
{
$user = User::query()
->withoutGlobalScope(‘active’)
->where(‘username’, $request->username)
->first();

if ($user !== null && Hash::check($request->password, $user->password)) {
$user->update([‘active’ => 1]); // Activate the user
return $this->login($request); // Proceed with regular login
}

// Handle invalid credentials
return redirect()->back()->withErrors([‘username’ => ‘Invalid username or password’]);
}
تستخدم في هذا الرمز User::query() لإنشاء مثيل لبناء استعلام جديد ثم استدعاء withoutGlobalScope(‘active’) لتجاهل النطاق العالمي عند الاستعلام عن المستخدم. إذا تم العثور على المستخدم وكانت كلمة المرور صحيحة ، فإننا نقوم بتحديث العمود “active” إلى 1 لتنشيط المستخدم قبل المتابعة مع عملية تسجيل الدخول العادية.

تسمح لك هذه الطريقة بتحقيق الوظيفة المطلوبة دون تعديل الشيفرة الرئيسية لـ Laravel.

مقالات ذات صلة

زر الذهاب إلى الأعلى
إغلاق

أنت تستخدم إضافة Adblock

يرجى تعطيل مانع الإعلانات حيث أن موقعنا غير مزعج ولا بأس من عرض الأعلانات لك فهي تعتبر كمصدر دخل لنا و دعم مقدم منك لنا لنستمر في تقديم المحتوى المناسب و المفيد لك فلا تبخل بدعمنا عزيزي الزائر