3

I am doing project in laravel 5.2. For authentication, I am using API Token Authentication from Laravel 5.2. I have referred https://gistlog.co/JacobBennett/090369fbab0b31130b51 and my code looks like,

Routes.php

Route::post('login/','UserController@login');
 Route::group(['prefix' => 'api/v1', 'middleware' => 'auth:api'], function () {
    Route::get('/', function () {
        return "Hi";
    });
});

and in UserController.php I did simple login as follows,

    public function login(Request $request){
        $email = $request->input('email');
        $user = DB::table('users')->where('email',$email)->first();
        return $user->api_token;
    }

I am using postman and first I did login and it returns me the correct api_token from table but when I try to access urls within middleware then it throws an error page as,

Sorry, the page you are looking for could not be found.

1/1
NotFoundHttpException in RouteCollection.php line 161:

I tried, localhost:8888/ as well as

localhost:8888/api_token=6CnUsIKlmwHXYQNFAuhUTDweUe707gJU2nM2j1Kwjn80nFgmaJHGXuAdN3BX

But still it shows same error page.

2 Answers 2

0

Try to change your route / for this code in your Routes.php:

Route::post('login/','UserController@login');
Route::group(['prefix' => 'api/v1', 'middleware' => 'auth:api'], function () {
    Route::get('/', array('as'=>'api_login', 'uses' => 'UserController@api_login'));
});

And in your UserController.php add:

public function api_login(){
    return "Hi";
}

I use the Auth class, not an api and if i put the code in Routes.php it didn't work, but if I put the same code inside a Controller and in my Routes.php add the 'uses', it works.

I hope that it works for you too.

Good Luck!

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for your reply. Can you tell me, what should be my route? I am using postman
The Route may be the same
I got solution, my route should be as, localhost:8888/api/v1?api_token=6CnUsIKlmwHXYQNFAuhUTDweUe707gJU2nM2j1Kwjn80nFgmaJHGXuAdN3BX
Perfect, happy to have helped if you found your solution. Regards!
0

If it helps anyone that googles this issue, I had the same problem and after hours of playing about with it realised it was because I had no api_tokens in my database, once I added it was fine, just weird behaviour that it gives 404 when api_token is not valid.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.