'How to access Sanctum package in custom laravel package

i want to access laravel sanctum auth which is working fine in project routes I'm making a custom package of api's which needs to use same sanctum authentication with in the custom package routes



Solution 1:[1]

use auth sanctum middleware for your routes, See below example.

https://laravel.com/docs/9.x/sanctum#protecting-routes

Solution 2:[2]

I was having the same problem, but I found that the packet routes did not have a default guard and the session was not accessible through the packet.

The solution was to add the 'web' middleware to the routes.


Before:

Route::get('/dashboard', [HomeController::class, 'index'])->middleware(['auth:sanctum'])->name('dashboard');

After:

Route::get('/dashboard', [HomeController::class, 'index'])->middleware(['web', 'auth:sanctum'])->name('dashboard');

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sajid
Solution 2 Macedo_Montalvão