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

Krlinhos's avatar

Connect with Api

Hello!

I have two projects (Api, and an App)

From my app when the user try loggin, my method getPost call by cURL to Api, and this return a response json with a Token and object User if all was right.

object(stdClass)#291 (3) { ["status"]=> string(2) "ok" ["user"]=> object(stdClass)#292 (13) { ["_id"]=> string(1) "1" ["name"]=> string(6) "Carlos"  ["password"]=> string(60) "$2y$10$DKcfWRA5zZ1IB8wwt.7LGe055emuoml2GabwXOKSHcxaafZb.6oIi" ["rol"]=> string(5) "super" } ["token"]=> string(333) "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIiwiaXNzIjoiaHR0cDpcL1wvYXBpLmxvY2FscGxheW1vLmVzXC92MVwvYXV0aGVudGljYXRlIiwiaWF0IjoiMTQ0MzczNzAxMSIsImV4cCI6IjE0NDM3NDA2MTEiLCJuYmYiOiIxNDQzNzM3MDExIiwianRpIjoiMmEzNmFkM2E1YmZmYzFiY2RkZTQ2NmQyMmZiMWIzYjciLCJyb2wiOiJzdXBlciIsIm5hbWUiOm51bGx9.tfsq6LXHA4ekPDQmOS5Ym0CU8m5e2TFdE7reMNjfGN8" } 

But I dont know how create a session when the response was good. I try this:

Auth::login($response->user);

But not work, I recieve this error

Argument 1 passed to Illuminate\Auth\Guard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of stdClass given

Someone can help me?

Thanks!

0 likes
6 replies
handy_man's avatar

Could just login the user using:

        Auth::loginUsingId($response->_id);
Krlinhos's avatar

But this function retrieve a user from table in bd. And I dont want that my app connect with the DB, only must connect through the Api.

Do you understand me? In my app there isn't a config to connect whit these DB. All connections must be through a cURL to API and so retrieve data

bobbybouwmann's avatar

The Auth::login function expects an object that implements the Authenticatable interface. The User model for example uses this interface.

You can either create your own authentication stuff, so create your own sessions and cookies.

Or create a class that extends the Authenticatable interface and pass the data to that class. This is much harder since it's used throughout Laravel!

1 like
Krlinhos's avatar
Krlinhos
OP
Best Answer
Level 1

@bobbybouwmann I thought about what you said and my solution is create a function in User model that recieve an array and return a user objetc

public function getUser($user) 
    {
        $userContract = new User();

        $userContract->_id = $user->_id;
        $userContract->name = $user->name;
        $userContract->rol = $user->rol;
        $userContract->email = $user->email;

        return $userContract;
    }

What do you think?

thanks!

1 like
bobbybouwmann's avatar

Looks fine to me ;) If you need to add more stuff to this, I suggest you to refactor it to it's own class that extends the User class ;)

1 like
thepsion5's avatar

If you want to implement a custom login via an API, you'll really need to set up your own UserProvider implementation that uses the API as it's equivalent of a data store. I tinkered with something similar while investigating building an SSO system for Laravel. The code was something like this:

class SsoUserProvider implements UserProvider
{
    private $api; //class that uses Guzzle and a configured API url to perform the user lookups

    private $factory; //converts the API user instances to Laravel user instances

    public function __construct(AuthApi $authApi, UserFactory $factory)
    {
        $this->api = $authApi;
        $this->factory = $factory;
    }

    public function retrieveById($identifier)
    {
        $baseUser = $this->api->getUserByEmail($identifier);
        return $this->factory->create($baseUser, ['password' => '']);
    }

    public function retrieveByToken($identifier, $token)
    {
        //TODO: Implement
        throw new \BadMethodCallException('retrieveByToken is not implemented by this driver.');
    }

    public function updateRememberToken(Authenticatable $user, $token)
    {
        //TODO: Implement
    }

    public function retrieveByCredentials(array $credentials)
    {
        $email = isset($credentials['email']) ? $credentials['email'] : null;
        $password = isset($credentials['password']) ? $credentials['password'] : null;
        $baseUser = $this->api->getUserByCredentials($email, $password, $credentials);
        if($baseUser === null) {
            return null;
        }
        return $this->factory->create($baseUser, ['password' => $password]);
    }

    public function validateCredentials(Authenticatable $user, array $credentials)
    {
        $email = isset($credentials['email']) ? $credentials['email'] : null;
        $password = isset($credentials['password']) ? $credentials['password'] : null;
        //the email is used as the primary identifier for the SSO server
        return ($user->getAuthIdentifier() === $email && $user->getAuthPassword() === $password);
    }
}

Please or to participate in this conversation.