Skip to content

Commit 4fb6dde

Browse files
Handle duplicate GitHub ID upon registration (#782)
* Added check inside the create method of the RegisterController to find duplicate github ID * Added error message saying "We already found a user with the given GitHub account....... * fixed failing test * fixed stycli errors * styleci fixes * styleci fixes * more styleci fixes * wip Co-authored-by: Dries Vints <[email protected]>
1 parent 9b64c5c commit 4fb6dde

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

app/Http/Requests/RegisterRequest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Requests;
44

5+
use App\Rules\UniqueGitHubUser;
56
use Illuminate\Foundation\Http\FormRequest;
67

78
class RegisterRequest extends FormRequest
@@ -19,7 +20,7 @@ public function rules()
1920
'username' => 'required|alpha_dash|max:40|unique:users',
2021
'rules' => 'accepted',
2122
'terms' => 'accepted',
22-
'github_id' => 'required',
23+
'github_id' => ['required', new UniqueGitHubUser],
2324
];
2425
}
2526

app/Rules/UniqueGitHubUser.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace App\Rules;
4+
5+
use App\Concerns\SendsAlerts;
6+
use App\Models\User;
7+
use Illuminate\Contracts\Validation\Rule;
8+
use Illuminate\Database\Eloquent\ModelNotFoundException;
9+
10+
final class UniqueGitHubUser implements Rule
11+
{
12+
use SendsAlerts;
13+
14+
private User $user;
15+
16+
public function passes($attribute, $value): bool
17+
{
18+
try {
19+
$this->user = User::findByGithubId($value);
20+
} catch (ModelNotFoundException) {
21+
return true;
22+
}
23+
24+
return false;
25+
}
26+
27+
public function message()
28+
{
29+
$this->error('errors.github_account_exists', [
30+
'username' => '@'.$this->user->githubUsername(),
31+
'login' => route('login'),
32+
]);
33+
}
34+
}

resources/lang/en/errors.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
'fields' => 'Something went wrong. Please review the fields below.',
66
'github_invalid_state' => 'The request timed out. Please try again.',
77
'github_account_too_young' => 'Your Github account needs to be older than 2 weeks in order to register.',
8+
'github_account_exists' => 'We already found a user with the given GitHub account (:username). Would you like to <a href=":login">login</a> instead?',
89
];

tests/Feature/AuthTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use App\Models\User;
34
use Carbon\Carbon;
45
use Illuminate\Auth\Events\Registered;
56
use Illuminate\Contracts\Auth\PasswordBroker;
@@ -61,6 +62,24 @@
6162
->see('The username must only contain letters, numbers, dashes and underscores.');
6263
});
6364

65+
test('registration fails with a duplicate github id', function () {
66+
User::factory()->create(['github_id' => 123, 'github_username' => 'johndoe']);
67+
68+
session(['githubData' => ['id' => 123, 'username' => 'johndoe']]);
69+
70+
$this->visit('/register')
71+
->type('John Doe', 'name')
72+
->type('[email protected]', 'email')
73+
->type('johndoe', 'username')
74+
->type('123', 'github_id')
75+
->type('johndoe', 'github_username')
76+
->check('rules')
77+
->check('terms')
78+
->press('Register')
79+
->seePageIs('/register')
80+
->see('We already found a user with the given GitHub account (@johndoe). Would you like to <a href="http://localhost/login">login</a> instead?');
81+
});
82+
6483
test('users can resend the email verification', function () {
6584
$this->login(['email_verified_at' => null]);
6685

0 commit comments

Comments
 (0)