You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+56-4Lines changed: 56 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -157,9 +157,15 @@ return [
157
157
After installing Laravel Breeze or your preferred UI scaffolding, you'll need to replace the login form's login step. Instead of authenticating directly, send the OTP email and redirect the user to the OTP entry page.
158
158
159
159
### Laravel Breeze Livewire Example
160
-
1. Replace the [LoginForm authenticate method](https://github.com/laravel/breeze/blob/2.x/stubs/livewire-common/app/Livewire/Forms/LoginForm.php#L29C6-L29C41) with a sendEmail method that runs the SendOtp action. For example:
160
+
1. Replace the [LoginForm authenticate method](https://github.com/laravel/breeze/blob/2.x/stubs/livewire-common/app/Livewire/Forms/LoginForm.php#L29C6-L29C41) with a sendEmail method that runs the SendOtp action and returns the newly created Otp.
161
+
161
162
```php
162
-
public function sendEmail(): void
163
+
use BenBjurstrom\Otpz\Actions\SendOtp;
164
+
use BenBjurstrom\Otpz\Exceptions\OtpThrottleException;
165
+
use BenBjurstrom\Otpz\Models\Otp;
166
+
//...
167
+
168
+
public function sendEmail(): Otp
163
169
{
164
170
$this->validate();
165
171
@@ -178,8 +184,7 @@ After installing Laravel Breeze or your preferred UI scaffolding, you'll need to
178
184
}
179
185
````
180
186
181
-
2. Next replace [Login component's login method](https://github.com/laravel/breeze/blob/e05ae1a21954c8d83bb0fcc78db87f157c16ac6c/stubs/livewire/resources/views/livewire/pages/auth/login.blade.php#L19-L23) with a method that calls the sendEmail method and redirects to the OTP entry page. For example:
182
-
187
+
2. Update the [Login component's login method](https://github.com/laravel/breeze/blob/e05ae1a21954c8d83bb0fcc78db87f157c16ac6c/stubs/livewire/resources/views/livewire/pages/auth/login.blade.php#L19-L23) to call the sendEmail method and redirect to the OTP entry page.
183
188
```php
184
189
public function login(): void
185
190
{
@@ -189,6 +194,53 @@ After installing Laravel Breeze or your preferred UI scaffolding, you'll need to
189
194
}
190
195
```
191
196
197
+
### Laravel Breeze Inertia Example
198
+
199
+
1. Remove password from the rules array and replace the [LoginRequest authenticate method](https://github.com/laravel/breeze/blob/e05ae1a21954c8d83bb0fcc78db87f157c16ac6c/stubs/default/app/Http/Requests/Auth/LoginRequest.php#L40) with a sendEmail method that runs the SendOtp action and returns the newly created Otp.
200
+
```php
201
+
use BenBjurstrom\Otpz\Actions\SendOtp;
202
+
use BenBjurstrom\Otpz\Exceptions\OtpThrottleException;
203
+
use BenBjurstrom\Otpz\Models\Otp;
204
+
//...
205
+
206
+
public function rules(): array
207
+
{
208
+
return [
209
+
'email' => ['required', 'string', 'email']
210
+
];
211
+
}
212
+
//...
213
+
214
+
public function sendEmail(): Otp
215
+
{
216
+
$this->ensureIsNotRateLimited();
217
+
RateLimiter::hit($this->throttleKey(), 300);
218
+
219
+
try {
220
+
$otp = (new SendOtp)->handle($this->email);
221
+
} catch (OtpThrottleException $e) {
222
+
throw ValidationException::withMessages([
223
+
'email' => $e->getMessage(),
224
+
]);
225
+
}
226
+
227
+
RateLimiter::clear($this->throttleKey());
228
+
229
+
return $otp;
230
+
}
231
+
```
232
+
233
+
2. Update the [AuthenticatedSessionController store method](https://github.com/laravel/breeze/blob/e05ae1a21954c8d83bb0fcc78db87f157c16ac6c/stubs/inertia-common/app/Http/Controllers/Auth/AuthenticatedSessionController.php#L30) to call the sendEmail method and redirect to the OTP entry page.
234
+
235
+
```php
236
+
public function store(LoginRequest $request): \Symfony\Component\HttpFoundation\Response
237
+
{
238
+
$otp = $request->sendEmail();
239
+
240
+
return Inertia::location($otp->url);
241
+
}
242
+
```
243
+
192
244
Everything else is handled by the package components.
0 commit comments