xmppjs / xmpp.js

XMPP for JavaScript
ISC License
2.19k stars 373 forks source link

[Question] How to pass username and password from HTTP handler #800

Closed wuriyanto48 closed 4 years ago

wuriyanto48 commented 4 years ago

First, thank you for this awesome project, really helpful. I am using SASL to handle login from my route login. My login handler looks like this

app.post('/login', async (req, res, next) => {
    const body = req.body;
    const username = body.username;
    const password = body.password;

    if (username == '' || password == '') {
        return res.json({'message': 'invalid username or password'});
    }
     ...... 

my question is, how do i pass username and password to the authenticate function

async function authenticate(auth, mechanism) {
    console.debug('authenticate', mechanism)
    const credentials = {
              username: ?,
              password: ?
         }
    console.debug('authenticating');
    try {
      await auth(credentials)
      console.debug('authenticated');
    } catch (err) {
      console.error(err)
      throw err
    }
}

thank you

sonnyp commented 4 years ago

This is more of a JavaScript question than xmpp.js :)

Why don't you move xmpp initialization inside that http handler?

app.post('/login', async (req, res, next) => {
    const body = req.body;
    const username = body.username;
    const password = body.password;

    const xmpp = client({
       ...,
       username,
       password
    })

?

wuriyanto48 commented 4 years ago

thank you for your fast response @sonnyp , honestly i need to create a connection first, before process the actual login. Because i need a xmpp instance that should globally. when i create a connection inside login handler i can't get a xmpp instance.

app.post('/login', async (req, res, next) => {
    const body = req.body;
    const username = body.username;
    const password = body.password;

    const xmpp = client({
       ...,
       username,
       password
    })

what about my event handler, like

xmpp.on('offline', address => {
            doSomething();
});

xmpp.on('stanza', stanza => {
            doSomething();
});

(IMHO) that should be outside of the HTTP handler. In my case, if i want to send a message, its also need a xmpp instance.

app.post('/send-message', async (req, res, next) => {
    const body = req.body;
    const msg= body.message;
    const message = xml(
            'message',
            {type: 'chat', to: to},
            xml('body', null, 
                xml('content', null, msg)
            )
          );

   await  xmpp.send(message);
sonnyp commented 4 years ago

then do something like

const connections = {}

app.post('/login', async (req, res, next) => {
    const body = req.body;
    const username = body.username;
    const password = body.password;

    const connection = client({
       ...,
       username,
       password
    })
    try {
    await connection.start()
    } catch (err) {return next(err)
    connections[username] = connection
    res.statusCode = 200
})

app.post('/send-message', async (req, res, next) => {
    const body = req.body;

    const msg= body.message;
    // BEWARE you probably want to use some form of http authentication instead!
    // this is very unsafe as anyone can pretend to be a connected user
    const username = body.username;
    const message = xml(
            'message',
            {type: 'chat', to: to},
            xml('body', null, 
                xml('content', null, msg)
            )
          );

   await connections[username].send(message);