Tự học Laravel: (28) CERTIFICATION (1)

In Laravel 5.6, a controller is provided so that user registration, login authentication, and password reset functions can be used immediately when creating a project. You can also create routing configurations and views to use these features with the artisan command. This time, we will implement user registration and login authentication.

MIGRATION

When you create the Laravel project, by default two migration files are created for user and password reset. If you have already done migration once, you have already made the necessary database entries.

For those who have forgotten, take a look back at db version control in migration, which we did earlier .

Default migration file

database/
└── migrations
    ├── 2014_10_12_000000_create_users_table.php
    └── 2014_10_12_100000_create_password_resets_table.php

CONTROLLER

Authentication related controllers also exist by default.

Default controller

app/Http/Controllers
└── Auth
    ├── ForgotPasswordController.php
    ├── LoginController.php
    ├── RegisterController.php
    └── ResetPasswordController.php

VIEW AND ROUTE GENERATION

Generate views and routes with the artisan command.

php artisan make:auth 

VIEW

The view for authentication is created as follows.

resources/views
├── layouts
│   └── app.blade.php
├── auth
│   ├── login.blade.php
│   ├── passwords
│   │   ├── email.blade.php
│   │   └── reset.blade.php
│   └── register.blade.php
└── home.blade.php

The layouts/app.blade.php used for authentication related screens and
the home.blade.php displayed after login are also created together.

CONTROLLER

HomeController.php has also been created.

app/Http/Controllers/
└── HomeController.php

ROUTE

An authentication route has also been added.

// routes/web/php
 
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');

Check the route with the artisan command.

php artisan route:list

+--------+-----------+-------------------------+------------------+------------------------------------------------------------------------+--------------+
| Domain | Method    | URI                     | Name             | Action                                                                 | Middleware   |
+--------+-----------+-------------------------+------------------+------------------------------------------------------------------------+--------------+
|        | GET|HEAD  | home                    | home             | App\Http\Controllers\HomeController@index                              | web,auth     |
|        | POST      | login                   |                  | App\Http\Controllers\Auth\LoginController@login                        | web,guest    |
|        | GET|HEAD  | login                   | login            | App\Http\Controllers\Auth\LoginController@showLoginForm                | web,guest    |
|        | POST      | logout                  | logout           | App\Http\Controllers\Auth\LoginController@logout                       | web          |
|        | POST      | password/email          | password.email   | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | web,guest    |
|        | POST      | password/reset          |                  | App\Http\Controllers\Auth\ResetPasswordController@reset                | web,guest    |
|        | GET|HEAD  | password/reset          | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web,guest    |
|        | GET|HEAD  | password/reset/{token}  | password.reset   | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web,guest    |
|        | POST      | register                |                  | App\Http\Controllers\Auth\RegisterController@register                  | web,guest    |
|        | GET|HEAD  | register                | register         | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | web,guest    |
+--------+-----------+-------------------------+------------------+------------------------------------------------------------------------+--------------+

OPERATION CHECK

USER REGISTRATION

I will try to actually register the user.

  • Access http://localhost:8000/register to display the user registration screen.
  • Enter user information and register. If http://localhost:8000/home is displayed, it is complete.
    You should see You are logged in! On the Dashboard.

Next, use the artisan command to see if the user has been saved to the DB.

$ php artisan tinker

>>> \App\User::all()->toArray();
=> [
       [
           "id"         => "2",
           "name"       => "who",
           "email"      => "who@sample.com",
           "created_at" => "2015-03-21 12:02:30",
           "updated_at" => "2015-03-21 12:02:30"
       ]
   ]
>>> 

One user data was registered. It is a success.

LOGOUT

I will try to log out next.

  • Access http://localhost:8000/home .
  • Click on the username in the upper right and select Logout from the pull-down menu.
  • If the article list is displayed, it is complete.

LOGIN

Try logging in as the previously registered user.

PASSWORD RESET

First, modify the APP_URL in .env.

# APP_URL=http://localhost
APP_URL=http://localhost:8000

The APP_URL value is used in the password reset email.

Next , set up the mail environment .

Please restart the WWW server after setting up the mail environment.

php serve

Then follow the steps below.

Log out and try logging in with your new password.

It has been long, so I will finish here. 
Next time I will make some minor adjustments to the content I generated this time.

SUMMARY

User registration and login, logout and password reset are available. It is helpful that the authentication function is included from the beginning.http://laravel.com/docs/master/authentication

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.