dipta_dey's avatar

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']]); }

0 likes
21 replies
Nakov's avatar

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.

1 like
dipta_dey's avatar

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');

Snapey's avatar

do you want a login route or not?

3 likes
Snapey's avatar

find your login route and make sure it has ->name('login')

3 likes
dipta_dey's avatar

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

shaungbhone's avatar

Make sure ! you have a POST route for that endpoint.

1 like
dipta_dey's avatar

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
    ]);
}
shaungbhone's avatar

@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

1 like
AndrykVP's avatar

@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');
dipta_dey's avatar

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']);

Snapey's avatar
route::post('login',[AuthController::class,'login'])->name('login');
dipta_dey's avatar

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

cyber1408's avatar

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.

Lara_Love's avatar

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
Jphilippe's avatar

Just add or the header Accept : Application/json to avoid this error

1 like

Please or to participate in this conversation.