Skip to content

Commit

Permalink
Enable request logging and use console.error for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Harsh14901 committed Jan 5, 2022
1 parent c171828 commit e51c758
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 21 deletions.
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"https-proxy-agent": "^5.0.0",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.9.13",
"morgan": "^1.10.0",
"passport": "^0.4.1",
"passport-facebook": "^3.0.0",
"passport-github2": "^0.1.12",
Expand Down
2 changes: 1 addition & 1 deletion src/config/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const rtokens = redis.createClient({
});

rtokens.on('error', (err) => {
console.log(err);
console.error(err);
});

export default rtokens;
2 changes: 1 addition & 1 deletion src/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ router.use(async (req, res, next) => {
);
next();
} catch (error) {
console.log(error);
console.error(error);
return res.sendStatus(401);
}
});
Expand Down
12 changes: 6 additions & 6 deletions src/routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ router.get('/email/verify/token', async (req, res) => {
await user.save();
res.render('account_verified');
} catch (error) {
console.log(error);
console.error(error);
res.clearCookie(accessTokenName, {
domain: !keys.isDev ? 'devclub.in' : null,
});
Expand Down Expand Up @@ -119,7 +119,7 @@ router.get('/password/reset/token', async (req, res) => {
serviceURL: '',
});
} catch (error) {
console.log(error);
console.error(error);
res.clearCookie(accessTokenName, {
domain: !keys.isDev ? 'devclub.in' : null,
});
Expand Down Expand Up @@ -262,7 +262,7 @@ router.get('/iitd/confirm', async (req, res) => {
authInfo = null
) => {
if (err || !user) {
console.log(err, user);
console.error(err, user);
return res.sendStatus(500);
}

Expand Down Expand Up @@ -313,7 +313,7 @@ router.get('/iitd/confirm', async (req, res) => {
);
}
} catch (error) {
console.log(error);
console.error(error);
return res.sendStatus(500);
}
});
Expand Down Expand Up @@ -347,7 +347,7 @@ router.get('/clientVerify', async (req, res) => {
msg: 'Client verified successfully',
});
} catch (error) {
console.log(error);
console.error(error);
return res.status(401).json({
err: true,
msg: 'Unauthorized Client',
Expand Down Expand Up @@ -413,7 +413,7 @@ router.post('/requestToken', async (req, res) => {
const token = getRequestToken(requestToken);
res.send(token);
} catch (error) {
console.log(error);
console.error(error);
return res.status(401).json({
err: true,
msg: 'Unauthorized Client',
Expand Down
10 changes: 5 additions & 5 deletions src/routes/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const verifyClientOwner = async (req, res, next) => {
throw Error('Unauthorized access');
}
} catch (error) {
console.log(error);
console.error(error);
return res.status(401).render('client/clients.ejs', {
err: true,
msg: 'This client does not belong to you',
Expand Down Expand Up @@ -181,7 +181,7 @@ router.post('/register', async (req, res) => {
await client.save();
return res.redirect(`/client/${client.id}`);
} catch (error) {
console.log(error);
console.error(error);
return res.render('client/client_register.ejs', {
err: true,
msg: 'Whoops! A server error occured',
Expand Down Expand Up @@ -225,7 +225,7 @@ router.get('/:id/config', verifyClientOwner, async (req, res) => {
const randomFile = path.resolve(__dirname, `./${makeid(10, true)}`);
fs.writeFileSync(randomFile, vars);
res.sendFile(randomFile, (err) => {
if (err) console.log(err);
if (err) console.error(err);
fs.unlinkSync(randomFile);
});
} catch (error) {
Expand Down Expand Up @@ -309,7 +309,7 @@ router.post('/:id/update', verifyClientOwner, async (req, res) => {
msg: 'Client Updated Successfully',
});
} catch (error) {
console.log(error);
console.error(error);
return res.status(500).json({
err: true,
msg: 'Whoops! A server error occured',
Expand Down Expand Up @@ -341,7 +341,7 @@ router.post('/:id/delete', verifyClientOwner, async (req, res) => {
await client.remove();
return res.redirect('/client?err=false&msg=Client+deleted+succesfully');
} catch (error) {
console.log(error);
console.error(error);
return res.status(500).json({
err: true,
msg: 'Whoops! A server error occured',
Expand Down
2 changes: 1 addition & 1 deletion src/routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ router.post('/register', async (req, res) => {
serviceURL,
});
} catch (err) {
console.log(err);
console.error(err);
res.render('register', {
message: 'WHOOPS!! A server error occured, please try again later',
error: true,
Expand Down
5 changes: 4 additions & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as keys from './config/keys';
import { socialAuthenticate, linkSocial } from './utils/utils';

const app = express();
const morgan = require('morgan');

const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
Expand Down Expand Up @@ -115,6 +116,8 @@ githubStrategy._oauth2.setAgent(httpsProxyAgent);
passport.use(googleStrategy);
passport.use(fbStrategy);
passport.use(githubStrategy);

app.use(morgan('combined'));
app.use(passport.initialize());

app.use(cors());
Expand Down Expand Up @@ -146,7 +149,7 @@ mongoose
console.log('Connected to the database...');
})
.catch((err) => {
console.log(err);
console.error(err);
});

// Root page
Expand Down
11 changes: 5 additions & 6 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ const verifyToken = async (

return user;
} catch (err) {
console.log(err);
// I wasn't able to verify the token as it was invalid
// clear the tokens
res.clearCookie(keys.accessTokenName, {
Expand Down Expand Up @@ -141,7 +140,7 @@ const sendVerificationEmail = async (user) => {
secret: process.env.MAILER_TOKEN,
});
} catch (error) {
console.log(error);
console.error(error);
}
};
const sendPassResetEmail = async (user, newPass) => {
Expand All @@ -168,7 +167,7 @@ const sendPassResetEmail = async (user, newPass) => {
secret: process.env.MAILER_TOKEN,
});
} catch (error) {
console.log(error);
console.error(error);
}
};

Expand Down Expand Up @@ -209,7 +208,7 @@ const assignRoleToUsers = async (role, del = false, users = null) => {
await user.save();
}
} catch (err) {
console.log(err);
console.error(err);
}
}
};
Expand Down Expand Up @@ -238,7 +237,7 @@ const addRoles = async (user) => {
await assignRoleToUsers(role, false, [user]);
}
} catch (error) {
console.log(error);
console.error(error);
}
};

Expand Down Expand Up @@ -310,7 +309,7 @@ const socialAuthenticate = async (
message: msg,
});
} catch (error) {
console.log(error);
console.error(error);
return done(error);
}
};
Expand Down

0 comments on commit e51c758

Please sign in to comment.