-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
427942a
commit 54b5631
Showing
41 changed files
with
1,224 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace App\Actions\Fortify; | ||
|
||
use App\Models\User; | ||
use Illuminate\Support\Facades\Hash; | ||
use Illuminate\Support\Facades\Validator; | ||
use Illuminate\Validation\Rule; | ||
use Laravel\Fortify\Contracts\CreatesNewUsers; | ||
|
||
class CreateNewUser implements CreatesNewUsers | ||
{ | ||
use PasswordValidationRules; | ||
|
||
/** | ||
* Validate and create a newly registered user. | ||
* | ||
* @param array<string, string> $input | ||
*/ | ||
public function create(array $input): User | ||
{ | ||
Validator::make($input, [ | ||
'name' => ['required', 'string', 'max:255'], | ||
'email' => [ | ||
'required', | ||
'string', | ||
'email', | ||
'max:255', | ||
Rule::unique(User::class), | ||
], | ||
'password' => $this->passwordRules(), | ||
])->validate(); | ||
|
||
return User::create([ | ||
'name' => $input['name'], | ||
'email' => $input['email'], | ||
'password' => Hash::make($input['password']), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace App\Actions\Fortify; | ||
|
||
use Illuminate\Validation\Rules\Password; | ||
|
||
trait PasswordValidationRules | ||
{ | ||
/** | ||
* Get the validation rules used to validate passwords. | ||
* | ||
* @return array<int, \Illuminate\Contracts\Validation\Rule|array<mixed>|string> | ||
*/ | ||
protected function passwordRules(): array | ||
{ | ||
return ['required', 'string', Password::default(), 'confirmed']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace App\Actions\Fortify; | ||
|
||
use App\Models\User; | ||
use Illuminate\Support\Facades\Hash; | ||
use Illuminate\Support\Facades\Validator; | ||
use Laravel\Fortify\Contracts\ResetsUserPasswords; | ||
|
||
class ResetUserPassword implements ResetsUserPasswords | ||
{ | ||
use PasswordValidationRules; | ||
|
||
/** | ||
* Validate and reset the user's forgotten password. | ||
* | ||
* @param array<string, string> $input | ||
*/ | ||
public function reset(User $user, array $input): void | ||
{ | ||
Validator::make($input, [ | ||
'password' => $this->passwordRules(), | ||
])->validate(); | ||
|
||
$user->forceFill([ | ||
'password' => Hash::make($input['password']), | ||
])->save(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace App\Actions\Fortify; | ||
|
||
use App\Models\User; | ||
use Illuminate\Support\Facades\Hash; | ||
use Illuminate\Support\Facades\Validator; | ||
use Laravel\Fortify\Contracts\UpdatesUserPasswords; | ||
|
||
class UpdateUserPassword implements UpdatesUserPasswords | ||
{ | ||
use PasswordValidationRules; | ||
|
||
/** | ||
* Validate and update the user's password. | ||
* | ||
* @param array<string, string> $input | ||
*/ | ||
public function update(User $user, array $input): void | ||
{ | ||
Validator::make($input, [ | ||
'current_password' => ['required', 'string', 'current_password:web'], | ||
'password' => $this->passwordRules(), | ||
], [ | ||
'current_password.current_password' => __('The provided password does not match your current password.'), | ||
])->validateWithBag('updatePassword'); | ||
|
||
$user->forceFill([ | ||
'password' => Hash::make($input['password']), | ||
])->save(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace App\Actions\Fortify; | ||
|
||
use App\Models\User; | ||
use Illuminate\Contracts\Auth\MustVerifyEmail; | ||
use Illuminate\Support\Facades\Validator; | ||
use Illuminate\Validation\Rule; | ||
use Laravel\Fortify\Contracts\UpdatesUserProfileInformation; | ||
|
||
class UpdateUserProfileInformation implements UpdatesUserProfileInformation | ||
{ | ||
/** | ||
* Validate and update the given user's profile information. | ||
* | ||
* @param array<string, string> $input | ||
*/ | ||
public function update(User $user, array $input): void | ||
{ | ||
Validator::make($input, [ | ||
'name' => ['required', 'string', 'max:255'], | ||
|
||
'email' => [ | ||
'required', | ||
'string', | ||
'email', | ||
'max:255', | ||
Rule::unique('users')->ignore($user->id), | ||
], | ||
])->validateWithBag('updateProfileInformation'); | ||
|
||
if ($input['email'] !== $user->email && | ||
$user instanceof MustVerifyEmail) { | ||
$this->updateVerifiedUser($user, $input); | ||
} else { | ||
$user->forceFill([ | ||
'name' => $input['name'], | ||
'email' => $input['email'], | ||
])->save(); | ||
} | ||
} | ||
|
||
/** | ||
* Update the given verified user's profile information. | ||
* | ||
* @param array<string, string> $input | ||
*/ | ||
protected function updateVerifiedUser(User $user, array $input): void | ||
{ | ||
$user->forceFill([ | ||
'name' => $input['name'], | ||
'email' => $input['email'], | ||
'email_verified_at' => null, | ||
])->save(); | ||
|
||
$user->sendEmailVerificationNotification(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace App\Actions\Jetstream; | ||
|
||
use Laravel\Jetstream\Contracts\DeletesUsers; | ||
|
||
class DeleteUser implements DeletesUsers | ||
{ | ||
/** | ||
* Delete the given user. | ||
* | ||
* @param mixed $user | ||
* @return void | ||
*/ | ||
public function delete($user) | ||
{ | ||
$user->deleteProfilePhoto(); | ||
$user->tokens->each->delete(); | ||
$user->connectedAccounts->each->delete(); | ||
$user->delete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace App\Actions\Socialstream; | ||
|
||
use JoelButcher\Socialstream\ConnectedAccount; | ||
use JoelButcher\Socialstream\Contracts\CreatesConnectedAccounts; | ||
use JoelButcher\Socialstream\Socialstream; | ||
use Laravel\Socialite\Contracts\User as ProviderUser; | ||
|
||
class CreateConnectedAccount implements CreatesConnectedAccounts | ||
{ | ||
/** | ||
* Create a connected account for a given user. | ||
*/ | ||
public function create(mixed $user, string $provider, ProviderUser $providerUser): ConnectedAccount | ||
{ | ||
return Socialstream::connectedAccountModel()::forceCreate([ | ||
'user_id' => $user->id, | ||
'provider' => strtolower($provider), | ||
'provider_id' => $providerUser->getId(), | ||
'name' => $providerUser->getName(), | ||
'nickname' => $providerUser->getNickname(), | ||
'email' => $providerUser->getEmail(), | ||
'avatar_path' => $providerUser->getAvatar(), | ||
'token' => $providerUser->token, | ||
'secret' => $providerUser->tokenSecret ?? null, | ||
'refresh_token' => $providerUser->refreshToken ?? null, | ||
'expires_at' => property_exists($providerUser, 'expiresIn') ? now()->addSeconds($providerUser->expiresIn) : null, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace App\Actions\Socialstream; | ||
|
||
use App\Models\User; | ||
use Illuminate\Support\Facades\DB; | ||
use JoelButcher\Socialstream\Contracts\CreatesConnectedAccounts; | ||
use JoelButcher\Socialstream\Contracts\CreatesUserFromProvider; | ||
use JoelButcher\Socialstream\Socialstream; | ||
use Laravel\Socialite\Contracts\User as ProviderUser; | ||
|
||
class CreateUserFromProvider implements CreatesUserFromProvider | ||
{ | ||
/** | ||
* The creates connected accounts instance. | ||
*/ | ||
public CreatesConnectedAccounts $createsConnectedAccounts; | ||
|
||
/** | ||
* Create a new action instance. | ||
*/ | ||
public function __construct(CreatesConnectedAccounts $createsConnectedAccounts) | ||
{ | ||
$this->createsConnectedAccounts = $createsConnectedAccounts; | ||
} | ||
|
||
/** | ||
* Create a new user from a social provider user. | ||
*/ | ||
public function create(string $provider, ProviderUser $providerUser): User | ||
{ | ||
return DB::transaction(function () use ($provider, $providerUser) { | ||
return tap(User::create([ | ||
'name' => $providerUser->getName() ?? $providerUser->getNickname(), | ||
'email' => $providerUser->getEmail(), | ||
]), function (User $user) use ($provider, $providerUser) { | ||
$user->markEmailAsVerified(); | ||
|
||
if (Socialstream::hasProviderAvatarsFeature() && $providerUser->getAvatar()) { | ||
$user->setProfilePhotoFromUrl($providerUser->getAvatar()); | ||
} | ||
|
||
$this->createsConnectedAccounts->create($user, $provider, $providerUser); | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace App\Actions\Socialstream; | ||
|
||
use JoelButcher\Socialstream\Contracts\GeneratesProviderRedirect; | ||
use Laravel\Socialite\Facades\Socialite; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
|
||
class GenerateRedirectForProvider implements GeneratesProviderRedirect | ||
{ | ||
/** | ||
* Generates the redirect for a given provider. | ||
*/ | ||
public function generate(string $provider): RedirectResponse | ||
{ | ||
return Socialite::driver($provider)->redirect(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace App\Actions\Socialstream; | ||
|
||
use Illuminate\Http\Response; | ||
use JoelButcher\Socialstream\Contracts\HandlesInvalidState; | ||
use Laravel\Socialite\Two\InvalidStateException; | ||
|
||
class HandleInvalidState implements HandlesInvalidState | ||
{ | ||
/** | ||
* Handle an invalid state exception from a Socialite provider. | ||
*/ | ||
public function handle(InvalidStateException $exception): Response | ||
{ | ||
throw $exception; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace App\Actions\Socialstream; | ||
|
||
use JoelButcher\Socialstream\Contracts\ResolvesSocialiteUsers; | ||
use JoelButcher\Socialstream\Socialstream; | ||
use Laravel\Socialite\Contracts\User; | ||
use Laravel\Socialite\Facades\Socialite; | ||
|
||
class ResolveSocialiteUser implements ResolvesSocialiteUsers | ||
{ | ||
/** | ||
* Resolve the user for a given provider. | ||
*/ | ||
public function resolve(string $provider): User | ||
{ | ||
$user = Socialite::driver($provider)->user(); | ||
|
||
if (Socialstream::generatesMissingEmails()) { | ||
$user->email = $user->getEmail() ?? ("{$user->id}@{$provider}".config('app.domain')); | ||
} | ||
|
||
return $user; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace App\Actions\Socialstream; | ||
|
||
use Illuminate\Support\Facades\Hash; | ||
use Illuminate\Support\Facades\Validator; | ||
use Illuminate\Validation\Rules\Password; | ||
use JoelButcher\Socialstream\Contracts\SetsUserPasswords; | ||
|
||
class SetUserPassword implements SetsUserPasswords | ||
{ | ||
/** | ||
* Validate and update the user's password. | ||
*/ | ||
public function set(mixed $user, array $input): void | ||
{ | ||
Validator::make($input, [ | ||
'password' => ['required', 'string', Password::default(), 'confirmed'], | ||
])->validateWithBag('setPassword'); | ||
|
||
$user->forceFill([ | ||
'password' => Hash::make($input['password']), | ||
])->save(); | ||
} | ||
} |
Oops, something went wrong.