How to login into a Laravel app using cURL

I want to setup a small file of cURL requests so I can quickly test my Laravel app's API quickly.

Unfortunately, I'm stuck at logging in :D.

This is what I have so far:

curl -H "Accept: application/json" -d @./curl/user.json http://localhost:8000/login -o ./curl/response.json -v

I have a user.json file that has my login info:

{
    "email": "[email protected]",
    "password": "password",
}

And I would like to output the data into my response.json file.

Nothing is working though. I have a poor understanding of a couple of these technologies. Anyone able to hold my hand through this?

With the -v tag, I'm getting:

 connect to ::1 port 8000 failed: Connection refused
*   Trying 127.0.0.1:8000...
* Connected to localhost (127.0.0.1) port 8000 (#0)
> POST /login HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.77.0
> Accept: application/json
> Content-Length: 81
> Content-Type: application/x-www-form-urlencoded
>
} [81 bytes data]
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Host: localhost:8000
< Date: Mon, 17 Jan 2022 01:47:53 GMT
< Connection: close
< X-Powered-By: PHP/7.4.27
< Cache-Control: no-cache, private
< Date: Mon, 17 Jan 2022 01:47:53 GMT
< Content-Type: application/json
< Set-Cookie: laravel_session=eyJpdiI6IkpoUkhoQnNWdEVhYUdGU2wzckg5c3c9PSIsInZhbHVlIjoidGwrUHpBcDV4Z2lXNWdObmQrdER2OUp0aEIveXhpdFNREmovedSomeSTufffForSecurityPurposesmNyb3oiLCJtYWMiOiIyZTY2Yzk1MWY3MDA3M2I3NDkzMmQzMTUwMjcyNDFmMTU3MTU
0MzRmZjAzNDBjZmZmZTgwMjg1MjMzOThkZmU5IiwidGFnIjoiIn0%3D; expires=Mon, 17-Jan-2022 03:47:53 GMT; Max-Age=7200; path=/; httponly; samesite=lax
<

Any idea how I can login to a Larvel app using cURL? The goal is to make it work like Postman, but in the terminal!


I want to setup a small file of cURL requests so I can quickly test my Laravel app's API quickly.

Any idea how I can login to a Larvel app using cURL? The goal is to make it work like Postman, but in the terminal!

Login using Laravel's default authentication page and API authentication are two different things. I can't explain to you about API authentication in depth, as it would be quite lengthy.

Laravel has two packages that you can use for this case, Passport and Sanctum.

Sanctum is automatically installed when you install Laravel.

Since middleware on web is different from API, you should focus on route routes/api.php. You can create a token generation route (same as login). For example :

Route::post('/token', function (Request $request) {

    // You can replace it with your login logic
    $user = User::first();
    $token = $user->createToken('Your Awesome Token Name');

    return ['token' => $token->plainTextToken];

});

Return :

{
  "token": "1|jLw1HhWJhSVQq81VFZwhxYB93GKMl5JRCrtuYQ36"
}

Then, the generated token you can use to access other API routes.

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

Calling via cURL :

curl -i http://your-awesome-app.test/api/user -H "Authorization: Bearer <TOKEN>"

Change <TOKEN> :

curl -i http://your-awesome-app.test/api/user -H "Authorization: Bearer 1|jLw1HhWJhSVQq81VFZwhxYB93GKMl5JRCrtuYQ36"

Viola!!!

{
  "id":1,
  "name":"Your Awesome User",
  "email":"[email protected]",
  "email_verified_at":"2022-01-17T03:03:58.000000Z",
  "created_at":"2022-01-17T03:03:58.000000Z",
  "updated_at":"2022-01-17T03:03:58.000000Z"
}