Hi all, I'm fairly acquainted with the basics of Laravel and while my app is looking quite good, I can't wrap my head around implementing API protection with Laravel's auth functionality in this scenario: the username and password is sent through a HTTP request to an external API, which returns a response on whether the user has entered valid credentials. My Laravel app also has some API routes that must only be accessible to authenticated users. I also have to protect certain web routes, and need to implement sessions/cookies.
I looked around everywhere but it seemed that most of the tutorials/questions involved setting up a database that had usernames and passwords, and manually registering users. I saw some answers that said to create a custom user provider and a service provider, but after that I'm not sure where to go, and would like advice. Here is an example of what I have now:
MyUserProvider.php
class MyUserProvider implements UserProvider
{
public function retrieveById($identifier) {
// Retrieve a user by their unique identifier.
}
public function retrieveByToken($identifier, $token) {
// Retrieve a user by their unique identifier and "remember me" token.
}
public function updateRememberToken(Authenticatable $user, $token) {
// Update the "remember me" token for the given user in storage.
}
public function retrieveByCredentials(array $credentials) {
// Retrieve a user by the given credentials.
}
public function validateCredentials(Authenticatable $user, array $credentials) {
$ch = curl_init();
// Make http post request here
if ($credentials_are_valid) {
return true;
}
return false;
}
}
MyAuthServiceProvider.php
class MyAuthServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Auth::provider('my-auth', function ($app, array $config) {
return new MyUserProvider();
});
}
}
and in my config/auth.php I have registered my user provider under providers.
I have looked into Passport but it seems the official docs utilized a database with users and passwords, and it didn't seem to have much advice for my scenario, where I don't have access to username and passwords. I am thinking to just do it without any Laravel help, like setting up a redis database and storing cookies in there, but I am always open to doing things the Laravel way.