Support the ongoing development of Laravel.io →
Authentication Validation
Last updated 1 year ago.
0

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');
Last updated 1 year ago.
0

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');
	}
Last updated 1 year ago.
0

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.

0

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');

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.