Jonathan Tyler
Jonathan Tyler
FFilament
Created by kenromdavids on 6/5/2023 in #❓┊help
Ldaprecord with filament
namespace App\Filament\Auth;

use Filament\Pages\Auth\Login as BaseAuth;
#...

class Login extends BaseAuth
{
public function form(Form $form): Form
{
#...
}

protected function getUsernameFormComponent(): Component
{
return TextInput::make('username')
->label('Username or Email')
->required()
->autocomplete()
->autofocus()
->extraInputAttributes(['tabindex' => 1]);
}

protected function getCredentialsFromFormData(array $data): array
{
$ldapKey = "samaccountname";
$localKey = "username";

if (filter_var($data['username'], FILTER_VALIDATE_EMAIL)) {
$ldapKey = "mail";
$localKey = "email";
}

return [
$ldapKey => $data['username'],
'password' => $data['password'],
'fallback' => [
$localKey => $data['username'],
'password' => $data['password'],
],
];
}

public function authenticate(): ?LoginResponse
{
#...

if (!Filament::auth()->attempt($this->getCredentialsFromFormData($data), $data['remember'])) {
$this->addError('username', __('filament::login.messages.failed'));

return null;
}

return app(LoginResponse::class);
}
}
namespace App\Filament\Auth;

use Filament\Pages\Auth\Login as BaseAuth;
#...

class Login extends BaseAuth
{
public function form(Form $form): Form
{
#...
}

protected function getUsernameFormComponent(): Component
{
return TextInput::make('username')
->label('Username or Email')
->required()
->autocomplete()
->autofocus()
->extraInputAttributes(['tabindex' => 1]);
}

protected function getCredentialsFromFormData(array $data): array
{
$ldapKey = "samaccountname";
$localKey = "username";

if (filter_var($data['username'], FILTER_VALIDATE_EMAIL)) {
$ldapKey = "mail";
$localKey = "email";
}

return [
$ldapKey => $data['username'],
'password' => $data['password'],
'fallback' => [
$localKey => $data['username'],
'password' => $data['password'],
],
];
}

public function authenticate(): ?LoginResponse
{
#...

if (!Filament::auth()->attempt($this->getCredentialsFromFormData($data), $data['remember'])) {
$this->addError('username', __('filament::login.messages.failed'));

return null;
}

return app(LoginResponse::class);
}
}
23 replies
FFilament
Created by kenromdavids on 6/5/2023 in #❓┊help
Ldaprecord with filament
Hmm, never mind... it appears my migrations weren't updating users table correctly. After dropping and re-creating I can now login with username or email (ldap or local). I've modified my custom Login class (similar to @kenro's setup), see next reply. Specifically Filament::auth()->attempt() is using the getCredentialsFromFormData, with logic to determine if username or email, and I added the 'tabindex' on the username field as it was skipping the password field when tabbing over. Many thanks to @kenro for your post putting me on the right direction!!
23 replies
FFilament
Created by kenromdavids on 6/5/2023 in #❓┊help
Ldaprecord with filament
I'm using email instead of username as I wanted both local and LDAP auth. I got this working similar to above, just replacing the getCredentialsFromFormData function, however I tried to do with fallback (as is done with Fortify, etc) but it's not working as expected. Local auth just doesn't work. Any thoughts as to why fallback doesn't work here?
protected function getCredentialsFromFormData(array $data): array
{
return [
'mail' => $data['email'],
'password' => $data['password'],
'fallback' => [
'email' => $data['email'],
'password' => $data['password'],
],
];
}
protected function getCredentialsFromFormData(array $data): array
{
return [
'mail' => $data['email'],
'password' => $data['password'],
'fallback' => [
'email' => $data['email'],
'password' => $data['password'],
],
];
}
23 replies