Skip to content

Commit

Permalink
fix(context): creation failing for new users (#178)
Browse files Browse the repository at this point in the history
* fix: add check for session exists

* chore: set correct session parameters

* fix: use correct session valid check

* fix: remove token requirements in imagekit auth endpoint
  • Loading branch information
rutajdash authored Feb 6, 2023
1 parent e92d713 commit f4bd753
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 25 deletions.
8 changes: 4 additions & 4 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ if (!process.env.NODE_ENV || process.env.NODE_ENV !== 'production') {
maxAge: 3600000, // 1 Hour
secure: true,
httpOnly: true,
domain: 'example.com',
path: 'foo/bar',
domain: 'mondaymorning.nitrkl.ac.in',
path: '/',
expires: new Date(Date.now() + 60 * 60 * 1000), // 1 hour
},
store,
resave: false,
saveUninitialized: false,
resave: true,
saveUninitialized: true,
})
);
}
Expand Down
12 changes: 1 addition & 11 deletions server/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,7 @@ router.use('/admin/spotify/auth', async (_req, res) => {
}
});

router.use('/admin/media/auth', async (req, res) => {
const { authToken, decodedToken } = await UserAuth.getContext(req);

if (!authToken || !decodedToken || !UserPermission.exists(req.session, authToken, decodedToken, 'media.write.new')) {
return res.status(401).json({
data: 'The user is not authorized to access media endpoint.',
code: 401,
error: true,
});
}

router.use('/admin/media/auth', (req, res) => {
try {
const imagekit = new ImageKit({
publicKey: process.env.IMAGEKIT_PUBLIC_KEY,
Expand Down
21 changes: 12 additions & 9 deletions server/utils/userAuth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,18 @@ const UserAuth = {
}

const { uid, exp, roles, mid } = _decodedToken;
req.session.auth = {
uid,
mid,
jwt: req.headers.authorization,
exp,
roles,
decodedToken: _decodedToken,
};
await req.session.save();

if (req.session) {
req.session.auth = {
uid,
mid,
jwt: req.headers.authorization,
exp,
roles,
decodedToken: _decodedToken,
};
await req.session.save();
}

return {
authToken: req.headers.authorization,
Expand Down
2 changes: 1 addition & 1 deletion server/utils/userAuth/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const UserSession = {
!session.auth ||
!session.auth.jwt ||
!session.auth.exp ||
!session.auth.roles ||
!session.auth.uid ||
session.auth.jwt !== jwt ||
session.auth.exp <= Date.now() / 1000
? false
Expand Down

0 comments on commit f4bd753

Please sign in to comment.