It's been several days now that I spend on Laravel Sanctum trying to figure out how I can set up my authentication system.
Context
I'm building a tickets web app for my company and for this i decided to build an API and a React frontend app that consumes the API. End-users of the app need an account to manage tickets so i create an user table in the database and create several routes for the API to create, delete and update users. To manage users api authentication, i decided to use Laravel Sanctum and his built-in SPA authentication system that is session based.
For that, i follow the guide and setup all the needs to ensure that the frontend is ready to work with laravel sanctum. Here my Kernel.php for the api middlewares :
'api' => [
\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
And the routes in web.php to login / logout the user through Laravel Sanctum SPA authentication.
<?php
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;
Route::post('/login', [UserController::class, 'login']);
Route::get('/logout', [UserController::class, 'logout']);
Problem
I have to build a non-web application, in particular a windows-service that needs to fetch data in a dabatase and use the /api/tickets
endpoint of the API with POST
method to create a new ticket with these data.
I know that this futur windows-service is not a front-end app so i can't use the SPA authentication system. Instead, i want to use tokens with Laravel Sanctum to authenticate the service.
My questions are :
- How i can achieve this and seperate the
users
authentication and theservice
authentication. - As my api contains
http://myapi.net/login
ethttp://myapi.net/logout
for end-users authentication, do i need to create something likeapi/auth/service
route for sending and deleting token ? - Should i create a new table called
service
in my database or stay withuser
table ?