Build Laravel 5.7 Email Authentication with Mailgun and Digital Ocean

Build Laravel 5.7 Email Authentication with Mailgun and Digital OceanConnor LeechBlockedUnblockFollowFollowingDec 13Build session authentication with email verification into a default Laravel 5.7 appMailgun integrates with default Laravel applicationsThe newly released Laravel version 5.7 adds a new capability to verify user’s emails..If you’re ever ran php artisan make:auth within a Laravel app you’ll know the feeling of pleasure and excitement when you see all of your register and login pages, complete with password hashing and a users table already built out..This excitement will soon subside after deploying to production only to find that anyone can register with or whatever they feel like typing in..This thin session authentication layer doesn’t help much in a live application..The introduction of Laravel 5.7’s MustVerifyEmail interface adds to the authentication scaffold so that in order for users to login they must use a real, valid email address.Source code on GitHubCreate a new Laravel applicationThe first step is to create a new application with some setup:$ laravel new email-verification-example$ php artisan make:authAuthentication scaffolding generated successfully.$ touch database/database.sqliteThis will create the default Laravel 5.7 app with the authentication scaffold and a file for a SQLite database..Update your .env file to point to the full path of the SQLite file:DB_CONNECTION=sqliteDB_DATABASE=/full/path/to/email-verification-example/database/database.sqliteEmail verification in LaravelLet’s inspect the default auth scaffold and see what changes need to be made to add email verification.You can view the Laravel Email Verification docsThe first step is to implement the MustVerifyEmail interface..An interface in PHP specifies the methods a class must implement.<?phpnamespace App;use IlluminateNotificationsNotifiable;use IlluminateContractsAuthMustVerifyEmail;use IlluminateFoundationAuthUser as Authenticatable;class User extends Authenticatable implements MustVerifyEmail{ …That interface enforces that we have three new methods on our User model:hasVerifiedEmail()markEmailAsVerified()sendEmailVerificationNotification()The implementations for these methods can be found in vendor/laravel/framework/src/Illuminate/Auth/MustVerifyEmail.php..To mark a user as verified we need to have a email_verified_at column on our users table, which is already provided in our default create users table migration:Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps();});Run the migrations to create the table: php artisan migrateIn the routes/web.php file update the authentication routes to include email verification:Auth::routes(['verify' => true]);To view all the routes available to your application you can run php artisan route:list..That’s pretty much all we need to do from the Laravel side to verify emails..If you try it now though, it won’t work:Error: Expected response code 250 but got code “530”, with message “530 5.7.1 Authentication required “Before we can send emails from our PHP server we need to configure a mail driver within Laravel.You can check though using tinker to see that a user was created with an email_verified_at of null.$ php artisan tinkerPsy Shell v0.9.9 (PHP 7.1.7 — cli) by Justin Hileman>>> $users = AppUser::all();Be sure to consult the Laravel docs on sending EmailEnter MailgunMailgun logoMailgun offers the ability to send emails via SMTP server or over their API..In this tutorial we’re going to send emails via the Mailgun API..It’s faster, scales better and takes less work to setup than configuring SMTP.How to Start Sending Email from the Mailgun docsWe’re going to be sending API calls.. More details

Leave a Reply