Quantcast
Channel: Login only if user is active using Laravel - Stack Overflow
Viewing all articles
Browse latest Browse all 25

Answer by Piotr Jankiewicz for Login only if user is active using Laravel

$
0
0

If someone uses ajax request on login and wants to have custom message, here is how I achieved this in login controller:

login() function

  // This section is the only change    if ($this->guard()->validate($this->credentials($request))) {        $user = $this->guard()->getLastAttempted();        // Make sure the user is active        if ($user->status == 1 && $this->attemptLogin($request)) {            // Send the normal successful login response            return $this->sendLoginResponse($request);        } else {            // Increment the failed login attempts and redirect back to the            // login form with an error message.            $this->incrementLoginAttempts($request);            return $this->sendFailedLoginResponse($request, true);        }    }

And other functions

 public function sendLoginResponse(Request $request){    $redirectTo = false;    if ($request->headers->get('referer') == env('APP_URL') . '/' || $request->headers->get('referer') == env('APP_URL') . '/login') {        $redirectTo = $this->redirectPath();    }    if ($request->expectsJson()) {        return response()->json(['status' => true, 'user' => auth()->user(), 'redirectTo' => $redirectTo, 'fragments' => ['#main-nav' => view('includes.nav')->render()        ]]);    } else {        return redirect($redirectTo);    }}public function sendFailedLoginResponse(Request $request, $user_not_active = fasle){    if ($user_not_active) {        return response()->json(['status' => false, 'email' => 'Your account is not active.']);    }    return response()->json(['status' => false, 'email' => 'Incorrect login credentials.']);}

Viewing all articles
Browse latest Browse all 25

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>