Skip to content

Commit

Permalink
Extract hashing and new password generation into methods
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed Oct 18, 2024
1 parent f88192c commit 684ce0b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
19 changes: 18 additions & 1 deletion src/UserProviderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Laravel\Socialite\Contracts\User as SocialiteUser;

Expand Down Expand Up @@ -46,7 +47,7 @@ public function updateOrCreate(string $driver, SocialiteUser $user): Authenticat
array_merge([
'name' => $user->name,
'provider_id' => $user->id,
'password' => $eloquent->password ?? bcrypt(Str::random()),
'password' => $eloquent->password ?? $this->hash($this->getNewPassword()),
],
$this->isUsingSoftDeletes($model)
? ['deleted_at' => null]
Expand All @@ -60,6 +61,22 @@ public function updateOrCreate(string $driver, SocialiteUser $user): Authenticat
return $eloquent;
}

/**
* Hash the given value.
*/
protected function hash(string $value): string
{
return Hash::make($value);
}

/**
* Get a new password for the user.
*/
protected function getNewPassword(): string
{
return Str::random();
}

/**
* Get a new user query instance.
*
Expand Down
5 changes: 3 additions & 2 deletions tests/UserProviderRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use DirectoryTree\Bartender\Tests\User;
use DirectoryTree\Bartender\UserProviderRepository;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Hash;
use Laravel\Socialite\Two\User as SocialiteUser;

beforeEach(fn () => Bartender::setUserModel(User::class));
Expand All @@ -14,7 +15,7 @@
'provider_name' => 'foo',
'name' => 'foo',
'email' => '[email protected]',
'password' => bcrypt('password'),
'password' => Hash::make('password'),
]);

$socialite = tap(new SocialiteUser(), function ($user) {
Expand All @@ -31,7 +32,7 @@
'provider_name' => null,
'name' => 'foo',
'email' => '[email protected]',
'password' => bcrypt('password'),
'password' => Hash::make('password'),
]);

$socialite = tap(new SocialiteUser(), function ($user) {
Expand Down

0 comments on commit 684ce0b

Please sign in to comment.