Courses

[NEW] NativePHP: Build Mobile App with Laravel

API Authentication with Sanctum

You're reading a FREE PREVIEW of a PREMIUM course.

Link to the repository

[Only for premium members]

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:api

This will install Sanctum into our application and set up a few things:

  • Create and register api.php routes file
  • Create sanctum.php configuration file
  • Create personal_access_tokens table

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:

The full lesson is only for Premium Members.
Want to access all 11 text lessons of this course? (122 min read)

You also get:

  • 83 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord