Two Laravel projects, API and Front (Web). How to "login" the user?

Posted 5 months ago by luddinus

Hi.

I'm making an API, and I will use laravel for that. No problemo here.

I want to do TWO completely separate projects. I want to use simple blade pages in the front.

The main problem in my mind:

  • How to "login" a user?

I would have an endpoint like domain.com/api/auth and when I login successfuly, a token will be return from the API (maybe some additional info like user name etc.)

The code in the FRONT project

// LoginController
public function store()
{
    // guzzle?
    $response = $http->post('https://domain.com/api/auth', [
        'email' => '[email protected]',
        'password' => 'secret'
    ]);

    // $response ---> ['token' => '...', 'user' => ['name' => '...', 'email' => '...']];

    // successfull
    // save the token in my DB? (Database of the Front End project, only to save the login users for example?)

    return redirect('dashboard');
}

Then I guess I would only have to do the requests with the saved token to the API

// ProfileController

public function update()
{
    // ...
    $response = $http->put('https://domain.com/api/me', [
        'token' => 'Saved-token-from-the-db',
        'some_data' => 'to update the profile'
    ]);

    return back();
}

Is this a good way? Any ideas?

Thanks!

Please sign in or create an account to participate in this conversation.