#laravel11 #laravel_tutorial #laravel_api Hi All, Sorry! I used error code 401 for validation. The code is wrong. Validation error code - 422 Unauthorized error code - 401 please change it while coding
Thanks a lot. Will fit for any level of developer :) I'm using laravel for 5-7 years but older versions mostly. I was surprised that 11th doesn't have scaffolded auth routes like it did before. From what I remember, previously you could just "php artisan make:auth" to have all needed auth routes
Awesome video, thanks! btw , I am getting this in the profile link when the token is not right : Symfony\Component\Routing\Exception\RouteNotFoundException: Route [login] not defined. in file Any idea how to fix it?
The route is protected. Please add "Accept"=>"application/json" in Postman - Headers parameter. It will show: { "message": "Unauthenticated." } Laravel will check the configuration for "Accept", it will return the web page or html if no value is set at "Accept" by default.
@@rubyvidhyaammu4984 but since this is in the api routes, can't we automatically get this behaviour without the necessity to send the Accept type? This would prevent the error on this endpoint.
You need to check your return type. You'll find the warnings go away if you wrote something like this. Please try this in function.... $user = Auth::user(); if ($user instanceof \App\Models\User) { // Hinting here for $user will be specific to the User object return $user->createToken("API TOKEN")->plainTextToken; } else { // Handle Error. Not logged in or guard did not return a User object. }
@@rubyvidhyaammu4984 Hello, this worked for me. Is this a normal solution? public function logout(Request $request) { $request->user()->tokens()->delete(); return response()->json([ 'status' => true, 'message'=> 'User logged out', 'data' => [], ]); }