From 76849cc74777e3a443181ab7c0579210ba1528e3 Mon Sep 17 00:00:00 2001 From: Renge Date: Sat, 24 Sep 2022 21:12:14 -0400 Subject: [PATCH] feat: implement getUserById and getUserByToken --- auth/auth/auth.js | 2 +- auth/index.js | 81 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/auth/auth/auth.js b/auth/auth/auth.js index bae41f9..ca8b707 100644 --- a/auth/auth/auth.js +++ b/auth/auth/auth.js @@ -2,7 +2,7 @@ const jwt = require("jsonwebtoken") const config = require('../config') function authManager() { - getUserID = function (token) { + getUserId = function (token) { try { if (!token) { return 'Guest'; diff --git a/auth/index.js b/auth/index.js index 52126d6..ed57e66 100644 --- a/auth/index.js +++ b/auth/index.js @@ -147,6 +147,41 @@ const login = async (req) => { } } +const getUserById = async (req) => { + const user = await User.findById({_id: req}); + if (!user) { + let res = { + found: false, + user: {} + } + return JSON.stringify(res) + } + else { + let res = { + found: true, + user: { + name: user.name, + email: user.email, + Id: user._id + } + } + return JSON.stringify(res) + } +} + +const getUserByToken = async (req) => { + let id = getUserId(req) + if (id !== 'Guest') { + return getUserById(id) + } + else { + let res = { + found: false, + user: {} + } + return JSON.stringify(res) + } +} const amqp = require('amqplib/callback_api'); amqp.connect(rabbitMQ, function (error0, connection) { @@ -198,4 +233,50 @@ amqp.connect(rabbitMQ, function (error0, connection) { }) }); }); + + connection.createChannel(function (error1, channel) { + if (error1) { + throw error1; + } + var queue = 'getUserById'; + + channel.assertQueue(queue, { + durable: false + }); + channel.prefetch(1); + console.log(' [x] Awaiting RPC requests'); + channel.consume(queue, function reply(msg) { + getUserById(msg.content.toString()).then((res) => { + channel.sendToQueue(msg.properties.replyTo, + Buffer.from(res.toString()), { + correlationId: msg.properties.correlationId + }); + + channel.ack(msg); + }) + }); + }); + + connection.createChannel(function (error1, channel) { + if (error1) { + throw error1; + } + var queue = 'getUserByToken'; + + channel.assertQueue(queue, { + durable: false + }); + channel.prefetch(1); + console.log(' [x] Awaiting RPC requests'); + channel.consume(queue, function reply(msg) { + getUserByToken(msg.content.toString()).then((res) => { + channel.sendToQueue(msg.properties.replyTo, + Buffer.from(res.toString()), { + correlationId: msg.properties.correlationId + }); + + channel.ack(msg); + }) + }); + }); }); \ No newline at end of file