F
Filament12mo ago
cakan

Filament login using API

I want to use filament for my project without database. Instead, it will use external API to fetch the data. I know that I can use Sushi library to work with data models. What I don't know how to do is login user using external API. I've managed to make the login work with Sushi, but I can't use Filament::auth()->attempt(). I have extended authenticate method of Filament login page with this code:
$data = $this->form->getState();

$uri =config('service.api.url');
$res = Http::post($uri, $data)->json();

if (!isset($res['token_data']) && !isset($res['user'])) {
$this->throwFailureValidationException();
}

$user = new User();
$user->fill($res['user']);
Filament::auth()->setUser($user);

if (!$user->canAccessPanel(Filament::getCurrentPanel())) {
Filament::auth()->logout();
$this->throwFailureValidationException();
}

session()->regenerate();

return app(LoginResponse::class);
$data = $this->form->getState();

$uri =config('service.api.url');
$res = Http::post($uri, $data)->json();

if (!isset($res['token_data']) && !isset($res['user'])) {
$this->throwFailureValidationException();
}

$user = new User();
$user->fill($res['user']);
Filament::auth()->setUser($user);

if (!$user->canAccessPanel(Filament::getCurrentPanel())) {
Filament::auth()->logout();
$this->throwFailureValidationException();
}

session()->regenerate();

return app(LoginResponse::class);
This partially works but guards don't work any more. What is the correct way of doing this?
1 Reply
toeknee
toeknee12mo ago
I don't think that will work properly. I would suggest on successful authorisation to the API you update or create a user record within filament, on that you then login as that user.

Did you find this page helpful?