اذهب إلى المحتوى
  • 0

خطأ في تسجيل الدخول لتطبيق Laravel

Mahmoud Alrashidi

السؤال

أحاول تسجيل الدخول لتطبيق لارافيل لكن بالرغم من إدخال البيانات الصحيحة يظهر لي الخطأ التالي:

These credentials do not match our records

و هذا المُتحكم الخاص بتسجيل الدخول:

<?php
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Notifications\LoginToWebsite as LoginToWebsiteNotification;
use App\Providers\RouteServiceProvider;
use App\Rules\ReCaptcha;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\ActiveCode;


class LoginController extends Controller
{

  use AuthenticatesUsers , TwoFactorAuthenticate;

  /**
         * Where to redirect users after login.
         *
         * @var string
         */
  protected $redirectTo = RouteServiceProvider::HOME;

  /**
         * Create a new controller instance.
         *
         * @return void
         */
  public function __construct()
  {
    $this->middleware('guest')->except('logout');
  }

  protected function authenticated(Request $request, $user)
  {
    $user->notify(new LoginToWebsiteNotification());
    return $this->loggendin($request , $user);
  }

  protected function validateLogin(Request $request)
  {
    $request->validate([
      $this->username() => 'required|string',
      'password' => 'required|string',
      'g-recaptcha-response' => ['required' , new Recaptcha]
    ],[
      'g-recaptcha-response.required' => 'Click to confirm you are not a bot'
    ]);
  }
}

و هذا الكود الخاص بالنموذج User:

<?php

namespace App\Models;

use App\Notifications\ResetPassword as ResetPasswordNotification;
use App\Notifications\VerifyEmail as VerifyEmailNotification;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
  use Notifiable;

  /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
  protected $fillable = [
    'name', 'email', 'password','two_factor_type' , 'phone_number' , 'is_superuser' , 'is_staff'
  ];

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

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

  /**
     * Send the password reset notification.
     *
     * @param  string  $token
     * @return void
     */
  public function sendPasswordResetNotification($token)
  {
    $this->notify(new ResetPasswordNotification($token));
  }

  /**
     * Send the email verification notification.
     *
     * @return void
     */
  public function sendEmailVerificationNotification()
  {
    $this->notify(new VerifyEmailNotification);
  }

  public function setPasswordAttribute($value)
  {
    $this->attributes['password'] = bcrypt($value);
  }

  public function isSuperUser()
  {
    return $this->is_superuser;
  }

  public function isStaffUser()
  {
    return $this->is_staff;
  }

  public function activeCode()
  {
    return $this->hasMany(ActiveCode::class);
  }

  public function hasTwoFactor($key)
  {
    return $this->two_factor_type == $key;
  }

  public function hasTwoFactorAuthenticatedEnabled()
  {
    return $this->two_factor_type !== 'off';
  }

  public function hasSmsTwoFactorAuthenticationEnabled()
  {
    return $this->two_factor_type == 'sms';
  }
}

لم أفهم سبب المُشكلة.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 1

أعتقد أن سبب المُشكلة أنك تقوم بتشفير كلمة المرور مرتين الأولى في المُتحكم RegisterController  و الثانية في الMutator او المُعدل: setPasswordAttribute و لحل المُشكل يُمكنك الإستغناء عن الMutator و إستعمال فقط التشفير في المُتحكم RegisterController :

<?php
protected function create(array $data)
{
  return User::create([
    'name' => $data['name'],
    'email' => $data['email'],
    'password' => Hash::make($data['password']),
  ]);
}

// أو 

protected function create(array $data)
{
  return User::create([
    'name' => $data['name'],
    'email' => $data['email'],
    'password' => bcrypt($data['password']),
  ]);
}

أما إن أردت إستخدام المُعدِل setPasswordAttribute فيُمكنك ذلك لكن يجب عدم تشفير كلمة المرور في المُتحكم RegisterController بهذا الشكل:

'password' => $data['password'],

بدل:

'password' => Hash::make($data['password']),

ثم في المُعدل:

public function setPasswordAttribute($value)
{
  $this->attributes['password'] = bcrypt($value);
}

و بهذا الشكل سيتم تشفير كلمة المرور من جهة واحدة.

ملاحظة: تستخدم Laravel التشفير bcrypt إفتراضياً و تدعم الأنواع التالية: bcrypt, argon, argon2id لذلك إن لم تُغير في الإعدادات يُمكنك إستخدام الدالة المُساعدة bcrypt أما إن قمت بتغيير الإعدادات فيجب إستعمال الكلاس Hash.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

ربما سبب المشكلة بعدم تطابق كلمة السر يعود إلى عدم تشفيرها باستخدام الدالة bcrypt عند إنشاء الحساب و قبل إضافة الكلمة المشفرة لقاعدة البيانات في الدالة Create الخاصة بالنموذج. RegisterController.php الخاص بالمستخدم وفرضا هو User

وتجنب تطبيق التشفير / التجزئة على كلمة المرور أكثر من مرة لأنه يعطي كلمة جديدة ولن يحدث مطابقة عند عمل الاختبار checkPassword

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

لديك طريقتان لتشفير كلمة المرور في Laravel.

$data['password'] = bcrypt($request->password);

و الطريقة الأخرى هي تخزين كلمة المرور غير مشفرة 

$data['password'] = $request->password;

ثم تشفير كلمة المرور في Model 

public function setPasswordAttribute($value)
{
    $this->attributes['password'] = bcrypt($value);
}

إذا قمت بذلك بهذه الطريقة ، فتأكد فقط من عدم تجزئة كلمة المرور مرتين ، لذلك يجب عليك الانتقال إلى RegisterController وبدلاً من:

'password' => Hash::make($data['password']);

تضيف

'password' => $data['password']

لاحظ أيضًا أنه لا يوجد فرق بين فئة Hash والوظيفة المساعدة bcrypt ().

إذا كنت تريد استخدام Hash

Hash::make()

وتحقق من ذلك عن طريق

Hash::check()

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...