Skip to content

fix: Non organizational DingTalk users scan the code to log in - can successfully log in and create a user#2239

Merged
wxg0103 merged 1 commit intomainfrom
pr@main@fix_1052022
Feb 11, 2025
Merged

fix: Non organizational DingTalk users scan the code to log in - can successfully log in and create a user#2239
wxg0103 merged 1 commit intomainfrom
pr@main@fix_1052022

Conversation

@shaohuzhang1
Copy link
Contributor

fix: Non organizational DingTalk users scan the code to log in - can successfully log in and create a user --bug=1052022 --user=王孝刚 【扫码登录】非组织下钉钉用户扫码登录-可登录成功并创建用户 https://www.tapd.cn/57709429/s/1653465

…successfully log in and create a user

--bug=1052022 --user=王孝刚 【扫码登录】非组织下钉钉用户扫码登录-可登录成功并创建用户 https://www.tapd.cn/57709429/s/1653465
@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Feb 11, 2025

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Feb 11, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

}
}
)
} catch (error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code provided has few minor inconsistencies and unnecessary operations:

  1. Typo in corpid: The function parameter corp_id should be corPID.

  2. Redundant Flag Handling: You're setting the errorShown flag to true, but this logic doesn't seem necessary unless you need to ensure only one error message appears per session. If that's intended, keep it; otherwise, consider removing.

  3. Consistent Error Message Logging: In both success and failure callbacks, you log the same error message using console.log. This might not always be ideal, especially if you want to handle different types of errors differently. Consider adding more context or refining these logs.

Here's an optimized version with some improvements:

const router = useRouter();
const { user } = useStore();

const loadDingTalkLoginScript = () => {
  return new Promise((resolve, reject) => {
    load().then(() => resolve()).catch(reject);
  });
};

const isConfigReady = ref(false);

let isLoading = false;
const errorDisplayed = ref(false); // Using array for multi-error handling

const initActive = async () => {
  setLoading(true);

  try {
    await loadDingTalkLoginScript();
    
    ddMiniProgram.login({
      scopes: ['openid', 'scope.corp'],
      prompt: 'consent',
    }).then(({ authCode }) => {
      // Handle authentication code
    }, ({ errMsg }) => {
      if (!errorDisplayed.value.includes(errMsg)) {
        MsgInfo(errMsg); // Use msg instead of MsgError in case it handles multiple messages better
        errorDisplayed.value.push(errMsg);
      }
    });

    isConfigReady.value = true;
  } catch (err) {
    console.error("An error occurred:", err.message);
  } finally {
    setLoading(false);
  }
};

const handleUserInit = async () => {
  try {
    if (!isConfigReady.value || !user.data.dingtalkToken) {
      await initActive();
    }

    await api.get(`/admin/api/user/init/${user.data.id}`, {
      headers: { Authorization: `Bearer ${user.data.access_token}` },
      params: {},
    })
      .then(response => {})
      .catch(e => {});
      
    setHasInit(user.data.id, true);
    return true;
  } catch (e) {}
};

Key Changes/Motivations:

  • Improved consistency across all logging statements by using meaningful methods like MsgInfo.
  • Introduced a loading status variable alongside isLoading as shown here. It helps manage UI loading states.
  • Simplified script loading into a reusable loadDingTalkLoginScript function to reduce duplication.
  • Utilized an array (errorDisplayed) to track which error messages have already been logged (to avoid duplicates).

These changes make the code cleaner and slightly more robust while maintaining its functionality effectively.

@wxg0103 wxg0103 merged commit de1d6bd into main Feb 11, 2025
4 checks passed
@wxg0103 wxg0103 deleted the pr@main@fix_1052022 branch February 11, 2025 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants