Skip to content

Commit ddfbf0a

Browse files
authored
[6.x] fix: custom oauth prompt callback registration (#398)
1 parent 38f4724 commit ddfbf0a

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*.php diff=php
88

99
/.github export-ignore
10+
/resources/views/oauth/test-prompt.blade.php export-ignore
1011
/workbench export-ignore
1112
/tests export-ignore
1213
.editorconfig export-ignore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<body class="font-sans antialiased">
4+
Confirm Your {{ $provider }} OAuth Request (Test)
5+
</body>
6+
</html>

src/Http/Controllers/OAuthController.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,7 @@ public function callback(Request $request, string $provider): SocialstreamRespon
7474
*/
7575
public function prompt(string $provider): View|InertiaResponse
7676
{
77-
if (Socialstream::$oAuthConfirmationPrompt) {
78-
return app(Socialstream::$oAuthConfirmationPrompt)($provider);
79-
}
80-
81-
return view('socialstream::oauth.prompt', [
82-
'provider' => $provider,
83-
]);
77+
return app()->call(Socialstream::getOAuthConfirmationPrompt(), ['provider' => $provider]);
8478
}
8579

8680
public function confirm(string $provider): SocialstreamResponse|RedirectResponse

src/Socialstream.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,17 @@ public static function refreshConnectedAccountToken(ConnectedAccount $connectedA
405405
*
406406
* @param ?(callable(string): (Response|View)) $callback
407407
*/
408-
public static function promptOAuthLinkUsing(?callable $callback): void
408+
public static function promptOAuthLinkUsing(?Closure $callback = null): void
409409
{
410-
self::$oAuthConfirmationPrompt = $callback ? $callback(...) : null;
410+
self::$oAuthConfirmationPrompt = $callback;
411+
}
412+
413+
public static function getOAuthConfirmationPrompt(): Closure
414+
{
415+
return self::$oAuthConfirmationPrompt ?? function (string $provider): View {
416+
return view('socialstream::oauth.prompt', [
417+
'provider' => $provider,
418+
]);
419+
};
411420
}
412421
}

tests/Feature/SocialstreamTest.php

+18-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Models\User;
66
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use Illuminate\Support\Facades\Blade;
78
use Illuminate\Support\Facades\Cache;
89
use Illuminate\Support\Facades\Config;
910
use Illuminate\Support\Facades\DB;
@@ -305,8 +306,23 @@ public function generate(string $provider): RedirectResponse
305306
'password' => Hash::make('password'),
306307
]));
307308

308-
get('http://localhost/oauth/github/callback/prompt')
309-
->assertSee('Confirm connection of your GitHub account.');
309+
expect(get('http://localhost/oauth/github/callback/prompt'))
310+
->getStatusCode()->toBe(200)
311+
->getContent()->toContain('Confirm connection of your GitHub account.');
312+
});
313+
314+
it('can render a custom prompt', function () {
315+
Socialstream::promptOAuthLinkUsing(fn (string $provider) => view('socialstream::oauth.test-prompt', compact('provider')));
316+
317+
$this->actingAs(User::create([
318+
'name' => 'Joel Butcher',
319+
'email' => '[email protected]',
320+
'password' => Hash::make('password'),
321+
]));
322+
323+
expect(get('http://localhost/oauth/github/callback/prompt'))
324+
->getStatusCode()->toBe(200)
325+
->getContent()->toContain('Confirm Your github OAuth Request (Test)');
310326
});
311327

312328
it('denies an attempt to link an account', function () {

0 commit comments

Comments
 (0)