This part ['except' => ['login'] is what gives you the error because the login route is not registered. You can search your project and look for 'login' string, and delete those usages if you want to avoid the login route.
Route [login] not defined. in file
Symfony\Component\Routing\Exception\RouteNotFoundException: Route [login] not defined. in file C:\laragon\www\api\vendor\laravel\framework\src\Illuminate\Routing\UrlGenerator.php on line 444
this consruct use at controller and code is as like as jwt auth public function __construct() { $this->middleware('auth:api', ['except' => ['login']]); }
this code also show me same error - and i could not find login anywhere .. can you please tell me where 'login' string situated ?
api.php Route::post('/login',[AuthController::class,'login']); Route::resource('/user',AuthController::class)->middleware('auth');
do you want a login route or not?
@Snapey Of course i want .
find your login route and make sure it has ->name('login')
@Snapey Thanks! I just started using Laravel en this helped me!
when i use ->name('login') at login route it show me new error Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: The GET method is not supported for this route. Supported methods: POST. in file C:\laragon\www\restapi\vendor\laravel\framework\src\Illuminate\Routing\AbstractRouteCollection.php on line 117
Make sure ! you have a POST route for that endpoint.
i already use post for login route
@dipta_dey show me
AuthController
class AuthController extends Controller {
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login']]);
}
public function login(Request $request)
{
$credentials = $request->only('email', 'password');
if ($token = $this->guard()->attempt($credentials)) {
return $this->respondWithToken($token);
}
return response()->json(['error' => 'Unauthorized'], 401);
}
/**
* Get the guard to be used during authentication.
*
* @return \Illuminate\Contracts\Auth\Guard
*/
public function guard()
{
return Auth::guard();
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\JsonResponse
*/
public function index()
{
$users = User::all();
return response()->json([
'success'=>true,
'message'=> 'Display a listing of the resource.',
'data' => $users
]);
}
@dipta_dey you have to write api/login since you place your routes in the api.php file, it will always be prefixed with api
@shaungbhone you means .. ['except' => ['api/login']]
@dipta_dey htttp://localhost/api/login
@dipta_dey so if I understand correctly, you're trying to use the auth:api middleware in your AuthController except for the login method of the same controller, right?
If this is the case, I think the solution is to change this line:
$this->middleware('auth:api')->except('login');
my guards at auth.php
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
my api.php route
Route::resource('/user',AuthController::class);
Route::post('login',[AuthController::class,'login']);
route::post('login',[AuthController::class,'login'])->name('login');
login route work fine but when i use construct for authenticate i face ----->>>> Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: The GET method is not supported for this route. Supported methods: POST. in file C:\laragon\www\restapi\vendor\laravel\framework\src\Illuminate\Routing\AbstractRouteCollection.php on line 117 <<<<<<< this error again github link: https://github.com/dipta995/restapi
thing is when i comment out the constructor, everything works fine. i begin getting messages that the token is invalid. Now, my question is if or not it's okay to proceed without the constructor. In my api.php, i am not protecting the /register and /login endpoints with any auth middleware since practically i dont need them to be guarded. For matters of best practice, someone please tell me if this is okay. Thanks. Otherwise i am finding JWT being way better than sanctum and passport as regards getting stuff moving and having a clearer documentation.
if you think anything is correct use this line's code maybe you'r project has cache
Clear Route Cache:
php artisan route:clear
Clear View Cache:
php artisan cache:clear
Clear Event Cache:
php artisan event:clear
Speed LOad Page
php artisan config:clear
Speed route
php artisan route:cache
Speed view
php artisan view:cache
php artisan optimize
Just add or the header Accept : Application/json to avoid this error
Please or to participate in this conversation.