jeudi 8 août 2019

Auth::attempt allways returns false even if informations passed are true

i'm on laravel and try to make a user verfication system but i have a probleme with the function Auth::attempt, it returns allways false even if i fix data.

this is my login function code in my controller

 $request->validate([
            'username' => 'string',
            'password' => 'required|string',
        ]);
        $credentials = ['username' =>$request->get('username'), 'password' =>$request->get('password')];

        try {

            if(!Auth::attempt($credentials))
                return response()->json([
                    'message' => 'Unauthorized'
                ], 401);

            $user = $request->user();
            $tokenResult = $user->createToken('Personal Access Token');
            $token = $tokenResult->token;
            if ($request->remember_me)
                $token->expires_at = Carbon::now()->addWeeks(1);
            $token->save();
            return response()->json([
                'access_token' => $tokenResult->accessToken,
                'token_type' => 'Bearer',
                'expires_at' => Carbon::parse(
                    $tokenResult->token->expires_at
                )->toDateTimeString()
            ]);

        } catch(Exception $e){
            echo $e;
        }

this is my customerProvider for checking because i have in my database MD5 password


    public function validateCredentials(UserContract $user, array $credentials)
    {
        $plain = $credentials['password'];

         if(md5($plain) == $user->getAuthPassword()){
            return true;
        } else if($this->hasher->check($plain, $user->getAuthPassword())){
            return true;
        } else {
            return false;
        }
    }

}

my user model

class User extends Authenticatable
{
    use Notifiable, HasApiTokens;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'username', 'password'
    ];

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


}

Help me please to found a solution..



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire