Skip to content

Commit

Permalink
Missing Jetstream/Socialstream code
Browse files Browse the repository at this point in the history
  • Loading branch information
curtisdelicata committed Jun 13, 2024
1 parent be11996 commit af317c3
Showing 41 changed files with 1,224 additions and 1 deletion.
40 changes: 40 additions & 0 deletions app/Actions/Fortify/CreateNewUser.php
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']),
]);
}
}
18 changes: 18 additions & 0 deletions app/Actions/Fortify/PasswordValidationRules.php
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'];
}
}
29 changes: 29 additions & 0 deletions app/Actions/Fortify/ResetUserPassword.php
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();
}
}
32 changes: 32 additions & 0 deletions app/Actions/Fortify/UpdateUserPassword.php
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();
}
}
58 changes: 58 additions & 0 deletions app/Actions/Fortify/UpdateUserProfileInformation.php
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();
}
}
22 changes: 22 additions & 0 deletions app/Actions/Jetstream/DeleteUser.php
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();
}
}
31 changes: 31 additions & 0 deletions app/Actions/Socialstream/CreateConnectedAccount.php
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,
]);
}
}
47 changes: 47 additions & 0 deletions app/Actions/Socialstream/CreateUserFromProvider.php
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);
});
});
}
}
18 changes: 18 additions & 0 deletions app/Actions/Socialstream/GenerateRedirectForProvider.php
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();
}
}
18 changes: 18 additions & 0 deletions app/Actions/Socialstream/HandleInvalidState.php
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;
}
}
25 changes: 25 additions & 0 deletions app/Actions/Socialstream/ResolveSocialiteUser.php
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;
}
}
25 changes: 25 additions & 0 deletions app/Actions/Socialstream/SetUserPassword.php
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();
}
}
Loading

0 comments on commit af317c3

Please sign in to comment.