tsmatz / bot-framework-with-auth-example

Authentication Example in Microsoft Bot Framework (obsolete)
5 stars 6 forks source link

Auth demo in Nodejs #1

Open hofman-p opened 7 years ago

hofman-p commented 7 years ago

Hello,

I'm very interested in both of your samples, bot and web auth. Unfortunately, I'm not very good in C#, even if I know the rudiments. I'd like to know if it is possible to have the same code using Node ? It'd be so helpful.

Thanks,

Pierre

mpicciolli commented 7 years ago

Hi @hofman-p, did you find any example ?

hofman-p commented 7 years ago

Hello @mpicciolli ! No I didn't :/ I don't think @tsmatsuz is maintaining this project anymore (see the date of my issue)

tsmatz commented 7 years ago

Hi @hofman-p san, I apologize for my late response. Sorry but I don't have the same code by Node and I'm not having the time to re-implement with Node now. However you can implement the same logic by Node. See the following "Pattern A" for the logical flow of this implementation.

https://blogs.msdn.microsoft.com/tsmatsuz/2016/09/06/microsoft-bot-framework-bot-with-authentication-and-signin-login/

This sample is just using the bot state service for storing authentication token. (This is the key point of this example.) And the default bot state service is implemented by both .NET SDKs and Node.js. That is, you can also use by Node.js. (You can also create your own bot state service to meet your needs.)

Tutorial by Node implementation https://docs.microsoft.com/en-us/bot-framework/nodejs/bot-builder-nodejs-overview

Sample using bot state with Node https://github.com/Microsoft/BotBuilder-Samples/tree/master/Node/core-State

mpicciolli commented 7 years ago

@hofman-p you can use my code example below :

server.post('/api/auth_callback', function(req, res, next){
   var botStorageContext = {
            userId: req.body.address.user.id,
            address: req.body.address,
            persistUserData: true,
            persistConversationData: true
        };

        connector.getData(botStorageContext, (err, data)=>{
            data.userData.token = req.body.token
            connector.saveData(botStorageContext, data, (err)=>{
                console.log(err);
                var msg = new builder.Message().address(req.body.address);
                msg.text('You are logged');
                bot.send(msg);
                next();
            });
        });
    }) 

The callback is POST http://localhost:3978/api/auth_callback BODY :

{
"token": "YOUR TOKEN",
"address":{"id":"ial0km58all5edd9i","channelId":"emulator","user":{"id":"default-user","name":"User"},"conversation":{"id":"chm1ci4l70en5g4gc"},"bot":{"id":"default-bot","name":"Bot"},"serviceUrl":"http://127.0.0.1:50294"}
}

Then you save token in the user data.