static function login($app) { // Get Post Data $post = $app->request->post(); // Validate Sent Input $valid = self::validateFacebookProfile($post); if ($valid !== true) { return array('authenticated' => false, 'msg' => 'Facebook login failed. Check your parameters and try again.'); } // Look for user with that facebook id $existing = AuthData::selectUserByFacebookId($post['facebookId']); if (!$existing) { // Look for user with that email $emailExists = AuthData::selectUserAndPasswordByEmail($post['email']); if (!$emailExists) { /// FAIL - If a user with that email does not exist return array('authenticated' => false, 'msg' => 'Login failed. No user with that Facebook account exists.'); } $facebookAdded = AuthData::updateUserFacebookId(array(':id' => $emailExists->id, ':facebook_id' => $post['facebookId'])); if (!$facebookAdded) { /// FAIL - If a user with that email already exists return array('authenticated' => false, 'msg' => 'Login failed. No user with that Facebook account exists.'); } unset($emailExists->password); $existing = $emailExists; } // Create logged in token $token = AuthControllerNative::createAuthToken($app, $existing->id); if ($token) { $found = array('user' => $existing); $found['user']->apiKey = $token['apiKey']; $found['user']->apiToken = $token['apiToken']; $found['sessionLifeHours'] = $token['sessionLifeHours']; $found['authenticated'] = true; // Send the session life back (in hours) for the cookies return $found; } else { return array('authenticated' => false, 'msg' => 'Facebook login failed to create token.'); } }