简体   繁体   中英

What is the best way to check active status for user whille loging in native multi-auth Laravel?

I am using laravel multi-auth. I have a table column called status. At the time of login in, I want compare user whether it is active or in active. If active only login and if not give a message 'inactive account, please contacct to administrator'.

Here is my login controller.

    <?php
    namespace Modules\University\Http\Controllers;  

    class LoginController extends Controller
    {
        protected $redirectTo = '/university';

        public function __construct()
        {
            $this->middleware('guest:university', ['except' => ['universityLogout']]);
        }

        public function showLoginForm()
        {

            return view('university::login');
        }

        public function login(Request $request)
        {
            $this->validate($request, [
                'email' => 'required|email',
                'password' => 'required|min:6',
            ]);

            //attempt to log the user in
            if(Auth::guard('university')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)){

/****Here I want check whether university status is active or not and give message if not else login*****/

                return redirect()->intended(route('university.dashboard'));
            }


            Session::flash('failed', 'Login credential incorrect!');
            return redirect()
                ->back()
                ->withInput($request->only('email', 'remember'));
        }

        public function universityLogout(Request $request)
        {
            Auth::guard('university')->logout();
            return redirect(route('university.login'));
        }
    }

Thanks in advance.

     if(Auth::guard('university')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)){

 if( Auth::guard('university')->user()->status == 'inactive')
{

return redirect()->route('university.dashboard-inactive');
}

                    return redirect()->intended(route('university.dashboard'));
                }

If you want to check before login is attempted you may just query the DB by the email address to check its status and then procceed with login attempt if the status is active. If you want to login anyhow regardless of the status and redirect only if inactive, something like above would work.

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