Logging In
Important: Before we begin, it's paramount to understand that LdapRecord does not support credentialess authentication (unless your server supports anonymous binding). If you attempt to configure an LDAP connection without credentials, your user will be logged out after the request ends. Please see the configuration guide for more information.
Once you have configured a new authentication provider, you're ready to start logging users into your application.
Logging in is simple -- you only need to use Laravel's Auth
facade:
use Illuminate\Support\Facades\Auth;
$credentials = [
'mail' => '[email protected]',
'password' => 'password',
];
if (Auth::attempt($credentials)) {
$user = Auth::user();
return redirect('/dashboard')->with([
'message' => "Welcome back, {$user->getName()}"
]);
}
As you can see above, we set the mail
key which is passed to the LdapRecord authentication provider.
A search query will be executed on your LDAP directory for a user that contains the mail
attribute
equal to the entered email address. The password
key will not be used in the search.
If you wish to log in a user by their username instead, simply change the mail
key
to whichever attribute you would like to locate the user by in your LDAP directory.
For example, samaccountname
:
use Illuminate\Support\Facades\Auth;
$credentials = [
'samaccountname' => 'jdoe',
'password' => 'password',
];
if (Auth::attempt($credentials)) {
//
}