-
Notifications
You must be signed in to change notification settings - Fork 128
Email verification should work across devices #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,27 +3,39 @@ | |
namespace App\Http\Controllers\Auth; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\User; | ||
use Illuminate\Auth\Events\Verified; | ||
use Illuminate\Foundation\Auth\EmailVerificationRequest; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\RedirectResponse; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class VerifyEmailController extends Controller | ||
{ | ||
/** | ||
* Mark the authenticated user's email address as verified. | ||
*/ | ||
public function __invoke(EmailVerificationRequest $request): RedirectResponse | ||
public function __invoke(Request $request, int $id, string $hash): RedirectResponse | ||
{ | ||
if ($request->user()->hasVerifiedEmail()) { | ||
return redirect()->intended(route('dashboard', absolute: false).'?verified=1'); | ||
if (! $request->hasValidSignature()) { | ||
abort(403, 'Invalid or expired verification link.'); | ||
} | ||
|
||
if ($request->user()->markEmailAsVerified()) { | ||
/** @var \Illuminate\Contracts\Auth\MustVerifyEmail $user */ | ||
$user = $request->user(); | ||
|
||
$user = User::findOrFail($id); | ||
|
||
if (! hash_equals($hash, sha1($user->getEmailForVerification()))) { | ||
abort(403, 'Invalid verification hash.'); | ||
} | ||
|
||
// Now you can verify the email | ||
if (! $user->hasVerifiedEmail()) { | ||
$user->markEmailAsVerified(); | ||
|
||
// Fire event when email is verified | ||
event(new Verified($user)); | ||
} | ||
|
||
Auth::login($user); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes verification links a little dangerous – anyone who gets hold of one is instantly logged in with no other auth required. Usual practice after verification is to require a login, which solves that problem, though at a mild UX expense. Note that this additional login doesn't really apply for OAuth, since verification and login are essentially the same thing there. |
||
|
||
return redirect()->intended(route('dashboard', absolute: false).'?verified=1'); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be calling
sha1
directly? Isn't there a Laravel abstraction for this? Also bearing this in mind.