Sabtu, 19 Februari 2022

Cara agar user non aktif tidak bisa login ke Laravel UI 8

 Menggunakan Laravel UI tentunya telah dibuatkan yang istilahnya scafolding nya, yaitu dibuatkan route, model, controller views dan database-nya, Sekarang gimana ya cara kita bisa memodifikasi scafolding tersebut biar misal user tersebut non aktif dan tidak bisa login..

Caranya buka trait AuthenticatesUsers yg lokasi nya di :

    vendor/laravel/ui/auth-backend/AuthenticatesUsers.php

Atau dengan kalau di visual studi code anda diinstall plugin laravel tinggal ctrl klik aja AuthenticatesUsers di login controller terus langsung deh menuju lokasi file -nya

Dia asumsikan dalam database ditambahi field "is_active" maka kita cukup tambahkan script berikut

         $user = User::where('email', $request->email)->first();

        if ($user && $user->is_active == 0) {

            abort(403, 'Your account has been disabled by an administrator.');

        }

yang saya letakkan disini :

 public function login(Request $request)
    {

        $this->validateLogin($request);

        // If the class is using the ThrottlesLogins trait, we can automatically throttle
        // the login attempts for this application. We'll key this by the username and
        // the IP address of the client making these requests into this application.
        if (
            method_exists($this, 'hasTooManyLoginAttempts') &&
            $this->hasTooManyLoginAttempts($request)
        ) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }

        // Check if user is active
        $user = User::where('email', $request->email)->first();
        if ($user && $user->is_active == 0) {
            abort(403, 'Your account has been disabled by an administrator.');
        }

        if ($this->attemptLogin($request)) {
            if ($request->hasSession()) {
                $request->session()->put('auth.password_confirmed_at', time());
            }

            return $this->sendLoginResponse($request);
        }

        // If the login attempt was unsuccessful we will increment the number of attempts
        // to login and redirect the user back to the login form. Of course, when this
        // user surpasses their maximum number of attempts they will get locked out.
        $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
    }

And jangan lupa import model User supaya jika database user ada di database lain bisa konek,, tapi kalau cuman ada 1 database sih gk perlu import model user...

Tidak ada komentar:

Posting Komentar