iamj
iamj

Reputation: 183

Laravel 5.4 (with JWT) - NotFoundHttpException in RouteCollection.php line 161

I'm learning Laravel and encountered this error.

NotFoundHttpException in RouteCollection.php line 161:

Yes I already search solutions from the web but none of them fixed my problem I have other routes in the same project that are working, only this authentication route is not working.

AuthenticateController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use JWTAuth;
use App\Http\Controllers\Controller;
use Tymon\JWTAuth\Exceptions\JWTException;

class AuthenticateController extends Controller{

public function __construct(){
    $this->middleware('jwt.auth',['except'=>['authenticate']]);
}

public function index(){
    return "Auth index";
}

public function authenticate(Request $request){
    $credentials =$request->only('phonenumber','generatednexmotoken');
    try{
        //verify the credentials and create a token for the user_error
        if($token=JWTAuth::attempt($credentials)){
            return response()->json(['error'=>'invalid_credentials']);
        }else{
            return response()->json(compact('token'));
        }
    }catch(Tymon\JWTAuth\Exceptions\JWTException $e){
        return response()->json(['error'=>'could not create toke']);
    }
}

public function getAuthenticatedUser(){
    try{
        if(! $user = JWTAuth::parseToken()->authenticate()){
            return responnse()->json(['user not found',404]);
        }else{
            return response()->json(compact('user'));
        }
    }catch(Tymon\JWTAuth\Exceptions\TokenExpiredException $e){
        return response()->json(['token expired'],$e->getStatusCode());
    }catch(Tymon\JWTAuth\Exceptions\TokenInvalidExceptions $e){
        return response()->json(['invalid token'],$e->getStatusCode());
    }catch(Tymon\JWTAuth\Exceptions\JWTException $e){
        return response()->json(['token is absent lol'],$e-  
      >getStatusCode());        
    }
}
}

User.php

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'phonenumber', 'generatednexmotoken',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'generatednexmotoken', 'jwttoken',
];
}

api.php

Route::group(['prefix' => 'v1'], function()
{
Route::post('authenticate', 'AuthenticateController@authenticate');
Route::get('authenticate/user',
'AuthenticateController@getAuthenticatedUser');        
});

 Route::get('/sms/send/{to}', function(\Nexmo\Client $nexmo, $to){
 $message = $nexmo->message()->send([
    'to' => $to,
    'from' => '@leggetter',
    'text' => 'Sending SMS from Laravel. Woohoo!'
 ]);
  Log::info('sent message: ' . $message['message-id']);
 });

Note:

  1. I'm just using my local machine(php artisan serve)

  2. api/sms/send/{to} is working.

  3. But if I'm going to access api/v1/authenticate it will throw a NotFoundHttpException in RouteCollection.php line 161 error.

Thank you

Upvotes: 2

Views: 967

Answers (1)

Vaibhavraj Roham
Vaibhavraj Roham

Reputation: 1165

How you are accessing api/v1/authenticate.? (Browser or else like postman or frontend). Your code seems correct but chance of error is if you are trying to access api/v1/authenticate api via GET method. Can you provide how you are accessing it?

Upvotes: 1

Related Questions