Friday, November 13, 2015

Multipule table authentication using laravel 5.1

 Multipule table authentication using laravel 5.1:

1. Install laravel 5.1
2.Open terminal and go to laravel instalation folder.
3.Run the following command.
composer require sarav/laravel-mltiauth dev-master
4.Open config/app.php
Replace Illuminate\Auth\AuthServiceProvider:: class with Sarav\Multiauth\MultiauthServiceProvider

5.Modify auth.php
'multi' => [
    'user' => [
        'driver' => 'eloquent',
        'model'  => App\User::class,
        'table'  => 'users'
    ],
    'admin' => [
        'driver' => 'eloquent',
        'model'  => App\Admin::class,
        'table'  => 'admins'
    ]
],
6. Create admins table
7.login using follwing details.
\Auth::attempt("user", ['email' => 'johndoe@gmail.com', 'password' => 'password']);

// Attempts to login admin with email id johndoe@gmail.com
\Auth::attempt("admin", ['email' => 'johndoe@gmail.com', 'password' => 'password']);

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