Files

51 lines
1.6 KiB
PHP
Raw Permalink Normal View History

2025-10-02 22:52:13 +03:00
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
2026-08-10 19:18:25 +03:00
use App\Models\User;
2025-10-02 22:52:13 +03:00
use Illuminate\Auth\Events\Verified;
use Illuminate\Http\RedirectResponse;
2026-08-10 19:18:25 +03:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
2025-10-02 22:52:13 +03:00
class VerifyEmailController extends Controller
{
/**
2026-08-10 19:18:25 +03:00
* Mark the user's email address as verified.
*
* Deliberately not session-gated: the link is opened from an email
* client, which may belong to a device/browser with no web session at
* all (e.g. an account registered through the Android app). The signed
* URL + hash-of-email check below is what proves identity here, not
* Auth::user().
2025-10-02 22:52:13 +03:00
*/
2026-08-10 19:18:25 +03:00
public function __invoke(Request $request, int $id, string $hash): RedirectResponse|\Illuminate\Http\JsonResponse
2025-10-02 22:52:13 +03:00
{
2026-08-10 19:18:25 +03:00
$user = User::findOrFail($id);
if (!hash_equals(sha1($user->getEmailForVerification()), $hash)) {
abort(403);
}
$alreadyVerified = $user->hasVerifiedEmail();
if (!$alreadyVerified && $user->markEmailAsVerified()) {
event(new Verified($user));
}
if ($request->wantsJson()) {
return response()->json([
'message' => $alreadyVerified ? 'Email already verified.' : 'Email verified.',
'verified' => true,
]);
}
if (Auth::check() && Auth::id() === $user->id) {
2025-10-02 22:52:13 +03:00
return redirect()->intended(route('dashboard', absolute: false).'?verified=1');
}
2026-08-10 19:18:25 +03:00
return redirect()->route('login')->with('status', 'Email verified. You can now log in.');
2025-10-02 22:52:13 +03:00
}
}