Laravel5.2 new feature series-implementation of multi-user authentication

Source: Internet
Author: User
Laravel5.2 new feature series-multi-user authentication feature implementation details Laravel 5.2 adds multi-user authentication support, that is, allows users of different data tables (such as foreground users and background users) to log on to the authentication. Next we will briefly introduce the implementation of multi-user login and registration functions.

1. generate certification scaffolding

First, use the authentication scaffolding provided by Laravel 5.2 to complete initialization:

php artisan make:auth

The Artisan command generates the routes, views, and HomeController required for user authentication:

View the route file routes. php and you will find that the file has been updated:

Route::group(['middleware' => 'web'], function () {    Route::auth();    Route::get('/home', 'HomeController@index');});

Here, Route: auth () defines the registered logon Route./home is the Route entry after the authentication is passed.

2. implement logon for foreground users

Next, we will first implement the login of the front-end User, that is, the User login that comes with Laravel. Through the scaffolding above, we have generated all the code required for authentication, and the rest is to use the migration command to create a user authentication table:

php artisan migrate

After the command is executed, the users table and password_resets table are generated, respectively, the user-owned table and the password reset table.

Then we can enter a http://laravel.app/register in the browser to register a new user:

We create a user named test, registration successful, enter the http://laravel.app/home, you will find that you have logged on:

3. edit the authentication configuration file

To implement multi-user authentication, you must first configure the authentication configuration file auth. php. here we implement the function of the frontend and backend user logon, so the corresponding configuration is as follows:

  [        'guard' => 'web',        'passwords' => 'users',    ],    'guards' => [        'web' => [            'driver' => 'session',            'provider' => 'users',        ],        'admin' => [            'driver' => 'session',            'provider' => 'admins',        ],        'api' => [            'driver' => 'token',            'provider' => 'users',        ],    ],    'providers' => [        'users' => [            'driver' => 'eloquent',            'model' => App\User::class,        ],        'admins' => [            'driver' => 'eloquent',            'model' => App\Admin::class,        ],    ],    'passwords' => [        'users' => [            'provider' => 'users',            'email' => 'auth.emails.password',            'table' => 'password_resets',           'expire' => 60,        ],    ],];

Authentication consists of guard and provider (refer to the user authentication document). Therefore, the admin and admins options are added in these two configuration items.

4. create a background user model

Next we will implement background user logon. First, use the following Artisan command to generate the background user model:

php artisan make:model Admin --migration

With the -- migration option, the corresponding user table admins will be generated. we define that the fields of this data table are the same as those of users:

Schema::create('admins', function (Blueprint $table) {    $table->increments('id');    $table->string('name');    $table->string('email')->unique();    $table->string('password', 60);    $table->rememberToken();    $table->timestamps();});

Run the migration command to generate the table:

php artisan migrate

Then update the Admin model class as follows:

      5. define Backend User Authentication routes and controllers     

Next, let's define the background user authentication route. modify the routes. php code as follows:

Route::group(['middleware' => ['web']], function () {    Route::auth();    Route::get('home', 'HomeController@index');    Route::get('admin/login', 'Admin\AuthController@getLogin');    Route::post('admin/login', 'Admin\AuthController@postLogin');    Route::get('admin/register', 'Admin\AuthController@getRegister');    Route::post('admin/register', 'Admin\AuthController@postRegister');    Route::get('admin', 'AdminController@index');});

Then use the Artisan command to create the corresponding controller:

php artisan make:controller Admin/AuthControllerphp artisan make:controller AdminController

Edit the Admin/AuthController. php code as follows:

  middleware('guest:admin', ['except' => 'logout']);    }    protected function validator(array $data)    {        return Validator::make($data, [            'name' => 'required|max:255',            'email' => 'required|email|max:255|unique:admins',            'password' => 'required|confirmed|min:6',        ]);    }    protected function create(array $data)    {        return Admin::create([            'name' => $data['name'],            'email' => $data['email'],            'password' => bcrypt($data['password']),        ]);    }}

Edit the AdminController. php code as follows:

  middleware('auth:admin');    }    public function index()    {        $admin = Auth::guard('admin')->user();        return $admin->name;    }}
6. create and modify view files

Finally, we need to create a view file for background user authentication. here we simply copy the foreground user view template and make some modifications:

cp -r resources/views/auth resources/views/admin

Modify the logon and registry ticket submission addresses in the resources/views/admin Directory:

/login -> /admin/login/register -> /admin/register
7. implement background user authentication

Accessing the http://laravel.app/admin/register in a browser also displays the registration page:

Register a user with a username of admin, after successful registration, the page jumps to the http://laravel.app/admin, indicating that the authentication is successful.

Now, we have completed the login authentication function for both front-end and back-end users.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.