botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

Events are not getting triggered when oauth sign in is success

Open aravindtrue opened this issue 8 years ago • 3 comments

Bot Info

  • SDK Platform: NodeJS
  • SDK Version: 8.11.3
  • Active Channels: MS Team
  • Deployment Environment: Azure Bot Service

Issue Description

Events are not getting triggered in the example code provide in basics-oauth

Code Example

var bot = new builder.UniversalBot(connector, function (session) {
    if (session.message.text == 'signout') {
        // It is important to have a SignOut intent
        connector.signOutUser(session.message.address, connectionName,  (err, result) => {
            if (!err) {
                session.send('You are signed out.');
            } else {
                session.send('There was a problem signing you out.');                
            }
        });
    } else {
        // First check whether the Azure Bot Service already has a token for this user
        connector.getUserToken(session.message.address, connectionName, undefined, (err, result) => {
            if (result) {
                // If there is already a token, the bot can use it directly
                session.send('You are already signed in with token: ' + result.token);
            } else {
                // If there not is already a token, the bot can send an OAuthCard to have the user log in
                if (!session.userData.activeSignIn) {
                    session.send("Hello! Let's get you signed in!");
                    builder.OAuthCard.create(connector, session, connectionName, "Please sign in", "Sign in", (createSignInErr, signInMessage) =>
                    {
                        if (signInMessage) {
                            session.send(signInMessage);
                            session.userData.activeSignIn = true;
                        } else {
                            session.send("Something went wrong trying to sign you in.");
                        }     
                    });
                } else {
                    // Some clients require a 6 digit code validation so we can check that here
                    session.send("Let's see if that code works...");
                    connector.getUserToken(session.message.address, connectionName, session.message.text, (err2, tokenResponse) => {
                        if (tokenResponse) {
                            session.send('It worked! You are now signed in with token: ' + tokenResponse.token);
                            session.userData.activeSignIn = false;
                        } else {
                            session.send("Hmm, that code wasn't right");
                        }
                    });
                }
            }
        });
    }
})
.set('storage', inMemoryStorage) // Register in memory storage
.on("event", (event) => {         // Handle 'event' activities
    if (event.name == 'tokens/response') {
        // received a TokenResponse, which is how the Azure Bot Service responds with the user token after an OAuthCard
        bot.loadSession(event.address, (err, session) => {
            let tokenResponse = event.value;
            session.send('You are now signed in with token: ' + tokenResponse.token);
            session.userData.activeSignIn = false;
        });
    }
});

connector.onInvoke((event, cb) => {
    if (event.name == 'signin/verifyState') {
        // received a MS Team's code verification Invoke Activity
        bot.loadSession(event.address, (err, session) => {
            let verificationCode = event.value.state;
            // Get the user token using the verification code sent by MS Teams
            connector.getUserToken(session.message.address, connectionName, verificationCode, (err, result) => {
                session.send('You are now signed in with token: ' + result.token);
                session.userData.activeSignIn = false;
                cb(undefined, {}, 200);
            });
        });
    } else {
        cb(undefined, {}, 200);
    }
});

Reproduction Steps

  1. Executed the code in https://github.com/Microsoft/BotBuilder/blob/master/Node/examples/basics-oauth/app.js

Expected Behavior

Event must be triggered once oauth sign in is complete

Actual Results

Sign in is success and I'm able to get the user token but the events which are bound are not working.

aravindtrue avatar Sep 03 '18 18:09 aravindtrue

It appears you have posted this on SO as well right here. So this will be tracked there.

sbuffkin avatar Sep 04 '18 17:09 sbuffkin

@nyxsys Yes, I'd posted it since I thought it may be a 'How To' request or problem with my code. Later, I cloned this project and tried to execute the /basics-oauth/app.js and it was not working. I've created this issues once I confirmed there is no problem with my code. Also, there was no help suggestions or answers on SO.

aravindtrue avatar Sep 04 '18 17:09 aravindtrue

This issue is coming for WebChat, things work fine in emulator. Is there any solution for this?

mayankX avatar Sep 14 '18 20:09 mayankX