Skip to main content

API Authentication with Sanctum

Premium
13 min read

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 of our courses? (31 h 13 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

No comments yet…

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.