1

I want to check user type when they log in and apply function

In my middleware, I have this code

public function handle($request, Closure $next)
{
    $user = Auth::user();
    if ($user->isBasic()) {
        $previous_session = $user->session_id;

        if ($previous_session) {
            \Session::getHandler()->destroy($previous_session);
        }

        Auth::user()->session_id = \Session::getId();
        Auth::user()->save();
        return redirect(route('home'));
    }

    return $next($request);
}

In my User model, I have this

public function isBasic()
{
    return $this->role=='basic';
}

I have already registered the middleware in Kernel

'basic' => \App\Http\Middleware\CheckSession::class,

And I passed it to my LoginController like this

public function __construct()
{
    $this->middleware('basic');
}

But when it tried to visit the login controller, it says

Call to a member function isBasic() on null

I am a beginner in Laravel and I don't know what to do

lagbox
  • 48,571
  • 8
  • 72
  • 83
xtremeCODE
  • 679
  • 1
  • 10
  • 26
  • 2
    "before login" would mean there is no authenticated user yet, because they haven't logged in, so what would you be checking since there is no user to check? – lagbox Dec 05 '19 at 09:32
  • I am new in Laravel, so what I want is to be able to check while they log in, if user type is basic, the function in the middleware should be called. I know I am doing something wrong, that's why I came here for help. I tried using the function in controller, but it applies to all user type – xtremeCODE Dec 05 '19 at 09:35
  • this is something you would have to check in the login functionality itself, something like the `authenticated` method that gets called **after** they are logged in in the LoginController – lagbox Dec 05 '19 at 09:37
  • you could use $this->middleware('basic')->except([''showLoginForm' , 'login']); you are checking for authenticated user while no user has login to your app – Anuj Shrestha Dec 05 '19 at 09:44

2 Answers2

1

you should use Auth::check() to determine if user is authenticated or not

in your middleware, you are doing

 $user = Auth::user();
    if ($user->isBasic()) {...}

In this part, if the user is not authenticated then the $user will have null value you will have to check if the user is authenticated or not.

Eg:

public function handle($request, Closure $next)
{
    if(Auth::check()) { //check if the user is logged in or not
        $user = Auth::user();
        if ($user->isBasic()) {
            $previous_session = $user->session_id;

            if ($previous_session) {
                \Session::getHandler()->destroy($previous_session);
            }

            Auth::user()->session_id = \Session::getId();
            Auth::user()->save();
            return redirect(route('home'));
        }
    }

    return $next($request);
}
lagbox
  • 48,571
  • 8
  • 72
  • 83
Anuj Shrestha
  • 966
  • 6
  • 18
1

This seems like a good candidate for the authenticated method of the LoginController which gets called after a user has logged in.

use Illuminate\Http\Request;


/**
 * The user has been authenticated.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  mixed  $user
 * @return mixed
 */
protected function authenticated(Request $request, $user)
{
    if ($user->isBasic()) {
        $previous_session = $user->session_id;

        if ($previous_session) {
            $request->session()->getHandler()->destroy($previous_session);
        }

        $user->session_id = $requset->session()->getId();
        $user->save();

        // do these specific users need to be redirected to somewhere special?
        return redirect()->route('home');
    }
}

You could even listen for the Auth events if you wanted to, as long as its not a queued listener.

lagbox
  • 48,571
  • 8
  • 72
  • 83