php - make laravel api only accesible by logged users in laravel 7
Get the solution ↓↓↓I m quite new to laravel and sanctum I made the authentification api and now I want to make my CRUD api only accessible for the logged users
how can I do this please ?
Answer
Solution:
if you use the Laravel built-in API system (api_token
) ,Then you can simply make the middlewareauth:api
for example:
way 1: in controller
public function __construct()
{
$this->middleware('auth:api');
}
way 2: in the route
Route::get('route', function () {
// Only authenticated users may enter...
})->middleware('auth:api');
Edit 1:
for laravel 7 andsanctum
look at laravel docs : https://laravel.com/docs/7.x/sanctum#protecting-routes
Answer
Solution:
In the route filr like this
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
You might find this walk trough easyer to follow.. https://dev.to/romanpaprotsky/vue-js-token-based-authentication-with-laravel-sanctum-3a84
Usualy you probably use a combination of route prefix and group.. so you can add many routes to a group... needing sanctum auth guard... and maybe prefixed by , for example /api/v1/
You might wanna take a look here for groups https://laravel.com/docs/7.x/routing#route-group-middleware
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: filter_sanitize_string deprecated
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.