@siteclub One possible solution to authenticating an Inertia session in a mobile webview using a token from Sanctum is to use the createFromToken method provided by the Laravel\Sanctum\HasApiTokens trait. This method allows you to create a new user instance from a given token. You can then use this instance to log the user in by calling the login method on the Auth facade.
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens;
// ...
}
Then in your controller or route handling the login you can use the following
$user = User::createFromToken($token);
Auth::login($user);
This will authenticate the user and set the necessary session data for the Inertia.js frontend to function properly.
Also make sure that your api group middleware is having the Sanctum middleware,
\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
Make sure to check if your token is valid and not expired by calling tokenCan method of the User model with the 'auth' scope
if ($user->tokenCan('auth')) {
Auth::login($user);
}
Additionally, you should also ensure that the EncryptCookies, AddQueuedCookiesToResponse, and StartSession middleware are added to your API middleware group in the Kernel class, to ensure that cookies are being set and encrypted properly.