Search code examples
phplaravelcartalyst-sentry

Laravel / Sentry login_attribute not changing


I am trying to implement the ability for a user to login either with their email or a username. At present I have the email login working with the code below, as that is the default set in the app/config/packages/cartalyst/sentry/config.php page.

The code I have currently is:

$email = Input::get('email');
$password = Input::get('password');
$remember = Input::get('remember_me');

$field = filter_var(Input::get('email'), FILTER_VALIDATE_EMAIL) ? 'email' : 'username';       
Config::set('Cartalyst/Sentry::users.login_attribute', $field);

if(Sentry::authenticate(array($field => $email, 'password' => $password), $remember == 'yes'))
    $destination = Redirect::intended('/');

However it keeps throwing the LoginRequiredException provided by Sentry.

However if I change the login_attribute in the config.php page to username then the login for that works fine but the email side of things stop.

If anyone can shed some light as to why this would be failing? I've added a line to extract the current value of login_attribute and that works and changes as anticipated but regardless the exception is still thrown.


Solution

  • The config item is used to set the login attribute at run time, not when actually processing a log in. You can see that being done in the Sentry service provider. Changing the config item will not affect the actual login attribute name being used, which is a static property on Sentry's user model.

    You should be able to replace the Config::set() command with something like SentryUser::setLoginAttributeName($field) (modified for your exact situation) and achieve the results you desire.