public function postRegister()
 {
     try {
         $jCryption = new JCryption();
         $data = json_decode(\Input::get('data'), true);
         parse_str($jCryption->decrypt(session('jkey'), $data), $output);
         $group = \User::findGroupByName('Agency');
         $user = \User::register(['email' => $output['email'], 'password' => $output['password'], 'activated' => true]);
         $user->addGroup($group);
         $aData = ['user_id' => $user->id, 'name' => $output['name'], 'owner_name' => $output['owner_name'], 'city' => $output['city'], 'country' => $output['country'], 'address' => $output['address'], 'phone' => $output['phone'], 'image' => null];
         $agency = \Agency::register($aData);
         $mailData = ['layout' => 'emails.welcome', 'data' => ['user' => $user], 'subject' => 'Welcome to Programme Chameleon', 'from_email' => '*****@*****.**', 'to_email' => $user->email];
         if (env('APP_ENV') === 'production') {
             $job = (new EmailJob($mailData))->onQueue('email-queue');
             $this->dispatch($job);
         } else {
             // for devs only
             \Mail::send($mailData['layout'], $mailData['data'], function ($message) use($mailData) {
                 $message->from($mailData['from_email'], 'Programme Chameleon Mailing Service');
                 $message->to($mailData['to_email'])->subject($mailData['subject']);
             });
         }
         return \Response::json(['type' => 'success', 'message' => 'Successfully registered. You can login <a href="' . route('front.login') . '">here</a>']);
     } catch (\Exception $e) {
         if (isset($user)) {
             $user->delete();
         }
         if (isset($agency)) {
             $agency->delete();
         }
         return \Response::json(['type' => 'danger', 'message' => env('APP_DEBUG') ? $e->getMessage() : 'Error, please contact webmaster.']);
     }
 }
 public function postLogin()
 {
     try {
         $jCryption = new JCryption();
         $data = json_decode(\Input::get('data'), true);
         parse_str($jCryption->decrypt(session('jkey'), $data), $output);
         $remember = isset($output['rememberme']) ? true : false;
         $user = \User::authenticate(['email' => $output['email'], 'password' => $output['password']], $remember);
         if (!$user->hasAccess('admin')) {
             $user->logout();
             throw new \Exception("Your account cannot be authenthicated this panel.", 1);
             return;
         }
         return \Response::json(['type' => 'success', 'message' => 'Login success, redirecting..', 'redirect' => route('admin.index')]);
     } catch (\Exception $e) {
         return \Response::json(['type' => 'danger', 'message' => $e->getMessage()]);
     }
 }
 public function postLogin()
 {
     try {
         $jCryption = new JCryption();
         $data = json_decode(\Input::get('data'), true);
         parse_str($jCryption->decrypt(session('jkey'), $data), $output);
         $remember = isset($output['rememberme']) ? true : false;
         $user = \User::authenticate(['email' => $output['email'], 'password' => $output['password']], $remember);
         if ($user->hasAccess('admin')) {
             $user->logout();
             throw new \Exception("Your account cannot be authenthicate this panel.", 1);
             return;
         }
         $redirect = null;
         if ($user->hasAccess('contractor')) {
             $redirect = url('contractor');
             session(['_sess_contractor' => ['model' => $user->contractor]]);
         } else {
             if ($user->hasAccess('company')) {
                 $redirect = url('company');
                 session(['_sess_company' => ['model' => $user->company]]);
             } else {
                 if ($user->hasAccess('agency')) {
                     $redirect = url('agency');
                     session(['_sess_agency' => ['model' => $user->agency]]);
                 }
             }
         }
         if (is_null($redirect)) {
             $user->logout();
             throw new \Exception("Your account cannot be authenthicate this panel.", 1);
             return;
         }
         return \Response::json(['type' => 'success', 'message' => 'Login success, redirecting..', 'redirect' => $redirect]);
     } catch (\Exception $e) {
         return \Response::json(['type' => 'danger', 'message' => $e->getMessage()]);
     }
 }