To start with the scaffold for Laravel authentication, run the following command on the console:
php artisan make:auth
This will create standard views for registration and login, as well as authentication middleware, along with a group of routes. All you have to do is put the protected routes within that group.
Example:
Route::get('/', function () {
return view('home.home', ['nav' => "home"]);
});
Route::group(['middleware' => ['auth']], function () {
Route::group(['prefix' => 'products'], function () {
Route::get('/', 'ProductController@getAll');
Route::get('/ajax', 'ProductController@getAllAjax');
Route::get('/stock/danger', 'ProductController@getStockDanger');
Route::get('/ajax/{id}', 'ProductController@getOne');
Route::post('/store', 'ProductController@store');
Route::get('/delete/{id}', 'ProductController@deleteView');
Route::get('/destroy/{id}', 'ProductController@destroy');
Route::get('/edit/{id}', 'ProductController@editView');
Route::post('/update', 'ProductController@update');
Route::post('/search', 'ProductController@search');
Route::get('/{id}', 'ProductController@detail');
});
Route::auth();
In this case the route that redirects to /
is published while the group within Route::group(['middleware' => ['auth']], function () {});
requires authentication. This is done from the users
table created by Laravel himself.