diff --git a/app/Http/Requests/RegisterRequest.php b/app/Http/Requests/RegisterRequest.php index ad09dd436..c776caca3 100644 --- a/app/Http/Requests/RegisterRequest.php +++ b/app/Http/Requests/RegisterRequest.php @@ -2,6 +2,7 @@ namespace App\Http\Requests; +use App\Rules\UniqueGitHubUser; use Illuminate\Foundation\Http\FormRequest; class RegisterRequest extends FormRequest @@ -19,7 +20,7 @@ public function rules() 'username' => 'required|alpha_dash|max:40|unique:users', 'rules' => 'accepted', 'terms' => 'accepted', - 'github_id' => 'required', + 'github_id' => ['required', new UniqueGitHubUser], ]; } diff --git a/app/Rules/UniqueGitHubUser.php b/app/Rules/UniqueGitHubUser.php new file mode 100644 index 000000000..e326d2d65 --- /dev/null +++ b/app/Rules/UniqueGitHubUser.php @@ -0,0 +1,34 @@ +user = User::findByGithubId($value); + } catch (ModelNotFoundException) { + return true; + } + + return false; + } + + public function message() + { + $this->error('errors.github_account_exists', [ + 'username' => '@'.$this->user->githubUsername(), + 'login' => route('login'), + ]); + } +} diff --git a/resources/lang/en/errors.php b/resources/lang/en/errors.php index 6d2dc3c01..55c8844e8 100644 --- a/resources/lang/en/errors.php +++ b/resources/lang/en/errors.php @@ -5,4 +5,5 @@ 'fields' => 'Something went wrong. Please review the fields below.', 'github_invalid_state' => 'The request timed out. Please try again.', 'github_account_too_young' => 'Your Github account needs to be older than 2 weeks in order to register.', + 'github_account_exists' => 'We already found a user with the given GitHub account (:username). Would you like to login instead?', ]; diff --git a/tests/Feature/AuthTest.php b/tests/Feature/AuthTest.php index 44dc9a1dd..d70e5296d 100644 --- a/tests/Feature/AuthTest.php +++ b/tests/Feature/AuthTest.php @@ -1,5 +1,6 @@ see('The username must only contain letters, numbers, dashes and underscores.'); }); +test('registration fails with a duplicate github id', function () { + User::factory()->create(['github_id' => 123, 'github_username' => 'johndoe']); + + session(['githubData' => ['id' => 123, 'username' => 'johndoe']]); + + $this->visit('/register') + ->type('John Doe', 'name') + ->type('john.doe@example.com', 'email') + ->type('johndoe', 'username') + ->type('123', 'github_id') + ->type('johndoe', 'github_username') + ->check('rules') + ->check('terms') + ->press('Register') + ->seePageIs('/register') + ->see('We already found a user with the given GitHub account (@johndoe). Would you like to login instead?'); +}); + test('users can resend the email verification', function () { $this->login(['email_verified_at' => null]);