Skip to content

Commit 6a57a20

Browse files
committed
add support for the hints.
1 parent 0919ab7 commit 6a57a20

File tree

2 files changed

+128
-1
lines changed

2 files changed

+128
-1
lines changed

src/Http/Requests/AuthKitLoginRequest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ class AuthKitLoginRequest extends FormRequest
1414
{
1515
/**
1616
* Redirect the user to WorkOS for authentication.
17+
*
18+
* @param array{
19+
* screenHint?: 'sign-in'|'sign-up',
20+
* domainHint?: string,
21+
* loginHint?: string
22+
* } $options
1723
*/
18-
public function redirect(): Response
24+
public function redirect(array $options = []): Response
1925
{
2026
WorkOS::configure();
2127

@@ -26,6 +32,9 @@ public function redirect(): Response
2632
'previous_url' => base64_encode(URL::previous()),
2733
],
2834
'authkit',
35+
domainHint: $options['domainHint'] ?? null,
36+
loginHint: $options['loginHint'] ?? null,
37+
screenHint: $options['screenHint'] ?? null,
2938
);
3039

3140
$this->session()->put('state', json_encode($state));
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Config;
4+
use Illuminate\Support\Facades\Session;
5+
use Laravel\WorkOS\Http\Requests\AuthKitLoginRequest;
6+
use Symfony\Component\HttpFoundation\RedirectResponse;
7+
8+
beforeEach(function () {
9+
Config::set('services.workos.client_id', 'test_client_id');
10+
Config::set('services.workos.secret', 'test_secret');
11+
Config::set('services.workos.redirect_url', 'https://laravel.com/authenticate');
12+
13+
$this->request = AuthKitLoginRequest::create('/', 'GET');
14+
$this->request->setLaravelSession(app('session.store'));
15+
});
16+
17+
it('redirects to WorkOS without screen hint', function () {
18+
$response = $this->request->redirect();
19+
20+
expect($response)->toBeInstanceOf(RedirectResponse::class);
21+
22+
expect($response->headers->get('Location'))->toContain('https://api.workos.com/user_management/authorize')
23+
->toContain('client_id=test_client_id')
24+
->toContain('response_type=code')
25+
->toContain('redirect_uri='.urlencode('https://laravel.com/authenticate'))
26+
->toContain('provider=authkit')
27+
->not->toContain('screen_hint');
28+
});
29+
30+
it('redirects to WorkOS with sign-in screen hint', function () {
31+
$response = $this->request->redirect(['screenHint' => 'sign-in']);
32+
33+
expect($response)->toBeInstanceOf(RedirectResponse::class);
34+
35+
expect($response->headers->get('Location'))
36+
->toContain('https://api.workos.com/user_management/authorize')
37+
->toContain('screen_hint=sign-in');
38+
});
39+
40+
it('redirects to WorkOS with sign-up screen hint', function () {
41+
$response = $this->request->redirect(['screenHint' => 'sign-up']);
42+
43+
expect($response)->toBeInstanceOf(RedirectResponse::class);
44+
45+
expect($response->headers->get('Location'))
46+
->toContain('https://api.workos.com/user_management/authorize')
47+
->toContain('screen_hint=sign-up');
48+
});
49+
50+
it('stores state in session', function () {
51+
$response = $this->request->redirect();
52+
53+
$sessionState = Session::get('state');
54+
expect($sessionState)->not->toBeNull();
55+
56+
$decodedState = json_decode($sessionState, true);
57+
58+
expect($decodedState)
59+
->toHaveKey('state')
60+
->toHaveKey('previous_url')
61+
->and($decodedState['state'])->toHaveLength(20);
62+
});
63+
64+
it('includes state in the authorization URL', function () {
65+
$response = $this->request->redirect();
66+
67+
$location = $response->headers->get('Location');
68+
expect($location)->toContain('state=');
69+
70+
parse_str(parse_url($location, PHP_URL_QUERY), $queryParams);
71+
72+
expect($queryParams)->toHaveKey('state');
73+
74+
$sessionState = json_decode(Session::get('state'), true);
75+
$urlState = json_decode($queryParams['state'], true);
76+
77+
expect($urlState)->toBe($sessionState);
78+
});
79+
80+
it('passes all parameters correctly to getAuthorizationUrl', function () {
81+
$response = $this->request->redirect(['screenHint' => 'sign-up']);
82+
83+
expect($response->headers->get('Location'))->not->toBeNull()
84+
->toBeString()
85+
->toContain('https://api.workos.com/user_management/authorize')
86+
->toContain('client_id=test_client_id')
87+
->toContain('response_type=code')
88+
->toContain('redirect_uri='.urlencode('https://laravel.com/authenticate'))
89+
->toContain('provider=authkit')
90+
->toContain('screen_hint=sign-up')
91+
->toContain('state=');
92+
});
93+
94+
it('supports domain hint parameter', function () {
95+
$response = $this->request->redirect(['domainHint' => 'laravel.com']);
96+
97+
expect($response->headers->get('Location'))
98+
->toContain('domain_hint=laravel.com');
99+
});
100+
101+
it('supports login hint parameter', function () {
102+
$response = $this->request->redirect(['loginHint' => '[email protected]']);
103+
104+
expect($response->headers->get('Location'))
105+
->toContain('login_hint='.urlencode('[email protected]'));
106+
});
107+
108+
it('supports multiple parameters at once', function () {
109+
$response = $this->request->redirect([
110+
'screenHint' => 'sign-in',
111+
'domainHint' => 'laravel.com',
112+
'loginHint' => '[email protected]',
113+
]);
114+
115+
expect($response->headers->get('Location'))->toContain('screen_hint=sign-in')
116+
->toContain('domain_hint=laravel.com')
117+
->toContain('login_hint='.urlencode('[email protected]'));
118+
});

0 commit comments

Comments
 (0)