Skip to content

Commit

Permalink
server: add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubham-Lal authored Jul 18, 2024
2 parents b5fb418 + 1bafa5b commit 9125d2d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
11 changes: 8 additions & 3 deletions server/controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ exports.recoverAccount = async function (fastify, request, reply) {

await sendMail(mailer, {
to: user[0].email,
subject: 'DebateMe',
subject: 'Reset Password',
html: `<p>You requested to reset your password. Click the link to reset:</p><p><a href="${`${process.env.FRONTEND_URL}/auth?type=reset&token=${token}`}">${`${process.env.FRONTEND_URL}/auth?type=reset&token=${token}`}</a></p>`
})
.then(info => {
Expand All @@ -166,8 +166,13 @@ exports.recoverAccount = async function (fastify, request, reply) {
message: `Activation link sent to your email${username ? (': ' + user[0].email) : ''}`
});
})
.catch(errors => { throw new ErrorHandler(400, false, 'Something went wrong') });

.catch(errors => {
reply.status(400).send({
success: false,
message: 'Something went wrong',
errors
});
});
} catch (err) {
return catchError(reply, err);
}
Expand Down
27 changes: 25 additions & 2 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,31 @@ fastify.decorate('upload', upload)

fastify.decorate('mysql', require('./db'))

const logs = [];
fastify.addHook('onRequest', async (request, reply) => {
request.logEntry = {
method: request.method,
url: request.url,
headers: request.headers,
body: request.body,
timestamp: new Date().toISOString()
};
});
fastify.addHook('onSend', async (request, reply, payload) => {
const responseLog = {
statusCode: reply.statusCode,
headers: reply.getHeaders(),
payload: payload,
timestamp: new Date().toISOString()
};
logs.push({ request: request.logEntry, response: responseLog });

if (logs.length > 1000) logs.shift();
});
fastify.get('/logs', (request, reply) => {
reply.send(logs);
});

fastify.register(require('@fastify/oauth2'), {
name: 'googleOAuth2',
scope: 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email',
Expand Down Expand Up @@ -53,8 +78,6 @@ fastify.register(require('fastify-mailer'), {

fastify.register(require('./routes/auth'), { prefix: '/api/auth' })

fastify.get('/test', (request, reply) => reply.code(200).send({ success: true, message: 'Server running...' }))

fastify.setNotFoundHandler((request, reply) => {
reply.redirect(process.env.FRONTEND_URL)
})
Expand Down

0 comments on commit 9125d2d

Please sign in to comment.