Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation #44

Merged
merged 20 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"nodemailer": "^6.7.2",
"npm-run-all": "^4.1.5",
"supertest": "^6.2.2",
"swagger-ui-express": "^4.3.0",
"winston": "^3.5.1",
"yarn-upgrade-all": "^0.6.1"
},
Expand Down
19 changes: 12 additions & 7 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
const express = require('express');

require('dotenv').config();

const cookieParser = require('cookie-parser');
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
const { connectToMongo } = require('./db/connection');

const authRoutes = require('./routers/auth');

const itemRoutes = require('./routers/item');

const userRoutes = require('./routers/user');

const globalRoutes = require('./routers/global');

const logger = require('./services/logger');

const app = express();
Expand All @@ -30,6 +25,16 @@ app.use('/api/items', itemRoutes);
app.use('/api/users', userRoutes);
app.use('/api/global', globalRoutes);

const options = {
customCss: '.swagger-ui .topbar { display: none }',
};

app.use(
'/api/docs',
swaggerUi.serve,
swaggerUi.setup(swaggerDocument, options)
);

const server = app.listen(port, () => {
logger.log('info', `Server is running on port ${port}`);
connectToMongo();
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ module.exports = {
maxAge: cookieAge * 1000,
httpOnly: true,
});
// This header is set for swagger users to access the token easily
res.setHeader('authorizationToken', token);
res.json({ message: 'Successfully signed in' });
} catch (err) {
res.status(422).json({ message: err.message ?? err });
Expand Down
5 changes: 4 additions & 1 deletion src/middlewares/user-authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
const jwt = require('jsonwebtoken');

module.exports = (req, res, next) => {
const { token } = req.cookies;
let { token } = req.cookies;
if (req?.headers?.swaggerToken) {
token = req.headers.swaggerToken;
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to send the authentication token with a header because the Cookie or Set-Cookie headers are forbidden headers and the browser forbids swagger from setting them.
You can refer to this issue that explains the problem, and this MDN article for forbidden headers list.

try {
const { user } = jwt.verify(token, process.env.JWT_SECRET);
// if verified pass the user to the next function with the req object
Expand Down
Loading