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
I made a custom provider and I registered it like this:
The custom provider looks like this:
And I set it in auth.php like this:
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.
I implemented the retrieveById method in my CustomUserProvider:
And it looks like work now.