Closed wuriyanto48 closed 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
})
?
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);
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);
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
my question is, how do i pass username and password to the
authenticate
functionthank you