Friday, November 6, 2015

Login after admin approval in Laravel 5.1

Login after admin approval in Laravel 5.1:

In Laravel 5.1, here is a system that when we register any user then it's by default logged in. If we want to make a system in which we are in need of admin approval then we have to make it like the following code:

Login after admin approval in Laravel

We have to override two method for this:

1. postLogin
2. postRegister


Open your this file app/Http/Controllers/Auth/AuthController.php

and add these functions.

Login after admin approval in Laravel


    public function postRegister(Request $request)
    {
        $validator = $this->validator($request->all());
        if ($validator->fails()) {
            $this->throwValidationException(
                $request, $validator
            );
        }
        $this->create($request->all());
        return redirect('/auth/login')->with('status', 'Successfully Registered! You can login after the approval of admin.');
    }



Login after admin approval in Laravel

    public function postLogin(Request $request)
    {
        $this->validate($request, [
            $this->loginUsername() => 'required', 'password' => 'required',
        ]);

        $throttles = $this->isUsingThrottlesLoginsTrait();

        if ($throttles && $this->hasTooManyLoginAttempts($request)) {
            return $this->sendLockoutResponse($request);
        }

        $credentials = $this->getCredentials($request);
        $credentials['status'] = 'active';

        if (Auth::attempt($credentials, $request->has('remember'))) {
            return $this->handleUserWasAuthenticated($request, $throttles);
        }

        if ($throttles) {
            $this->incrementLoginAttempts($request);
        }

        return redirect($this->loginPath())
            ->withInput($request->only($this->loginUsername(), 'remember'))
            ->withErrors([
                $this->loginUsername() => $this->getFailedLoginMessage(),
            ]);
    }


Login after admin approval in Laravel



In postRegister() function there will only registration is possible & in postLogin() we will firstly check its status.


Here in this function $credentials['status'] = 'active' will check if its approved by admin or not.

Here status is my field name in users table and active is its value.

This will work.

Thanks

No comments:

Post a Comment