From 0d043ff880cd3fb00e1d187ad4b8a45ea0c1a2b9 Mon Sep 17 00:00:00 2001 From: Refringe Date: Mon, 30 Sep 2024 16:43:31 -0400 Subject: [PATCH] Username Handling - When a new user is created using Discord OAuth information, if the username returned from Discord is already taken, append randomness to the end of the username. - Validates that a new account name is unique. - Validates that an updated account name is unique. --- app/Actions/Fortify/CreateNewUser.php | 2 +- app/Actions/Fortify/UpdateUserProfileInformation.php | 2 +- app/Http/Controllers/SocialiteController.php | 12 ++++++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/app/Actions/Fortify/CreateNewUser.php b/app/Actions/Fortify/CreateNewUser.php index 566e51d..057f25c 100644 --- a/app/Actions/Fortify/CreateNewUser.php +++ b/app/Actions/Fortify/CreateNewUser.php @@ -20,7 +20,7 @@ class CreateNewUser implements CreatesNewUsers public function create(array $input): User { Validator::make($input, [ - 'name' => ['required', 'string', 'max:255'], + 'name' => ['required', 'string', 'max:36', 'unique:users'], 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], 'password' => $this->passwordRules(), 'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['accepted', 'required'] : '', diff --git a/app/Actions/Fortify/UpdateUserProfileInformation.php b/app/Actions/Fortify/UpdateUserProfileInformation.php index 170c9ce..845647b 100644 --- a/app/Actions/Fortify/UpdateUserProfileInformation.php +++ b/app/Actions/Fortify/UpdateUserProfileInformation.php @@ -16,7 +16,7 @@ class UpdateUserProfileInformation implements UpdatesUserProfileInformation public function update(User $user, array $input): void { Validator::make($input, [ - 'name' => ['required', 'string', 'max:255'], + 'name' => ['required', 'string', 'max:255', Rule::unique('users')->ignore($user->id)], 'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($user->id)], 'photo' => ['nullable', 'mimes:jpg,jpeg,png', 'max:1024'], 'cover' => ['nullable', 'mimes:jpg,jpeg,png', 'max:2048'], diff --git a/app/Http/Controllers/SocialiteController.php b/app/Http/Controllers/SocialiteController.php index 29488fd..474929a 100644 --- a/app/Http/Controllers/SocialiteController.php +++ b/app/Http/Controllers/SocialiteController.php @@ -78,14 +78,22 @@ class SocialiteController extends Controller return $oauthConnection->user; } + // If the username already exists in the database, append a random string to it to ensure uniqueness. + $username = $providerUser->getName() ?? $providerUser->getNickname(); + $random = ''; + while (User::whereName($username.$random)->exists()) { + $random = '-'.Str::random(5); + } + $username .= $random; + // The user has not connected their account with this OAuth provider before, so a new connection needs to be // established. Check if the user has an account with the same email address that's passed in from the provider. // If one exists, connect that account. Otherwise, create a new one. - return DB::transaction(function () use ($providerUser, $provider) { + return DB::transaction(function () use ($providerUser, $provider, $username) { $user = User::firstOrCreate(['email' => $providerUser->getEmail()], [ - 'name' => $providerUser->getName() ?? $providerUser->getNickname(), + 'name' => $username, 'password' => null, ]);