Skip to content

Commit

Permalink
9-end: upgrade packages, withAuth fix #476
Browse files Browse the repository at this point in the history
  • Loading branch information
klyburke committed Jun 11, 2022
1 parent 587bd74 commit 61009f7
Show file tree
Hide file tree
Showing 4 changed files with 1,216 additions and 2,109 deletions.
13 changes: 9 additions & 4 deletions book/9-end/lib/withAuth.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function withAuth(
static async getInitialProps(ctx) {
// console.log('withAuth.getInitialProps');
const isFromServer = typeof window === 'undefined';
const user = ctx.req ? ctx.req.user && ctx.req.user.toObject() : globalUser;
const user = ctx.req ? ctx.req.user : globalUser;

if (isFromServer && user) {
user._id = user._id.toString();
Expand All @@ -39,12 +39,17 @@ export default function withAuth(
return;
}

if (adminRequired && (!user || !user.isAdmin)) {
if (adminRequired && user && !user.isAdmin) {
Router.push('/customer/my-books', '/my-books');
}

if (logoutRequired && user) {
Router.push('/');
if (!user.isAdmin) {
Router.push('/customer/my-books', '/my-books');
return;
}

Router.push('/admin');
}
}

Expand All @@ -55,7 +60,7 @@ export default function withAuth(
return null;
}

if (adminRequired && (!user || !user.isAdmin)) {
if (adminRequired && user && !user.isAdmin) {
return null;
}

Expand Down
14 changes: 7 additions & 7 deletions book/9-end/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
"coverageDirectory": "./.coverage"
},
"dependencies": {
"@emotion/cache": "^11.6.0",
"@emotion/react": "^11.6.0",
"@emotion/cache": "^11.7.1",
"@emotion/react": "^11.9.0",
"@emotion/server": "^11.4.0",
"@emotion/styled": "^11.6.0",
"@mui/icons-material": "^5.2.0",
"@mui/material": "^5.2.0",
"@emotion/styled": "^11.8.1",
"@mui/icons-material": "^5.8.3",
"@mui/material": "^5.8.3",
"@octokit/oauth-authorization-url": "^4.1.0",
"@octokit/rest": "^18.0.3",
"@stripe/stripe-js": "^1.21.1",
Expand All @@ -40,10 +40,10 @@
"lodash": "^4.17.21",
"marked": "^1.2.7",
"mongoose": "^5.10.0",
"next": "^12.0.3",
"next": "^12.1.6",
"node-fetch": "^2.6.1",
"nprogress": "^0.2.0",
"passport": "^0.4.1",
"passport": "^0.6.0",
"passport-google-oauth": "^2.0.0",
"prop-types": "^15.7.2",
"qs": "^6.10.1",
Expand Down
12 changes: 8 additions & 4 deletions book/9-end/server/google.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,21 @@ function setupGoogle({ server, ROOT_URL }) {

if (req.user && req.user.isAdmin) {
res.redirect('/admin');
} else if (req.session.finalUrl) {
} else if (req.user && req.session.finalUrl) {
res.redirect(`${ROOT_URL}${req.session.finalUrl}`);
} else {
res.redirect('/my-books');
}
},
);

server.get('/logout', (req, res) => {
req.logout();
res.redirect('/login');
server.get('/logout', (req, res, next) => {
req.logout((err) => {
if (err) {
next(err);
}
res.redirect('/login');
});
});
}

Expand Down

0 comments on commit 61009f7

Please sign in to comment.