Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Sultenhest's avatar

Consuming own API - passport (Sanctum)

Hi

I want to build a Laravel application where most of the functionality (CRUD) will exist once the user has logged in. I want to build this backend part as an SPA (using vue-router) inside the Laravel project, consuming it's own API.

I also want to use the "standard" authentication from the laravel/ui package to log the user in and, as of now, i don't plan on building external consumers or letting the user create their own consumers of the API.

Since Laravel 7, the section about API Authentication with api_tokens ( https://laravel.com/docs/6.x/api-authentication ) has been removed from the docs, so i suppose this approach has been deprecated, even though, this basically does what i need with localStorage, but lacks security.

So my question is, is it overkill to use Passport for this or is this generally the way to do it in Laravel 7 for a use case like this? What is, if there is one, the alternative?

0 likes
2 replies
Sultenhest's avatar

Hi @oussama.tn

Of course! I forgot about Laravel Sanctum. Thank you :)

So if i want to consume my own API, all i need to do is add the HasApiTokens trait to my User models and create a token on registration like this in my RegisterController.php:

protected function create(array $data)
{
    $user = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);

    $user->createToken('token-name');

    return $user;
}

All my routes with the auth:sanctum middleware seems to work correctly... it all seems suspiciously easy

Please or to participate in this conversation.