File tree Expand file tree Collapse file tree 4 files changed +56
-1
lines changed Expand file tree Collapse file tree 4 files changed +56
-1
lines changed Original file line number Diff line number Diff line change 2
2
3
3
namespace App \Http \Requests ;
4
4
5
+ use App \Rules \UniqueGitHubUser ;
5
6
use Illuminate \Foundation \Http \FormRequest ;
6
7
7
8
class RegisterRequest extends FormRequest
@@ -19,7 +20,7 @@ public function rules()
19
20
'username ' => 'required|alpha_dash|max:40|unique:users ' ,
20
21
'rules ' => 'accepted ' ,
21
22
'terms ' => 'accepted ' ,
22
- 'github_id ' => 'required ' ,
23
+ 'github_id ' => [ 'required ' , new UniqueGitHubUser ] ,
23
24
];
24
25
}
25
26
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 5
5
'fields ' => 'Something went wrong. Please review the fields below. ' ,
6
6
'github_invalid_state ' => 'The request timed out. Please try again. ' ,
7
7
'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? ' ,
8
9
];
Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
+ use App \Models \User ;
3
4
use Carbon \Carbon ;
4
5
use Illuminate \Auth \Events \Registered ;
5
6
use Illuminate \Contracts \Auth \PasswordBroker ;
61
62
->see ('The username must only contain letters, numbers, dashes and underscores. ' );
62
63
});
63
64
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
+
64
83
test ('users can resend the email verification ' , function () {
65
84
$ this ->login (['email_verified_at ' => null ]);
66
85
You can’t perform that action at this time.
0 commit comments