Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

david19's avatar

General API Question

I have question. When i call a API Request from Laravel 1 to Laravel 2 for Login.

After that i go in Web Browser to Laravel 2, i am Login in the session? I think not, because API is a different „guard“. For this i need a package, PHP-JWT i think.

0 likes
6 replies
LaryAI's avatar
Level 58

Yes, you are correct. You will need to use a package like PHP-JWT to authenticate the user in the second Laravel application. You can use the JWT package to generate a token when the user logs in to the first application. Then, when the user makes a request to the second application, you can pass the token in the request header and use the JWT package to authenticate the user.

Here is an example of how you can generate a token in the first application:

use \Firebase\JWT\JWT;

$key = "example_key";
$token = array(
    "iss" => "http://example.org",
    "aud" => "http://example.com",
    "iat" => 1356999524,
    "nbf" => 1357000000
);

$jwt = JWT::encode($token, $key);

And here is an example of how you can authenticate the user in the second application:

use \Firebase\JWT\JWT;

$key = "example_key";
$jwt = $_SERVER['HTTP_AUTHORIZATION'];

try {
    $decoded = JWT::decode($jwt, $key, array('HS256'));
    // User is authenticated
} catch (Exception $e) {
    // User is not authenticated
}
david19's avatar

@MohamedTammam Yes, for the API Call to Laravel 2. But when i go to Lara2 in the Webrowser i am not Loged in..?

david19's avatar

@MohamedTammam No, would like seperate Databases. Because i dont like problems with migrations or Load Balancing later …

MohamedTammam's avatar

@david19 I think, you're right. JWT would be good option. However, I don't know how you're going to handle authorization for different apps.

1 like

Please or to participate in this conversation.