79244633

Date: 2024-12-02 15:31:34
Score: 2
Natty:
Report link

I made a custom provider and I registered it like this:

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Vite;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        //
    }

    public function boot(): void
    {
        Vite::prefetch(concurrency: 3);

        Auth::provider('custom', function ($app, array $config) {
            return new CustomUserProvider();
        });
    }
}

The custom provider looks like this:

<?php

namespace App\Providers;

use App\Models\User;
use App\utils\AuthFileUtils;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Support\Facades\Hash;

class CustomUserProvider implements UserProvider
{

    public function retrieveById($identifier)
    {

    }

    public function retrieveByToken($identifier, #[\SensitiveParameter] $token)
    {

    }

    public function updateRememberToken(Authenticatable $user, #[\SensitiveParameter] $token)
    {

    }

    public function retrieveByCredentials(#[\SensitiveParameter] array $credentials)
    {
        $fileName = config('backup_values.backup_path') . $credentials['email'] . '.json';
        $userFromFile = AuthFileUtils::readFile($fileName);
        $user = new User();
        foreach ($credentials as $key => $value) {
            $user->$key = $value;
        }
        return $user;
    }

    public function validateCredentials(Authenticatable $user, #[\SensitiveParameter] array $credentials)
    {
        $result = Hash::check($credentials['password'], $user->getAuthPassword());
        return $result;
    }

    public function rehashPasswordIfRequired(Authenticatable $user, #[\SensitiveParameter] array $credentials, bool $force = false)
    {
        $result = $credentials['password'];
        return hash::make($result);
    }
}

And I set it in auth.php like this:

    'providers' => [
        'users' => [
            'driver' => 'custom',
            'model' => env('AUTH_MODEL', App\Models\User::class),
        ],
    ],

But the user not logged in. How can I use it?

Reasons:
  • Blacklisted phrase (0.5): How can I
  • Long answer (-1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Papp Zoltán