Apis Rest for e-commerce

0

I'm trying to set up an e-commerce with a friend and we're thinking of putting together an api rest to access the database and creating a website and an application to consume it. As this is an e-commerce, I would like to know if the Rest APIs serve to register and log a user into the system.

Thanks in advance.

    
asked by anonymous 24.05.2018 / 23:59

1 answer

1

First, start the project:

composer create-project --prefer-dist laravel/laravel sua-api

Enter the folder:

cd sua-api

After this, you will need to create the authentication, so use the command:

php artisan make:auth

This command will create a series of files .blade resourcer/view/auth and resources/view/auth/passwords , will create another Controller and Model that will automate your login process, also, will add inside your api.php file % two lines:

Auth::routes();

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

Now you will need to place your routes within a middleware , this file should have the following route configuration:

Route::group(['middleware' => ['web']], function () {
    Route::get('/', function () {
        return redirect()->to('http://127.0.0.1:8000/login');
    });

    Auth::routes();

    Route::get('/home', 'HomeController@index')->name('home');
});

In order to log in, you will need to have a table with the users and the memory key of the authentication (remember_token). To do this, use the command:

php artisan make:migration create_users_table

After that, open the migration that was generated in the database/migrations folder, and leave the up function as follows:

public function up()
{
    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id');

        $table->string('name', 32);
        $table->string('username', 32);
        $table->string('email', 320);
        $table->string('password', 64);
        $table->string('remember_token', 100)->nullable();
        $table->timestamps();
  });
}

Run the migration:

php artisan migrate

And finally, start your project server:

php artisan serve

Now, go to:

https://127.0.0.8000/

You will be redirected to a default Laravel login page, on this page in the upper left corner you can register users, after registering, login, it should be redirected to Home Laravel as well .

From this, you can change all generated templates as you wish, and use the other functions.

To check if a user is logged in and allowed to execute functions in the methods created later use the second command in their models:

 Auth::check()

You should answer true or false .

This should be enough to start your project.

    
26.05.2018 / 02:19