I come here because I forgot to give the solution if someone else falls on the same worries.
use Cartalyst\Sentry\Users\Eloquent\User;
...
$u = new User();
$u->setLoginAttributeName('first_name');
this is mine:
try
{
$username = Input::get('username');
$password = Input::get('password');
if(str_contains($username, '@'))
{
Config::set('cartalyst/sentry::users.login_attribute', 'email');
$credentials = array(
'email' => $username,
'password' => $password,
);
}
else
{
Config::set('cartalyst/sentry::users.login_attribute', 'username');
$credentials = array(
'username' => $username,
'password' => $password,
);
}
// Try to authenticate the user
$user = Sentry::authenticate($credentials, false);
return Redirect::intended('app/dashboard');
}
catch (Cartalyst\Sentry\Users\LoginRequiredException $e)
{
echo 'Login field is required.';
}
catch (Cartalyst\Sentry\Users\PasswordRequiredException $e)
{
echo 'Password field is required.';
}
catch (Cartalyst\Sentry\Users\WrongPasswordException $e)
{
echo 'Wrong password, try again.';
}
catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
{
echo 'User was not found.';
}
catch (Cartalyst\Sentry\Users\UserNotActivatedException $e)
{
echo 'User is not activated.';
return Redirect::back()->with('failed', 'Gagal untuk log masuk');
}
it's simplest way
http://stackoverflow.com/questions/20225054/laravel-the-login-attribute-is-required-sentry2-auth
Actually, @prgmsaha, that does not address the problem here.
@dontspamagain that is brilliant!
$u = new \User();
$u->setLoginAttributeName('email');
This works perfectly.
I'm transitioning users from usernames to email addresses, so I want to check both. This is working great.
try
{
// First try username. (Default login attribute.)
$user = Sentry::authenticate(Input::only('username', 'password'), Input::has('remember'));
}
catch (Exception $e)
{
try
{
// Next try email address. Brilliant!
$u = new \User();
$u->setLoginAttributeName('email');
// Try again with email address.
$credentials = Input::only('username', 'password');
$credentials['email'] = $credentials['username'];
unset($credentials['username']);
$user = Sentry::authenticate($credentials, Input::has('remember'));
}
catch (Exception $e)
{
// ... let the user know they were unsuccessful
}
}
Thanks so much. This is such a simple answer. I was digging around in the sentry code for an hour trying to decide how to solve this problem. Dreading the possible need to extend the class.
dontspamagain said:
I come here because I forgot to give the solution if someone else falls on the same worries.
use Cartalyst\Sentry\Users\Eloquent\User;
...
$u = new User(); $u->setLoginAttributeName('first_name');
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community