Let's start preparing to have a mobile app along with our web page. Here's our action plan:
- Installing Laravel API engine (which sets up route file, installs sanctum)
- Create Auth endpoints for Login and Registration
- Create a Profile update endpoint
- In the next lesson: Build API endpoints for Events and Talks
Let's get started!
Installing Laravel API
Our first step is to install Laravel API scaffolding:
php artisan install:apiThis will install Sanctum into our application and set up a few things:
- Create and register
api.phproutes file - Create
sanctum.phpconfiguration file - Create
personal_access_tokenstable
The only thing we have to do is add a trait to our Users Model:
app/Models/User.php
use Laravel\Sanctum\HasApiTokens; // ... class User extends Authenticatable{ /** @use HasFactory<\Database\Factories\UserFactory> */ use HasFactory, Notifiable; use HasFactory, Notifiable, HasApiTokens; // ...}Once this is done, we are ready to create our first API endpoints.
Creating Authentication Endpoints
Let's start our API implementation by creating the first endpoints:
- One for Registration
- One for Login
- One for User details
These endpoints will live in a single Controller for our use case: