Something went wrong. Try again.
Store your seeds
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364<?php
declare(strict_types=1);
namespace App\Http\Controllers;
use App\Http\Requests\UpdateUserRequest;use App\Models\OtpVerificationToken;use App\Models\User;use App\Notifications\User\EmailUpdated;use Illuminate\Contracts\View\View;use Illuminate\Http\RedirectResponse;use Illuminate\Support\Facades\Auth;use Illuminate\Support\Facades\Crypt;use Illuminate\Support\Facades\Hash;
class AuthUserController extends Controller{ public function edit(): View { return view('profile.edit', ['user' => Auth::user()]); }
public function update(UpdateUserRequest $request, User $user): RedirectResponse { $data = collect($request->safe()->only([ 'name', 'email', 'password', ]))->reject(fn ($attr): bool => is_null($attr))->toArray();
$currentEmail = $user->email; $currentPassword = $request->validated('currentPassword');
if (isset($data['email']) && $data['email'] !== $currentEmail) { $otp = OtpController::generate();
OtpVerificationToken::updateOrInsert( ['email' => $currentEmail], [ 'token' => Hash::make($otp), 'payload' => Crypt::encrypt($data), 'created_at' => now(), ], );
$user->notify(new EmailUpdated($data['email'], $otp));
return to_route('otp.show', $user->id)->with([ 'info' => 'An otp has been sent to your registered email address', ]); }
if ($currentPassword && ! Hash::check($currentPassword, $user->password)) { return to_route('profile.edit')->with([ 'error' => 'Wrong password provided!', 'askPassword' => true, ]); }
$user->update($data);
return to_route('profile.edit')->with(['success' => 'Profile updated successfully!']); }}