Something went wrong. Try again.
Store your seeds
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374<?php
declare(strict_types=1);
namespace App\Http\Controllers;
use App\Models\OtpVerificationToken;use App\Models\User;use App\Notifications\User\EmailUpdated;use Illuminate\Contracts\View\View;use Illuminate\Http\RedirectResponse;use Illuminate\Http\Request;use Illuminate\Support\Facades\Crypt;use Illuminate\Support\Facades\Hash;
class OtpController extends Controller{ /** * @param int $length Total number of digits the otp should connsist of */ public static function generate($length = 4): int { $min = 10 ** ($length - 1); $max = (10 ** $length) - 1;
return random_int($min, $max); }
public function show(User $user): View { return view('otp.show', ['user' => $user]); }
public function verify(Request $request, User $user): RedirectResponse { $record = OtpVerificationToken::where('email', $user->email)->first();
if (! $record || ! Hash::check($request->otp, $record->token)) { return back()->with(['error' => "The provided otp doesn't match our records"]); }
if ($record->isExpired()) { $record->delete();
return back()->with(['error' => 'OTP expired! Please request a new one']); }
$payload = Crypt::decrypt($record->payload);
$user->update($payload);
$record->delete();
return to_route('profile.edit')->with(['success' => 'Profile updated successfully!']); }
public function resend(User $user): RedirectResponse { $otp = static::generate();
OtpVerificationToken::updateOrInsert( ['email' => $user->email], [ 'token' => Hash::make($otp), 'created_at' => now(), ], );
$user->notify(new EmailUpdated($user->email, $otp));
return back()->with(['success' => 'A new otp has been sent to your email address']); }}