sbuhacks/api/apis/user-apis.js

78 lines
2.3 KiB
JavaScript
Raw Normal View History

2022-09-24 17:06:11 -04:00
const uuid = require('uuid');
const config = require('../config')
const rabbitMQ = config.rabbitMQ
const amqp = require('amqplib/callback_api');
registerUser = async (req, res) => {
req = req.body;
return await assert("register", req, res);
}
loginUser = async (req, res) => {
req = req.body;
return await assert("login", req, res);
}
const assert = async (queueName, req, res) => {
amqp.connect(rabbitMQ, function (error0, connection) {
if (error0) {
throw error0;
}
connection.createChannel(function (error1, channel) {
if (error1) {
throw error1;
}
channel.assertQueue('', {
exclusive: true
}, function (error2, q) {
if (error2) {
throw error2;
}
let correlationId = uuid.v4();
console.log(' [x] Requesting: ', req);
channel.consume(q.queue, function (msg) {
console.log(msg)
if (msg.properties.correlationId == correlationId) {
console.log(' [.] Got %s', msg.content.toString());
msg = JSON.parse(msg.content)
2022-09-24 20:23:26 -04:00
console.log(msg.cookie)
if (msg.cookie) {
res.cookie("token", msg.cookie, {
httpOnly: true,
secure: true,
sameSite: "lax"
}).status(msg.status).send(msg.body)
}
else {
res.status(msg.status).send(msg.body)
}
2022-09-24 17:06:11 -04:00
setTimeout(function () {
connection.close();
}, 500);
}
else {
res.status(500).send("Internal server error")
}
}, {
noAck: true
});
channel.sendToQueue(queueName,
Buffer.from(JSON.stringify(req)), {
correlationId: correlationId,
replyTo: q.queue
});
});
});
});
}
module.exports = {
registerUser,
loginUser,
}