/home/mip/mip/app/Modules/Client/Controllers/Auth/AuthController.php
<?php

namespace QxCMS\Modules\Client\Controllers\Auth;

use QxCMS\Modules\Likod\Models\Clients\User;
use Validator;
use JsValidator;
use Auth;
use Artisan;
use Illuminate\Http\Request;
use QxCMS\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use QxCMS\Modules\Client\Repositories\Settings\Roles\RoleRepositoryInterface as Role;
use QxCMS\Modules\Client\Models\Settings\LoginLogs\LoginLogs;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/client/dashboard';
    protected $guard = 'client';
    protected $redirectAfterLogout = 'client/auth/login';
    protected $role;
    protected $loginlogs;
    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct(Role $role, LoginLogs $loginlogs)
    {
        $this->middleware('guest.client', ['except' => 'logout']);
        $this->role = $role;
        $this->loginlogs = $loginlogs;
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }

    public function getLogin()
    {
        $rules = array(
            'email' => 'required|email',
            'password' => 'required',
        );
        $validator = JsValidator::make($rules, array(), array(), '#login-form');
        return view('Client::auth.login', ['validator'=> $validator]);
    }    

    public function postLogin(Request $request)
    {
        return $this->login($request);
    }
    
    protected function authenticated(Request $request, $user)
    {                
        $user = Auth::guard('client')->user();
        if(Auth::guard('client')->user()->client->status==1) {
            if($user->status == 1)
            {
                if(view()->exists('Client::cache.menus.'.$user->client->id.'.custom_'.$user->role_id))
                {
                    //check file modified if already eight hours
                    if(\File::lastModified(app_path().'/Modules/Client/Views/cache/menus/'.$user->client->id.'/custom_'.$user->role_id.'.blade.php')<time() - (8 * 60 * 60)) {
                        $this->role->setConfig($user->client);
                        $this->role->write_menu($user->client->id, $user->role_id, $user->client->database_name);
                    }                
                } else {
                    $this->role->setConfig($user->client);
                    $this->role->write_menu($user->client->id, $user->role_id, $user->client->database_name);
                }
                //run migrate
                $this->role->setConfig($user->client);
                Artisan::call('migrate', array('--database'=>'client','--path'=>'database/migrations/client'));
                //log in logs
                $loginlog = $this->loginlogs;
                $loginlog_details = [
                    'name' => $user->name,
                    'username' => $user->email,
                    'ipaddress' => $_SERVER['REMOTE_ADDR']
                ];
                $loginlog->fill($loginlog_details);                              
                $loginlog->save();
                return redirect($this->redirectTo);
            } else {
                $this->guard()->logout();
                $request->session()->regenerate();
                session()->flash('error', 'You account is inactive.');
                return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
            }
        } else {
            $this->guard()->logout();
            $request->session()->regenerate();
            session()->flash('error', 'This client is inactive.');
            return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
        }      
    }

    public function guard() 
    {
        return Auth::guard('client');
    }

    public function logout(Request $request)
    {
        $this->guard()->logout();
        $request->session()->regenerate();
        session()->flash('logout', 'You have successfully logged out!');
        return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
    }
}