|
2 | 2 |
|
3 | 3 | namespace JoelButcher\Socialstream\Http\Controllers;
|
4 | 4 |
|
| 5 | +use Illuminate\Contracts\View\View; |
5 | 6 | use Illuminate\Http\RedirectResponse;
|
6 | 7 | use Illuminate\Http\Request;
|
7 | 8 | use Illuminate\Http\Response;
|
8 | 9 | use Illuminate\Routing\Controller;
|
| 10 | +use Illuminate\Support\Facades\Session; |
| 11 | +use Illuminate\Support\MessageBag; |
| 12 | +use Illuminate\Support\ViewErrorBag; |
9 | 13 | use JoelButcher\Socialstream\Contracts\AuthenticatesOAuthCallback;
|
10 | 14 | use JoelButcher\Socialstream\Contracts\GeneratesProviderRedirect;
|
11 | 15 | use JoelButcher\Socialstream\Contracts\HandlesInvalidState;
|
12 | 16 | use JoelButcher\Socialstream\Contracts\HandlesOAuthCallbackErrors;
|
| 17 | +use JoelButcher\Socialstream\Contracts\OAuthProviderLinkFailedResponse; |
13 | 18 | use JoelButcher\Socialstream\Contracts\ResolvesSocialiteUsers;
|
14 | 19 | use JoelButcher\Socialstream\Contracts\SocialstreamResponse;
|
| 20 | +use JoelButcher\Socialstream\Events\OAuthProviderLinkFailed; |
| 21 | +use JoelButcher\Socialstream\Providers; |
| 22 | +use Laravel\Jetstream\Jetstream; |
15 | 23 | use Laravel\Socialite\Two\InvalidStateException;
|
16 | 24 | use Symfony\Component\HttpFoundation\RedirectResponse as SymfonyRedirectResponse;
|
17 | 25 |
|
@@ -58,4 +66,57 @@ public function callback(Request $request, string $provider): SocialstreamRespon
|
58 | 66 |
|
59 | 67 | return $this->authenticator->authenticate($provider, $providerAccount);
|
60 | 68 | }
|
| 69 | + |
| 70 | + /** |
| 71 | + * Show the oauth confirmation page. |
| 72 | + */ |
| 73 | + public function prompt(string $provider): View |
| 74 | + { |
| 75 | + return view('socialstream::oauth.prompt', [ |
| 76 | + 'provider' => $provider, |
| 77 | + ]); |
| 78 | + } |
| 79 | + |
| 80 | + public function confirm(string $provider): SocialstreamResponse|RedirectResponse |
| 81 | + { |
| 82 | + $user = auth()->user(); |
| 83 | + $providerAccount = cache()->pull("socialstream.{$user->id}:$provider.provider"); |
| 84 | + |
| 85 | + $result = request()->input('result'); |
| 86 | + |
| 87 | + if ($result === 'deny') { |
| 88 | + event(new OAuthProviderLinkFailed($user, $provider, null, $providerAccount)); |
| 89 | + |
| 90 | + $this->flashError( |
| 91 | + __('Failed to link :provider account. User denied the request.', ['provider' => Providers::name($provider)]), |
| 92 | + ); |
| 93 | + |
| 94 | + return app(OAuthProviderLinkFailedResponse::class); |
| 95 | + } |
| 96 | + |
| 97 | + if (!$providerAccount) { |
| 98 | + throw new \DomainException( |
| 99 | + message: 'Could not retrieve social provider information.' |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + return $this->authenticator->linkProvider($user, $provider, $providerAccount); |
| 104 | + } |
| 105 | + |
| 106 | + private function flashError(string $error): void |
| 107 | + { |
| 108 | + if (auth()->check()) { |
| 109 | + if (class_exists(Jetstream::class)) { |
| 110 | + Session::flash('flash.banner', $error); |
| 111 | + Session::flash('flash.bannerStyle', 'danger'); |
| 112 | + |
| 113 | + return; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + Session::flash('errors', (new ViewErrorBag())->put( |
| 118 | + 'default', |
| 119 | + new MessageBag(['socialstream' => $error]) |
| 120 | + )); |
| 121 | + } |
61 | 122 | }
|
0 commit comments