skip to Main Content

I try build a Laravel authentication application that is based on file system storage and in database based way parallel.

I made a method, like this:

    private function fileAuth(array $credentials): void
    {
        $userFileName = config('backup_values.backup_path') . $credentials['email'] . '.json';
        $user = AuthFileUtils::readFile($userFileName);
        if (Hash::check($credentials['password'], $user->password)) {
            $loggedInUser = new User();
            foreach ($user as $key => $value) {
                $loggedInUser->{$key} = $value;
            };
            Auth::guard('web')->login($loggedInUser);
        } else {
            RateLimiter::hit($this->throttleKey());
            throw ValidationException::withMessages([
                'email' => trans('auth.failed'),
            ]);
        }
    }

I try use Auth::guard(‘web’)->login($loggedInUser) method but it call database query.
Do I need create custom provider? How can I make it in a simply way?
Please help me.

2

Answers


  1. Chosen as BEST ANSWER

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

    <?php
    
    namespace AppProviders;
    
    use IlluminateSupportFacadesAuth;
    use IlluminateSupportFacadesVite;
    use IlluminateSupportServiceProvider;
    
    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 AppProviders;
    
    use AppModelsUser;
    use ApputilsAuthFileUtils;
    use IlluminateContractsAuthAuthenticatable;
    use IlluminateContractsAuthUserProvider;
    use IlluminateSupportFacadesHash;
    
    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 ($userFromFile 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', AppModelsUser::class),
            ],
        ],
    
    

    But the user not logged in. How can I use it? The methods called in CustomUserProvider and I think result is good, but session not redirected to dashboard.


  2. I implemented the retrieveById method in my CustomUserProvider:

        public function retrieveById($identifier)
        {
            $dirPath = config('backup_values.backup_path');
            $files = scandir($dirPath);
            foreach ($files as $file) {
                $filePath = $dirPath . '/' . $file;
                if (is_file($filePath)) {
                    try {
                        $user = AuthFileUtils::readFile($filePath);
                        if ($user->id == $identifier) {
                            $locatedUser = new User();
                            foreach ($user as $key => $value) {
                                $locatedUser->{$key} = $value;
                            }
                            return $locatedUser;
                        }
                    } catch (Exception $e) {
    
                    }
                }
            }
            return null;
        }
    

    And it looks like work now.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search