How to Create Email Verification in Laravel 8 with Gmail (ok)
https://codelapan.com/post/how-to-create-email-verification-in-laravel-8-with-gmail
Last updated
Was this helpful?
https://codelapan.com/post/how-to-create-email-verification-in-laravel-8-with-gmail
Last updated
Was this helpful?
Đường dẫn để gửi lại xác thực trông giống như này Ví dụ đã hoàn thành :)
C:\xampp\htdocs\test\routes\web.php
C:\xampp\htdocs\test\app\Models\User.php
C:\xampp\htdocs\test\app\Http\Controllers\HomeController.php
Previously, we had to know what the purpose of making email verification was in this project made with Laravel 8. So, making account verification or email verification will prevent users from entering or registering fake emails when registering.
For example, if there is a website that requires users to register but does not include account verification or email verification, the user can just enter fake emails such as sdsd@dds.dsfds to be able to go directly to the protected page (requires user login). It's different if the website requires that every user who registers or registers must verify the account first, it will certainly minimize users who register with fake data or emails.
Because we will make email verification or account verification with email in laravel that uses Google mail service as a mail host, so we need to set up the gmail account.
Make sure the gmail account that will be used, in the Less secure application access section is active or can be seen as shown above (Sorry, in image above, i use google account with bahasa Indonesia).
Alright, let's just start the experiment of making email verification in laravel 8 with gmail. In this experiment we will start from 0 that is from starting to install a new laravel project until we successfully verify the email. Here are the steps:
The first step is to install a new laravel project. Here we install it using composer. Open a terminal, go to the directory where we want to put the project folder. then run the command as below.
Copy
With the above command, we will install a new laravel project which will be named emailverify.
We will use the laravel ui package to create the auth feature, and therefore we need to install the laravel ui package in our project. To install it, you can go to the project directory that we just installed with the cd emailverify command then run the command below.
Copy
After that, we need to install the ui using bootstrap to do authentication. Run the command below to install the bootstrap ui in the project.
Copy
wait until the process is complete. If you have run the command
Copy
Wait for it to finish. If there is a "Finished, Please run mix again" message, run the command npm install && npm run dev again.
To accommodate user data, we need to create a new database. For that please create a database first in xampp, laragon or others. In this experiment, I created a new database with the name laravel.
Next, edit some records in the .env file. There are several records that we need to adjust, including DB_DATABASE. in DB_DATABASE enter the name of the new database that was created in step number 3. Then adjust it to the record below.
Copy
Here we will use smtp as the mailer, then for the host and port we use smtp.googlemail.com and 587, for encryption we can use tls. In the MAIL_USERNAME, MAIL_PASSWORD, MAIL_FROM_ADDRESS and MAIL_FROM_NAME records, please adjust them to your gmail account.
So overall, the .env file will be like below.
Copy
To migrate the tables that have been created with migration to the database that has been created, we can run the php artisan migrate command.
To enable the verification feature, we need to define it in the web.php file in the routes folder.
find code
Copy
Then, change into
Copy
In the Models/User.php file add implements MustVerifyEmail, so the overall Models/User.php file will be as below.
Copy
To provide protection on a page, we can use middleware. As in this experiment, we will protect the home page so that it cannot be accessed before the user verifies the email used when registering. Manage middleware can be placed in the controller or in the route.
Example of middleware in controller, we can add function as below in HomeController.php
Copy
Overall, the code contained in the HomeController.php file will be as below.
Copy
Or you can also put the middleware in the route as below.
Copy
We are free to choose whether we want to use the middleware in the controller or route.
FINISHED.
Now comes the testing stage. To try it, please run the project with php artisan serve or if you use valet please access it with namaproject.test.
Then we will not be directly directed to the home page but to the email verify page first. To be able to proceed to the home page, we must verify our email first. Please check the inbox on our gmail account.
For email verification, we can directly click the Verify Email Address button, then we will be directed to the home page.
OK, we have succeeded in making email verification in laravel 8 with gmail. That's all for this article, if you have suggestions, criticisms, input or whatever you want to discuss, please write them in the comment form below
Admin・ 7 Oktober 20219 min read ・ 5434 views
Laravel Email Verification - If previously we have shared an article that discusses r, then on this occasion we will share an article that is almost the same but in different media. In this article, we will try how to make account verification with email or maybe better known as laravel email verification.
Link:
Go to the register menu, try registering by entering an active email and if everything is filled in, click Register.