0

I have modified Login function in LoginController and this is what I have written

public function login(Request $request)
{
    $data = $request->all();
    if($this->validateLogin((array)$data))
    {
        $cred = $request->email;
        $cred_p = $request->password;

        //if()
        $user = User::select('id', 'password')->where('email','=',$cred)->first();

        if(count($user) == 1)
        {
            if(Hash::check($cred_p, $user->password))
            {
                $user_id = $user->id;

                $status = User_status::select('is_active', 'is_completed')->where('user_id','=',$user_id)-> first();

                $active = $status->is_active;
                //dd($status->is_active);
                if($active == 0)
                {
                    return redirect()->to('/login')->withErrors(['status'=>"Your account is blocked. Please contact administrator or visit here <a href='/support/acount-blocked'>Support Center</a>"]);
                }
                else
                {
                        $this->attemptLogin($request);

                        $this->sendLoginResponse($request);                
                }
            }
            else
            {
                if ($this->hasTooManyLoginAttempts($request)) {
                    $this->fireLockoutEvent($request);

                    return $this->sendLockoutResponse($request);
                }
                $this->incrementLoginAttempts($request);
                return $this->sendFailedLoginResponse($request);
            }

        }
        else
        {
            return $this->sendFailedLoginResponse($request);
        }
    }
    else
    {
        return $this->sendFailedLoginResponse($request);
    }
}

This seems to work fine and Auth::user() is working but when I add new middleware that is IsCompletedMiddleware and I check dd(Auth::user()); this returns null value.

public function handle($request, Closure $next)
{

    /*if(Auth::user()->status->is_completed == 0)
    {
        return redirect()->to('/profile/management/complete');
    }*/
    return $next($request);
}

I have also included use Auth; in the header of middleware but it always returns null value. And in other controllers Auth::user() returns proper user. Why is this so? And what is missing. Please help thank you.

Alen
  • 1,221
  • 5
  • 21
  • 43
  • try to use the global helper `auth()->user()` in your middleware – Amr Aly Aug 08 '17 at 18:04
  • Null value :( never ran into such problem I think the custom login function has something to do with it – Alen Aug 08 '17 at 18:07
  • I have searched your problem the [answer-er](https://stackoverflow.com/questions/35160144/authuser-returns-null) suggested to add the `web` middleware in order to get access to `Auth::user()` – Amr Aly Aug 08 '17 at 18:20
  • web? what is it? – Alen Aug 08 '17 at 18:24
  • you can surround your route that handle the login process `Route::group(['middleware' => ['web']], function () { // your login route })` check the [docs](https://laravel.com/docs/5.4/middleware) – Amr Aly Aug 08 '17 at 18:36
  • Possible duplicate of [Auth::user() returns null](https://stackoverflow.com/questions/35160144/authuser-returns-null) – Amr Aly Aug 08 '17 at 22:59

1 Answers1

0

In your middleware you have to get the user is doing the request, try to do this:

public function handle($request, Closure $next)
{
    $request = $next($request);
    if($request->user()->status->is_completed == 0){
        return redirect()->to('/profile/management/complete');
    }

    return $request;
}
Diego Blaker
  • 341
  • 1
  • 7