Question

$cred = array(
    'username' => $post['username'],
    'password1' => md5($post['password1']),
    'password2' => md5($post['password2']) // custom third field
);

// Check all for authentication
Auth::attempt($cred);

How to add custom fields to AUTH module ?

Was it helpful?

Solution 2

You don't, instead you may try something like this:

$cred = array(
    'username' => Input::get('username'),
    'password' => Input::get('password1')
);

// At first normally check the credentials using validate method
// but doesn't login, if check succeeded then check the second
// password manually, using your encrypted password2 field
if(Auth::validate($cred)) {
    // Now check the password2, assumed you're using md5
    // hashing for this field (according to your question)
    $password2 = md5(Input::get('password2'));
    $user = User::where('username', $cred['username'])->first();
    if( $user->password2 == $password2) {
        // Now manually login the user
        Auth::login($user);
        return Redirect::to('url'); // successful login url
    }
    else{
        // Password2 missmatched
        $validator = Validator::make(array(), array())->getMessagebag();
        $validator->add('errorLogin', 'Invalid Credentials!');
        return Redirect::back()->withInput()->withError($validator);
    }
}
else {
    // Didn't pass even the first validation (username, password1)
    $validator = Validator::make(array(), array())->getMessagebag();
    $validator->add('errorLogin', 'Invalid Credentials!');
    return Redirect::back()->withInput()->withError($validator);
}

In the view you can use this to show error message on failed login:

{{ $errors->first('errorLogin') }} // Invalid Credentials!

For the first validate methos don't use any encryption, let Laravel do it as it does and for the second password you may use your own encryption.

OTHER TIPS

If you want to have the user matched with another credential/where clause, then simply pass it into the credentials array. For example:

<?php
$credentials = [
    'username' => Input::get('username'),
    'password' => Input::get('password'),
    'active'   => 1
];

if(Auth::attempt($credentials)) {
    // code here
}

If you wanted to check a confirm password, like the above suggestion, you'd want to check this first, before checking anything else, not after.

<?php
$validate = [
    'password' => Input::get('password'),
    'password_confirmation' => Input::get('password_confirmation')
];

$validator = Validator::make(
    $validate,
    ['password' => ['same:password_confirmation']]
);

// now you make a credentials array and attempt to auth

You must send an array with username and password to the Auth::attempt method

You can fill those with whatever you need though, in your case it would be something like this:

$post = Input::all(); // I assume this is how you are filling the $post variable?
Auth::attempt(array('username' => $post['username'], 'password' => $post['password1']);

Note that you do not need to hash the password, the attempt method will handle that. You also do not need to send the second password along with it, it will completely ignore everything except 'username' and 'password'

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top