First, start the project:
composer create-project --prefer-dist laravel/laravel sua-api
Enter the folder:
cd sua-api
After that, 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 one more Controller
and a Model
that will automate your login process, too, will add inside your file api.php
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 authentication memory key (remember_token), for this use the command:
php artisan make:migration create_users_table
After that, open the migration
that was generated in the folder database/migrations
, and leave the function up
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();
});
}
Perform 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 standard login page of Laravel
, on this page in the upper left corner you can register users, after registering it, log in, must be redirected to a Home
pattern of Laravel
also.
From this, you can change all the generated templates as you want, and use the other functions.
To check if a user is logged in and is allowed to perform functions in the methods created later use the following command in your models:
Auth::check()
You must answer true
or false
.
This should be enough to start your project.
Perhaps you should rephrase your question a little better so that it does not end up being closed because it is too wide, what is the level of your doubt ? on how to create an ? api about which framework or tool to use ? and yes, API’s Rest serve for this, you can even use combined front and back frameworks, a good example of this is the combination of API Rest in Laravel with Vuejs, explain a little better what your question, so me or someone else can respond with assertiveness.
– AnthraxisBR
My question is just this. If it was possible to log in and register a user using a Rest api. I have no doubts about how to create an api. And not even about tools. We’re still thinking about how the system will work.
– Leon Cruz
Ah, so the comment solved your doubt ? recommend using Laravel for this type of product if you use it in PHP, just confirm if you want to do it in PHP, then I can prepare a good answer.
– AnthraxisBR
We are thinking of using Django or Laravel. How these operations would be done in Laravel?
– Leon Cruz
I am preparing a complete answer to help you, already put it ;D
– AnthraxisBR