/**
  * Attempt to find and authenticate member if possible from the given data
  *
  * @param array $data
  * @param Form $form
  * @param bool &$success Success flag
  * @return Member Found member, regardless of successful login
  * @throws RestUserException
  */
 protected static function authenticate_member($data, $form, &$success)
 {
     if (!empty($data['Token'])) {
         /** @var Member $member */
         $member = null;
         // First check that the token is valid
         if (self::validate_token($data['Token'], $data['AuthService'], $data['UserID'])) {
             // Second, check that the Member exists
             /** @var SocialIdentity $identity */
             $identity = SocialIdentity::get()->filter(['AuthService' => $data['AuthService'], 'UserID' => $data['UserID']])->first();
             if ($identity) {
                 $member = $identity->Member();
                 $success = true;
                 return $member;
             }
             throw new RestUserException("User not found", 401, 401);
         } else {
             throw new RestUserException("Invalid access token", 401, 401);
         }
     } else {
         return parent::authenticate_member($data, $form, $success);
     }
 }
 /**
  * Attempt to find and authenticate member if possible from the given data
  *
  * @param array $data
  * @param Form $form
  * @param bool &$success Success flag
  * @return Member Found member, regardless of successful login
  * @throws RestUserException
  */
 protected static function authenticate_member($data, $form, &$success)
 {
     if (!empty($data['Token'])) {
         /** @var Member $member */
         $member = null;
         // First check that the token is valid
         if (self::validate_token($data['Token'], $data['AuthService'], $data['UserID'])) {
             // Second, check that the Member exists
             /** @var SocialIdentity $identity */
             $identity = SocialIdentity::get()->filter(['AuthService' => $data['AuthService'], 'UserID' => $data['UserID']])->first();
             if ($identity) {
                 $member = $identity->Member();
                 $success = true;
                 return $member;
             } elseif (self::config()->allow_login_to_connect) {
                 $profile = self::get_profile($data['Token'], $data['AuthService']);
                 $member = Member::get()->filter('Email', $profile['Email'])->first();
                 if ($member && !empty($profile['Email'])) {
                     $identity = new SocialIdentity();
                     $identity->MemberID = $member->ID;
                     $identity->AuthService = $data['AuthService'];
                     $identity->UserID = $data['UserID'];
                     $identity->write();
                     $success = true;
                     return $member;
                 }
             }
             throw new RestUserException("User not found", 401, 401);
         } else {
             throw new RestUserException("Invalid access token", 401, 401);
         }
     } else {
         return parent::authenticate_member($data, $form, $success);
     }
 }