dipeshkhanal79 liked a comment+100 XP
1mo ago
Hello everyone,
I'm currently using Laravel 11 and I use sanctum for authentication.
When i don't pass any Bearer Token i have the redirection to the Login page through AuthenticationException, that's the normal workflow from sanctum.
As my Laravel project will be only for API i would like to return a custom Json response. I know that i can pass the "Accept : application/json" in the Header request to return the default sanctum error Json but i would like to always return my custom JSON without passing "Accept : application/json" in the Header.
My question : How can I override the default workflow when AuthenticationException is raised by Sanctum ? I tried by creation a custom Exception and also in the bootstrap/app.php but nothing works.
bootstrap/app.php ->
use Illuminate\Auth\AuthenticationException;
->withExceptions(function (Exceptions $exceptions) {
$exceptions->stopIgnoring(AuthenticationException::class);
$exceptions->render(function (AuthenticationException $exception, Request $request) {
//Unauthorized
return response()->json([
'message' => 'Unauthorized action',
'custom_field' => 'My new field'
], 401);
});
Thank very much !
dipeshkhanal79 wrote a reply+100 XP
2mos ago
try getting it inside a closure function.
dipeshkhanal79 wrote a reply+100 XP
5mos ago
Go for new column and add belongsTo Relation. Make it as simple as possible.
dipeshkhanal79 wrote a reply+100 XP
5mos ago
Made an package to make it easier for developer to make frontend seo optimized.
Here is the link: https://khanaldipesh.com.np/package/laravel-seo-manager
Would love to get your feedback or maybe PRs.
dipeshkhanal79 wrote a reply+100 XP
5mos ago
I have made a package to do this for you.
Here is the link: https://khanaldipesh.com.np/package/laravel-seo-manager
Would love to get your feedback or maybe PRs.
dipeshkhanal79 wrote a reply+100 XP
5mos ago
I had faced this kind of problems in the past and from then i always use "" to add values in .env file.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE="databasename"
DB_USERNAME="username"
DB_PASSWORD="pa$$word"