function cw_fbauth_user_login($user)
{
    global $tables, $current_area;
    cw_load('crypt', 'user', 'mail');
    $user_data = cw_query_first("SELECT customer_id, email, password\n\t\t\t\t\t\t\t\t\tFROM {$tables['customers']} \n\t\t\t\t\t\t\t\t\tWHERE oauth_uid='" . $user['id'] . "' AND oauth_type='F'  \n\t\t\t\t\t\t\t\t\t\tAND usertype='" . $current_area . "' \n\t\t\t\t\t\t\t\t\t\tAND status='Y'");
    if (!empty($user_data)) {
        // login user
        global $email, $password, $action;
        $email = $user_data['email'];
        $password = text_decrypt($user_data['password']);
        $action = 'login';
        cw_include('include/login.php');
    } else {
        // create user
        $register = array();
        $register['email'] = $user['email'];
        if (strpos($user['email'], "proxymail.facebook.com") !== FALSE) {
            cw_header_location("index.php?target=fb_auth_get_email", TRUE);
        }
        $register['password'] = $register['password2'] = md5(uniqid('cw_', TRUE), TRUE);
        $register['usertype'] = $current_area;
        $partner_membership =& cw_session_register('partner_membership');
        $register['membership_id'] = $partner_membership;
        $customer_id = cw_user_create_profile($register);
        $identifiers =& cw_session_register('identifiers', array());
        $identifiers[$current_area] = array('customer_id' => $customer_id);
        $customer = array('oauth_uid' => $user['id']);
        cw_array2update('customers', $customer, "customer_id='{$customer_id}'");
        $address = array('main' => 1, 'firstname' => $user['first_name'], 'lastname' => $user['last_name']);
        $additional_info = array('sex' => $user['gender'] == 'male' ? 1 : 0);
        $userinfo = array('addresses' => $address, 'additional_info' => $additional_info);
        cw_user_update($userinfo, $customer_id, $customer_id);
        cw_user_send_modification_mail($customer_id, TRUE);
    }
}
 if (!count($fill_error)) {
     $new_profile = cw_check_user_field_build_profile($userinfo, $update_fields, $profile_fields);
     $is_new_profile = false;
     if ($mode == 'add') {
         $new_profile['usertype'] = $usertype;
         $user = cw_user_create_profile($new_profile);
         // Login registered user
         $identifiers =& cw_session_register('identifiers', array());
         $identifiers['C'] = array('customer_id' => $user);
         $is_new_profile = true;
         cw_event('on_login', array($user, 'C', 1));
     }
     cw_user_update($new_profile, $user, $customer_id);
     // Sign in confirmation letter sending have to be apparently blocked if an user is fake
     if (!isset($is_anonymous) || $is_anonymous != 1) {
         cw_user_send_modification_mail($user, $is_new_profile);
     }
     cw_event('on_profile_modify', array($user, $new_profile));
     if ($mode == 'add' && $is_anonymous) {
         $top_message_text = 'Please proceed to shipping and payment';
     } else {
         $top_message_text = $mode == 'add' ? cw_get_langvar_by_name('msg_user_has_been_added_' . (empty($usertype) ? 'C' : $usertype)) : cw_get_langvar_by_name('msg_profile_upd');
     }
     cw_add_top_message($top_message_text, 'I');
 } else {
     $filled_profile = $update_fields;
     cw_add_top_message(cw_check_user_get_error($fill_error), 'E');
 }
 if (AREA_TYPE == 'A') {
     if (empty($fill_error)) {
         $mode = 'modify';
        $fill_error['password'] = true;
        $error_text .= '<br/>' . cw_get_langvar_by_name('lbl_password_confirmation_wrong');
    }
    if (count($fill_error)) {
        $prefilled_info = $register;
        $top_message['content'] = $error_text;
        $top_message['type'] = 'E';
    } else {
        $usertype = 'C';
        if ($action == 'register_reseller') {
            $usertype = 'R';
        }
        $register['usertype'] = $usertype;
        $register['membership_id'] = $partner_membership;
        $customer_id = cw_user_create_profile($register);
        cw_user_send_modification_mail($customer_id, true);
        $identifiers =& cw_session_register("identifiers", array());
        $identifiers[$usertype] = array('customer_id' => $customer_id);
        $prefilled_info = array();
    }
    cw_header_location("index.php?target={$target}&usertype={$usertype}");
}
if ($action == 'login_customer') {
    $action = 'login';
    $current_area = 'C';
    include $app_main_dir . '/include/login.php';
    cw_header_location("index.php?target={$target}");
}
if ($action == 'login_reseller') {
    $action = 'login';
    $current_area = 'R';