Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion client/src/pages/home/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ function Home() {

//upload files to server
Axios.post(`${process.env.REACT_APP_SERVER_URL}/api/upload`, formData, {
withCredentials:true,
Copy link
Owner

Choose a reason for hiding this comment

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

Can you explain this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

mentioning withCredentials:true will ensure that the cookies are sent to the server with every request.

headers: {
'Content-Type': 'multipart/form-data',
'Authorization': 'Bearer ' + window.localStorage.getItem("didToken")
}
})
.then(res => {
Expand Down
1 change: 1 addition & 0 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const app = express();
app.use(cors());
app.use(morgan('dev'))
app.use(express.json());
app.use(cookieParser());
app.use(express.urlencoded({ extended: true }));
app.use(fileUpload());
app.use(limiter);
Expand Down
15 changes: 15 additions & 0 deletions server/middlewares/authenticate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { Magic } = require("@magic-sdk/admin");
const magic = new Magic(process.env.MAGIC_SECRET_KEY);

const authenticate = async (req, res, next) => {
console.log("Auth middleware 2 called");
try {
const { didToken } = req.cookies;
await magic.token.validate(didToken);
next();
} catch (error) {
return res.status(401).json({ error: error.message });
}
};

module.exports = authenticate;
37 changes: 37 additions & 0 deletions server/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 server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"license": "ISC",
"dependencies": {
"@magic-sdk/admin": "^1.4.1",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^16.0.1",
"express": "^4.18.1",
Expand Down
2 changes: 2 additions & 0 deletions server/routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ router.post('/api/user/create', authMiddleware, async (req, res, next) => {
const magic_id = req.body.magic_id;
const user_name = req.body.user_name;
const email = req.body.email;
const didToken = req.headers.authorization.substring(7);

if (!user_name || !magic_id || !email) {
return next( new AppError("Missing required fields", 400));
Expand All @@ -43,6 +44,7 @@ router.post('/api/user/create', authMiddleware, async (req, res, next) => {
})
console.log("saving user")
await user.save();
res.cookie('didToken',didToken,{httpOnly:true})
Copy link
Owner

Choose a reason for hiding this comment

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

Explain this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Remember I commented the window.localStorage.setItem("didToken", newDidToken); well I did that for this. This is setting the token ass httpOnly cookie in the response header so whenever client receives this response , client will automatically save the token in the browser cookie and I have passed httpOnly:true because by mentioning this it will ensure that it will not be accessible from client javascript.

return res.status(200).json({ message: "User created successfully" });
}
else {
Expand Down