Laravel/UI: Register Without Password, Send Password Via Email
You can modify the registration process in a Laravel application that uses laravel/ui (link) to generate a random password and send it…
In Laravel applications using the laravel/ui package for authentication, the feature for verifying an email address after registration is built-in but is not enforced by default. To implement email verification, follow the steps below:
Ensure that your User model implements the MustVerifyEmail interface, which will enforce the user to verify their email.
use Illuminate\Contracts\Auth\MustVerifyEmail;
class User extends Authenticatable implements MustVerifyEmail
{
// ...
}
In your routes/web.php file, make sure to use the auth and verified middleware on routes that require a verified email address.
use Illuminate\Support\Facades\Route;
Route::get('/dashboard', function () {
// Only verified users may access this route...
})->middleware(['auth', 'verified']);
Ensure your users table has an email_verified_at column to store the timestamp when the user verified their email. If you don't have it, you may need to create a migration to add this column.
php artisan make:migration add_email_verified_at_to_users_table --table=users
In the generated migration file, you might add something like this:
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->timestamp('email_verified_at')->nullable()->after('email');
});
}
Then run the migration:
php artisan migrate
Laravel uses notifications to send the email verification link. Ensure that your User model uses the Notifiable trait.
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
// ...
}
toMail method on the VerifyEmail notification.redirectTo method or property on the EmailVerificationController.protected function redirectTo()
{
// Your redirect logic here...
return '/home';
}
If you want to make email verification optional:
verified middleware, you could create a custom middleware that checks if the user has verified their email and acts accordingly (e.g., showing a persistent reminder to verify the email).Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com