0
votes

I wrote an API for User Login using Laravel 5.8

When I tested the Login on Postman, it generated an error.

Status: 404 Not Found.

The File Path is Http->Controllers->UserController->Login

The project is laravelapi. I used POST in Postman and added this:

http://localhost/laravelapi/public/user/api/login

But when I run php artisan serve and used:

http://localhost:8000/api/login

Status was OK.

Controller

public function login(Request $request)
{
    $credentials = $request->json()->all();

    try {
        if(! $token == JWTAuth::attempt($credentials)) {
            return response()->json(['error' => 'invalid_credentials'], 400);
        }
    } catch(JWTException $e) {
            return response()->json(['error' => 'could_not_create_token'], 500);
    }

    return response()->json(compact('token'));
}   

api.php

use Illuminate\Http\Request;

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::post('login', 'UserController@login');

What do I do to make http://localhost/laravelapi/public/user/api/login work and have Status: OK

Thanks.

1
It works like magic. Thanks a lotuser11352561

1 Answers

0
votes

link must be localhost/laravelapi/public/api/login