|
5 | 5 | namespace App\Http\Controllers;
|
6 | 6 |
|
7 | 7 | use App\Jobs\SyncVerifiedUser;
|
8 |
| -use App\Jobs\UpdateUserAvatar; |
9 | 8 | use App\Models\User;
|
| 9 | +use App\Services\GitHub; |
10 | 10 | use Illuminate\Http\RedirectResponse;
|
11 | 11 | use Illuminate\Http\Request;
|
12 |
| -use Illuminate\Support\Facades\Validator; |
13 |
| -use Illuminate\Validation\ValidationException; |
14 | 12 | use Laravel\Socialite\Facades\Socialite;
|
| 13 | +use Laravel\Socialite\Two\GithubProvider; |
15 | 14 |
|
16 | 15 | final readonly class UserGitHubUsernameController
|
17 | 16 | {
|
|
20 | 19 | */
|
21 | 20 | public function index(): RedirectResponse
|
22 | 21 | {
|
23 |
| - return type(Socialite::driver('github')->redirect())->as(RedirectResponse::class); |
| 22 | + /** @var GithubProvider $driver */ |
| 23 | + $driver = Socialite::driver('github'); |
| 24 | + |
| 25 | + return $driver->redirectUrl(route('profile.connect.github.update'))->redirect(); |
24 | 26 | }
|
25 | 27 |
|
26 | 28 | /**
|
27 | 29 | * Handles the GitHub connection update.
|
28 | 30 | */
|
29 |
| - public function update(Request $request): RedirectResponse |
| 31 | + public function update(Request $request, GitHub $github): RedirectResponse |
30 | 32 | {
|
31 | 33 | $githubUser = Socialite::driver('github')->user();
|
32 | 34 |
|
33 | 35 | $user = type($request->user())->as(User::class);
|
34 | 36 |
|
35 |
| - try { |
36 |
| - $validated = Validator::validate([ |
37 |
| - 'github_username' => $githubUser->getNickname(), |
38 |
| - ], [ |
39 |
| - 'github_username' => ['required', 'string', 'max:255', 'unique:users,github_username'], |
40 |
| - ], [ |
41 |
| - 'github_username.unique' => 'This GitHub username is already connected to another account.', |
42 |
| - ]); |
43 |
| - } catch (ValidationException $e) { |
44 |
| - if ($githubUser->getNickname() === $user->github_username) { |
45 |
| - session()->flash('flash-message', 'The same GitHub account has been connected.'); |
46 |
| - |
47 |
| - return to_route('profile.edit'); |
48 |
| - } |
49 |
| - |
50 |
| - return to_route('profile.edit')->withErrors($e->errors(), 'verified'); |
51 |
| - } |
| 37 | + $githubUsername = $githubUser->getNickname(); |
52 | 38 |
|
53 |
| - $user->update($validated); |
| 39 | + $errors = $github->linkGitHubUser($githubUsername, $user, $request); |
54 | 40 |
|
55 |
| - SyncVerifiedUser::dispatchSync($user); |
56 |
| - |
57 |
| - $user = type($user->fresh())->as(User::class); |
58 |
| - |
59 |
| - $user->is_verified |
60 |
| - ? session()->flash('flash-message', 'Your GitHub account has been connected and you are now verified.') |
61 |
| - : session()->flash('flash-message', 'Your GitHub account has been connected.'); |
| 41 | + if ($errors !== []) { |
| 42 | + if ($githubUsername === $user->github_username) { |
| 43 | + session()->flash('flash-message', 'The same GitHub account has been connected.'); |
| 44 | + } |
62 | 45 |
|
63 |
| - if (! $user->is_uploaded_avatar) { |
64 |
| - UpdateUserAvatar::dispatch( |
65 |
| - user: $user, |
66 |
| - service: 'github', |
67 |
| - ); |
| 46 | + return to_route('profile.edit')->withErrors($errors, 'verified'); |
68 | 47 | }
|
69 | 48 |
|
70 | 49 | return to_route('profile.edit');
|
|
0 commit comments