Learn how to create token based authentication for rest api using laravel sanctum. This project provides an API endpoint for registering, logging in, logging out, and getting data which is the implementation of one of Laravel Sanctum's uses, namely the API Token. In the HTTP Request process for the registration and login process, the system will generate an api token. This API token will later be used for the process of retrieving user data and also the logout process.
- Download this project or clone this repo and save to your local
- Run
composer install
to install all dependencies needed, if you have not the composer you can go to here to see how to install it - Download and install Laravel in your computer, go to here to see how to install it
- Create new laravel project, see the documentation
- Open your computer server to run your server, then create new database on your database
- Configure your .env file, go to database section, and configure the database according to the database you have made before, the user, and the password you are using as below. If you don't make database yet, so make new database in your server. Then configure in .env file.
DB_CONNECTION=mysql DB_HOST=your_host DB_PORT=your_port DB_DATABASE=your_db_name DB_USERNAME=your_username DB_PASSWORD=your_password
- You can first check in the
composer.json
file whether laravel sanctum has been installed or not in your laravel project - If there is no laravel sanctum so run command below
composer require laravel/sanctum
- After that, we publish the laravel sanctum configuration and also the migration file using the command
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
- Next we run migrations
php artisan migrate
- Reopen the terminal, then run the following command
php artisan make:controller Api\AuthController
- After we run the command, we can see that there is a new file, namely AuthController.php in the app/Http/Controllers/Api directory. Open the AuthController.php file, then we add register(), login(), and logout() methods. You can see how the code run in this folder
app\Http\Controllers
- The next step is to define the route. It's different from the usual tutorial, because this tutorial is about api, so we define routes in the routes/api.php file. Open the routes/api.php file, then we add a route to register, login and logout. You can see how the code run in this file
routes\api.php
- To test it, we first run our project first. Open terminal, then run the following command
php artisan serve
- Then you can open your postman, and try the endpoint below
Request :
- Method : POST
- Endpoint :
/api/register
- Header :
- Accept: application/json
- Body :
{
"name": "string",
"email": "string, email",
"password": "string",
}
Response :
{
"status": "boolean",
"status_code": "integer",
"message": "string",
"data": {
"id": "integer, unique",
"name": "string",
"email": "string",
"created_at": "timestamp",
"updated_at": "timestamp"
},
"access_token": "string, unique",
"token_type": "string",
}
Request :
- Method : POST
- Endpoint :
/api/login
- Header :
- Accept: application/json
- Body :
{
"email": "string, email",
"password": "string",
}
Response :
{
"status": "boolean",
"status_code": "integer",
"message": "string",
"access_token": "string, unique",
"token_type": "string",
}
Request :
- Method : POST
- Endpoint :
/api/logout
- Header :
- Accept: application/json
- Autohorization: Bearer token
Response :
{
"status": "boolean",
"status_code": "integer",
"message": "string",
"access_token": "string, unique",
"token_type": "string",
}
Request :
- Method : GET
- Endpoint :
/api/user
- Header :
- Accept: application/json
- Autohorization: Bearer token
Response :
{
"status": "boolean",
"status_code": "integer",
"message": "string",
"data": {
"id": "integer, unique",
"name": "string",
"email": "string",
"created_at": "timestamp",
"updated_at": "timestamp"
},
"access_token": "string, unique",
"token_type": "string",
}