22
33namespace App \Http \Controllers \Auth ;
44
5+ use App \Actions \ConnectGitHubAccount ;
56use App \Http \Controllers \Controller ;
67use App \Jobs \UpdateProfile ;
78use App \Models \User ;
@@ -28,7 +29,7 @@ public function redirectToProvider()
2829 /**
2930 * Obtain the user information from GitHub.
3031 */
31- public function handleProviderCallback ()
32+ public function handleProviderCallback (ConnectGitHubAccount $ connectGitHubAccount )
3233 {
3334 try {
3435 $ socialiteUser = $ this ->getSocialiteUser ();
@@ -42,6 +43,26 @@ public function handleProviderCallback()
4243 return $ socialiteUser ;
4344 }
4445
46+ $ isConnectingAttempt = session ()->pull ('settings.github.connect.intended ' , false );
47+ if ($ isConnectingAttempt ) {
48+ $ currentUser = auth ()->user ();
49+ $ githubId = $ socialiteUser ->getId ();
50+
51+ // Check if the GitHub account is already connected to another user
52+ $ existingUser = User::where ('github_id ' , $ githubId )->where ('id ' , '!= ' , $ currentUser ->id )->first ();
53+ if ($ existingUser ) {
54+ $ this ->error ('This GitHub account is already connected to another user. ' );
55+
56+ return redirect (route ('settings.profile ' ));
57+ }
58+
59+ $ connectGitHubAccount ($ currentUser , $ socialiteUser );
60+
61+ $ this ->success ('Your GitHub account has been connected. ' );
62+
63+ return redirect (route ('settings.profile ' ));
64+ }
65+
4566 try {
4667 $ user = User::findByGitHubId ($ socialiteUser ->getId ());
4768 } catch (ModelNotFoundException $ exception ) {
0 commit comments