简体   繁体   中英

Laravel 5.2 Login With Condition

So, I have users table which is default by laravel. But I add a new column named 'status'.
So the columns on the users table are id, name, email, password, remember_token, created_at, updated_at, status
The status values are between 0 and 1. 1 for admin, and 0 for regular user.
Also I have auth, but the problem is how can I check if users' status is 0 or 1 when login so that I can redirect it according to their status? -- admin will go to admin panel and user will go to home

User default by laravel with addition 'status'

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

    class User extends Authenticatable
    {
        use Notifiable;
        protected $fillable = [
            'name', 'email', 'password', 'status',
        ];
        protected $hidden = [
            'password', 'remember_token',
        ];
    }

HomeController also default by laravel

use Illuminate\Http\Request;

class HomeController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        return view('home');
    }
}

LoginController also default by laravel

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    use AuthenticatesUsers;
    protected $redirectTo = '/home';

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}

THANKS.

如果它全部是默认的laravel身份验证,那么您应该能够

( Auth::user()->status === 0 ) ? 'do something' : 'do something else';

By default, The laravel redirect to "authenticated" function after login. So you can add "authenticated" function in "LoginController". In the "authenticated" function will get the user object.

protected function authenticated(Request $request, $user)
{
    if (!$user->status == 1) {
        return redirect('admin');
    }
    return redirect('web');
}

You can override the authenticated() method in App\\Http\\Controllers\\Auth\\LoginController to add logic after the user is authenticated:

   protected function authenticated($request,$user)
    {
        if(\Auth::user()->status){
            return redirect('/admin'); 
        }

        return redirect('/home');   
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM