Search code examples
phplaravelauthenticationlaravel-5middleware

Insert "status" data to database in AuthController


I want to make user only have 1 login. I already make active column in database with boolean data type.
And then, I use this Auth::user()->active = true; to insert the data to database but it seems didn't work when I try to Login.

This is in my AuthController:

protected function authenticated(){
    if (!Auth::user()->active) {
        if(Auth::user()->role == 'Admin') {
            Auth::user()->active = true;
            return redirect('/listUser');
        } elseif (Auth::user()->role == 'CCO'){
            Auth::user()->active = true;
            return redirect('/eCalendar');
        }
        Auth::user()->active = true;
        return redirect('/eCalendars');
    } else{
        Auth::logout();
        return redirect('/login');
    }
}

public function logout()
{
    Auth::user()->active = false;
    Auth::logout();
    return redirect('/login');
}

If I try to login, the active column in database won't change. But if logout it success to change to false.


Solution

  • you need to save the data first after changing your attribute value as

    Auth::user()->active = true;
    Auth::user()->save();