1

I have a Laravel web application (website.nl) AND an Laravel API (api.website.nl).

When the user logs in with the login form on the website. A post request is done to the API. When the login request is successfull a token AND User object is send back in the form of a JSON string. The login method looks like this.

public function login(Request $request)
{
    $email = $request->input('email');
    $password = $request->input('password');

    $response = $this->client->request('POST', self::$apiRootUrl . '/login', [
        'headers'     => [
            'Accept' => 'application/json',
        ],
        'form_params' => [
            'email'    => $email,
            'password' => $password
        ],
    ]);

    $result = json_decode($response->getBody()->getContents());

    if (isset($result->success->token))
    {
        $user = User::create([
            'id'    => $result->success->user->id,
            'name'  => $result->success->user->name,
            'email' => $result->success->user->email,
            'password' => $password
        ]);
        Auth::login($user);
        $this->apiToken = $result->success->token;

        return $this->sendLoginResponse($request);
    }

    return $this->sendFailedLoginResponse($request);
}

Where I want this login function to be as stupid as possible, meaning, let the API figure out if this is a valid login request. This method only passes the arguments.

The main website should not have a database. It only uses the API to login, get a token, and do requests with that token to gather data for the user to see on the main website's dashboard.

For this reason my question is this: How can I store the User object retrieved from the API in such a manner that it will be query-able like an Eloquent model. But without the need to store it in a database. Again, the main website shouldn't need a database. It only communicates with the API for it's data.

I hope this question makes sense, if not, please ask for more details.

I don't want to create a duplicate database with migrations on the main website. Because that's the job of the .api subdomain. Also, all the models are used within a session. Never being stored for a longer time, because that's also located in a store method on the .api subdomain.

Floris
  • 2,727
  • 2
  • 27
  • 47
  • i hope this approach can help. https://stackoverflow.com/questions/33331421/custom-user-authentication-base-on-the-response-of-an-api-call at least it's work for me – Garpepi Jul 15 '20 at 10:26

1 Answers1

0

This would require you to write your own grammar for Eloquent, an ActiveRecord implementation for the API you're using. In theory it's possible, in practice it's a lot of work.

Take a look here for how Laravel implements the database types it currently supports. https://laravel.com/api/5.8/Illuminate/Database/Schema/Grammars.html

  • It should be less work than creating the duplicate tables with migrations on the main website. – Floris Feb 19 '20 at 10:42