As you can see on this line, the removal is called immediately when the onChangeLocalStorage is called. It does not verify first if there is an actual code or not.
https://github.com/cuongdevjs/reactjs-social-login/blob/24b29fe5cda1316eb176ad14711cc3dc84edad5d/src/LoginSocialMicrosoft/index.tsx#L140C30-L140C30
By simply moving this line to after the If condition, i.e. Line 143, the issue is resolved. Here is the result:
const onChangeLocalStorage = useCallback(() => {
const code = localStorage.getItem('microsoft')
if (code) {
window.removeEventListener('storage', onChangeLocalStorage, false)
void handlePostMessage({
provider: 'microsoft',
type: 'code',
code,
}).then(() => {
localStorage.removeItem('microsoft')
})
}
}, [handlePostMessage])