Compare commits

..

No commits in common. 'master' and 'feature/api-detail-exercise' have entirely different histories.

  1. 42
      app/Auth/PassportUserRepository.php
  2. 2
      app/Models/User.php

@ -3,22 +3,13 @@
namespace App\Auth; namespace App\Auth;
use Laravel\Passport\Bridge\UserRepository; use Laravel\Passport\Bridge\UserRepository;
use Laravel\Passport\Bridge\UserRepositoryInterface;
use Laravel\Passport\Bridge\User;
use League\OAuth2\Server\Entities\ClientEntityInterface; use League\OAuth2\Server\Entities\ClientEntityInterface;
use Laravel\Passport\Bridge\User;
use Illuminate\Support\Facades\Hash;
use App\Models\User as UserModel; use App\Models\User as UserModel;
class PassportUserRepository extends UserRepository class PassportUserRepository extends UserRepository
{ {
/**
* OAuth2.
*
* @param string $username
* @param string $password
* @param string $grantType
* @param ClientEntityInterface $clientEntity
* @return User|null
*/
public function getUserEntityByUserCredentials( public function getUserEntityByUserCredentials(
$username, $username,
$password, $password,
@ -27,14 +18,37 @@ class PassportUserRepository extends UserRepository
) { ) {
$user = UserModel::where('email', $username)->first(); $user = UserModel::where('email', $username)->first();
if (! $user) { if (!$user) {
return null; return null;
} }
if (md5($password) === $user->password) { // Avoid Hash::check() error with non-bcrypt hashes
return new User($user->getAuthIdentifier()); if ($this->isBcryptHash($user->password)) {
if (Hash::check($password, $user->password)) {
return new User($user->id);
}
} else {
// If the hash is not bcrypt, check for MD5 manually
if (md5($password) === $user->password) {
// Upgrade password to bcrypt
$user->password = Hash::make($password);
$user->save();
return new User($user->id);
}
} }
return null; return null;
} }
/**
* Check if the given hash uses the bcrypt algorithm.
*
* @param string $hashedPassword
* @return bool
*/
protected function isBcryptHash($hashedPassword): bool
{
return password_get_info($hashedPassword)['algo'] === PASSWORD_BCRYPT;
}
} }

@ -15,7 +15,7 @@ class User extends Authenticatable
/** @use HasFactory<\Database\Factories\UserFactory> */ /** @use HasFactory<\Database\Factories\UserFactory> */
use HasApiTokens, HasFactory, Notifiable, HasRoles; use HasApiTokens, HasFactory, Notifiable, HasRoles;
protected $table = 'users'; protected $table = 'users_laravel';
/** /**
* The attributes that are mass assignable. * The attributes that are mass assignable.

Loading…
Cancel
Save