How to use web and api guards at the same time in Laravel?

不想你离开。 提交于 2019-12-04 22:43:29

Protect whatever routes are for your api with auth:api middleware

Route::group(['middleware' => ['auth:api']], function(){
    //protected routes for API
});

We must make sure that your users table has an api_token column:

php artisan make:migration alter_users_table_add_api_token_column

Then inside of the up function:

Schema::table('users', function($table){
    $table->string('api_token', 60)->unique(); //this must be 60 characters
});

Finally in your App\Http\Controllers\Auth\RegisterController, modify the create file to add your api_token

protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'api_token' => str_random(60) //add this
    ]);
}

Now request to the auth:api protected routes will need to contain an api_token in their payload.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!