3

I my session controller i code for the verify email ,login and changepassword but getting an error

ErrorException in EloquentUserProvider.php line 116: Undefined index: password.

and getting the error from line code

if (!Auth::attempt($credentials_verifiy))

<?php
namespace App\Http\Controllers;

use Request;
use Response;
//----models--------
use App\Site;
use App\Jobs;


use Auth;
use DB;
use Validator;
use Redirect;

//use Illuminate\Support\Facades\Validator;
use Illuminate\Support\MessageBag;
class SessionController extends Controller {

    public function index(){
        return Redirect::to('login')->with('alert-success', 'test awrnning message.');
    }
    public function store()
    {
             $input = Request::only('username', 'email', 'password');
             $credentials = [
                 'username' => Request::get('username'),
                 'password' => Request::get('password')
             ];

            if (!Auth::attempt($credentials))
            {
                return Redirect::back()->with('alert-danger', 'Username or password do not match.');
            }
            else
            {
                $credentials_verifiy = [
                    'verified_email' => '1'
                ];
                if (!Auth::attempt($credentials_verifiy))
                {
                    return Redirect::back()->with('alert-danger', 'Please verify your email.');
                }
                else
                {
                    if(Auth::user()->last_login_at=='' || Auth::user()->last_login_at==null)
                    {
                        return redirect('/change_password');
                    }
                    else
                    {
                        return redirect('/tests');
                    }
                }
            }
    }
}
Qirel
  • 25,449
  • 7
  • 45
  • 62
Alfiza malek
  • 994
  • 1
  • 14
  • 36
  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – LF00 Dec 13 '16 at 09:30

2 Answers2

3

In order to skip this Exception you can use this line of code

array_get('password', $input);

if the array $input has no index password it will not throw an Exception, it will return null, you can make it returns what you want : array_get('password', $input, ' ');

Bara' ayyash
  • 1,913
  • 1
  • 15
  • 25
  • can you please explain how to use in $credentials_verifiy = [ 'verified_email' => '1' ]; if (!Auth::attempt($credentials_verifiy)) { return Redirect::back()->with('alert-danger', 'Please verify your email.'); } – Alfiza malek Dec 13 '16 at 10:09
  • 1
    just replace with this : `$credentials = [ 'username' => array_get('username', $input, ' '), 'password' => array_get('password', $input, ' ') ];` – Bara' ayyash Dec 13 '16 at 10:14
  • how can icheck not null in credential. – Alfiza malek Dec 13 '16 at 10:19
  • how to 'last_login_at' field is not null with $credentials = [ 'username' => array_get('username', $input, ' '), 'password' => array_get('password', $input, ' ') ]; – Alfiza malek Dec 13 '16 at 10:20
  • try to do this `if (!Auth::attempt(array_merge($credentials_verifiy, $credentials))` – Bara' ayyash Dec 13 '16 at 10:30
1

$credentials array in Auth::attempt($credentials) should contain value with password key, that's why you're getting the error. It's hardcoded in validateCredentials() method which Auth::attempt() is using:

public function validateCredentials(UserContract $user, array $credentials)
{
    $plain = $credentials['password'];

    return $this->hasher->check($plain, $user->getAuthPassword());
}
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • but in this line if (!Auth::attempt($credentials_verifiy)) i just need to check the credential related to verified_email field only. – Alfiza malek Dec 13 '16 at 09:34