The introduction

In order to retain users, we do everything we can to get users’ registrations and keep them in the app. Laravel has developed a part of the user registration and login code, if it is easy not to re-develop, it can be used. For developers who want to deeply customize user permissions, it’s important to know about users and permissions.

This issue is about the functions of user registration and login.

The users table

The User’s data and information must be in the database, so Laravel comes with the User model. In the app/ user.php file, the internal code declares the necessary parts:

namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;
    protected $fillable = [
        'name'.'email'.'password',];protected $hidden = [
        'password'.'remember_token',]; }Copy the code

You’ll notice that this Model file is very different from the Model we introduced earlier because it directly inherits the Illuminate Foundation Auth User class, which we then trace back to:

class User extends Model implements
    AuthenticatableContract.AuthorizableContract.CanResetPasswordContract
{
    use Authenticatable.Authorizable.CanResetPassword.MustVerifyEmail;
}
Copy the code

It turns out that the User class not only inherits the Model class, but also implements a number of permission-related interface classes.

  • AuthenticatableContract Identifies user identities and tokens
  • AuthorizableContract Defines whether a user has certain permissions
  • CanResetPasswordContract Is used to change a password

These will be covered in more detail in other chapters.

With the model, there is no database table, and the model does not get any data except processing logic. So the first step is to complete the database migration.

2014_10_12_000000_create_users_table.php = 2014_10_12_000000_create_users_table.php

public function up()
{
    Schema::create('users'.function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}
Copy the code

There is also the down method used by the rollback migration:

public function down()
{
    Schema::dropIfExists('users');
}
Copy the code

During the migration rollback, if the Users table is created successfully, it is deleted. Execute the migration command from the command line:

php artisan migrate
Copy the code

After successful execution, use mysql client to connect to the database and print the users table structure as shown below:

Log in to register

With the data support of the Users table and the built-in user logic of Laravel, we can easily have a login and registration page. Executing commands on the command line:

php artisan make:auth
Copy the code

This is a scaffolding command, you can see the home page login LGOIN, REGISTER navigation button. The default view file is also created in ** resources/views/auth**.

Take a look at the routing file routes/web.php and add a line of code:

Auth::routes();
Copy the code

In the same way that the Route:: Resource () method creates all controller methods required by default for restfulapi, the Auth:: Routes () method contains content that is related to user login, forgetting password, resetting password, etc.

The pre-registered routes are shown as follows:

How much time would it have taken us to design our own routing and controller methods? Oh, great. I’ll just use it. Laravel is delicious! Here is the default registration front end.

Write in the last

This article has shown you how to use the user authorization functionality associated with laravel’s built-in Users table, which is the cornerstone of a user-enabled application and will be covered in more depth in future articles.

Happy coding 🙂

I am a @programmer assistant, an original author in the IT field who focuses on programming knowledge and circle dynamics