Esempio n. 1
0
 public function login(Request $request)
 {
     if (Auth::check()) {
         // If the user is already logged in then redirect to landing page.
         return redirect($this->landingPage());
     }
     $p = ['email' => '', 'password' => ''];
     $data = [];
     view()->share(['title' => 'Log In', 'CB_PAGE_JS' => [url('/js/mods/Cb.Notify.js')]]);
     if ($request->isMethod('post') && $request->has('submit')) {
         $p = $request->all();
         // See: https://github.com/Respect/Validation/blob/master/docs/README.md
         $checks = [];
         $checks['email'] = Valid::email()->notEmpty()->validate($p['email']);
         $checks['password'] = Valid::string()->notEmpty()->validate($p['password']);
         try {
             if (in_array(false, $checks)) {
                 throw new Exception('Some required field have invalid values');
             }
             $auth_response = App\Cb\Users::authenticate($p['email'], $p['password']);
             if (!is_object($auth_response)) {
                 if (is_numeric($auth_response)) {
                     // $auth_response <-- is user id in this context
                     $resend_link = route('resend_signup_confirmation', ['uid' => App\Crypt::urlencode($auth_response)]);
                     throw new Exception('Please verify your account. Click <a href="' . $resend_link . '">here</a> to resend the confirmation email');
                 }
                 throw new Exception('Invalid email or password');
             }
             // Successfully authenticated, save some details to session for faster access //
             $request->session()->put('current_user', $auth_response);
             $request->session()->put('current_user_type', $auth_response->type);
             App\Cb\Users\Presence::setOnline($auth_response->id);
             // Set presence as online
             return redirect($this->landingPage($auth_response->type));
         } catch (Exception $err) {
             cb_set_message($err->getMessage(), 0);
         }
     }
     $data['post'] = $p;
     return View::make('user_login', $data)->render();
 }
Esempio n. 2
0
 protected function userAuthenticate($_post)
 {
     $p = $_post;
     $this->req($p, ['email', 'password', 'token', 'os']);
     $auth_response = App\Cb\Users::authenticate($p['email'], $p['password'], true);
     $uid;
     if (!is_object($auth_response)) {
         if (is_numeric($auth_response)) {
             // $auth_response <-- is user id in this context
             $resend_link = route('resend_signup_confirmation', ['uid' => App\Crypt::urlencode($auth_response)]);
             $uid = $auth_response;
         } else {
             $this->error('Invalid email or password');
         }
     } else {
         $uid = $auth_response->id;
         App\Cb\Users\Presence::setOnline($uid);
         // Set presence as online
     }
     // Save the token for this user //
     App\Cb\Devices::add($uid, $p['token'], $p['os']);
     xplog('Registered device token "' . $p['token'] . '" for user "' . $uid . '" for os "' . $p['os'] . '"', __METHOD__);
     $user_details = App\Cb\Users::getDetailsById($uid);
     if (!$user_details) {
         $this->error('Unable to find user details.');
     }
     if (isset($resend_link)) {
         $user_details->resend_link = $resend_link;
     }
     return ['api_name' => $_post['api_name'], 'payload' => $user_details];
 }