2

I am doing azure ad authentication in my node js bot. in the last step it is showing a pop windows with magic code. I want to close that magic window pop up automatically after 5 sec.

this is code

res.send('Welcome ' + req.user.displayName + '! Please copy this number and paste it back to your chat so your authentication can complete: ' + magicCode);

this line send a window pop with magic code. iwant to close this window after 5 sec automatically. my code

server.get('/api/OAuthCallback/',
  passport.authenticate('azuread-openidconnect', { failureRedirect: '/login' }),
  (req, res) => {
    console.log('OAuthCallback');
    console.log(req);
    const address = JSON.parse(req.query.state);
    const magicCode = crypto.randomBytes(4).toString('hex');
    const messageData = { magicCode: magicCode, accessToken: req.user.accessToken, refreshToken: req.user.refreshToken, userId: address.user.id, name: req.user.displayName, email: req.user.preferred_username };
    magicNum = magicCode;

    var continueMsg = new builder.Message().address(address).text(JSON.stringify(messageData));
    console.log(continueMsg.toMessage());
    test_name = JSON.parse(req.user._raw).preferred_username.split("@")[0]
    bot.receive(continueMsg.toMessage());
    res.send('Sign-in successful');
  }
);

Thanks in advance

C P Verma
  • 320
  • 7
  • 23

1 Answers1

4

It looks like you are using botauth lib, if so, there should be a html page which you need to use to display magic code.

This html should contain a similar js script code like:

document.getElementById("magic_code").innerText = (window.location.hash || '').replace('#', '');

And you can add following code to close the window in 5 secs

    setTimeout(() => {
        window.close();
    }, 5000);

The similar code sample at https://github.com/CatalystCode/node-authbot. And all use the OAuth 2.0 process, which once user success signin the authenticate provider, it will redirect to your return url you configed in your server.

In your code, your auth return route simply send a string in browser. Also, you can render a HTML page to your user, however, you will face the issue

Scripts may close only the windows that were opened by it.

The same as the question you provide in comment.

Unfortunately, there are several question for this on SO: window.close() doesn't work - Scripts may close only the windows that were opened by it window.close and self.close do not close the window in Chrome, and currently no good solution for this.

Gary Liu
  • 13,758
  • 1
  • 17
  • 32
  • there is no webpage for magic code. this is a pop up. there are two ways one is for web based authentication and another for desktop application , I am using desktop application auth, so there is no webpage for that, after filling ADID and password it in one pop up. magic code displayed in another pop up – C P Verma May 17 '18 at 06:09
  • I think I mix up the authentication method you are using. As mentioned in original question you are using AAD, but what confusing me is that bot in node.js and expressjs both are web applications, so how you authenticate your user via desktop application auth? – Gary Liu May 17 '18 at 07:38
  • yes I am using bot auth with node js.bot will be integrated with msteams. now when user sign in into bot it open a window pop up(not in browser) so the magic code comes on windows pop up.i wan to close that automatically. – C P Verma May 17 '18 at 08:44
  • @CPVerma, actually, I mean could you provide more details about how you integrate with AAD in your bot. Any code snippet, or post you follow, or library you are using. – Gary Liu May 18 '18 at 01:09
  • i have same eproblem like this https://stackoverflow.com/questions/45672670/how-to-close-the-sign-in-tab-after-successful-sign-in – C P Verma May 18 '18 at 05:53