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

Fix for users getting redirected to the wrong path #279

Merged
merged 1 commit into from
Oct 6, 2023
Merged
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
36 changes: 24 additions & 12 deletions frontend/routes/authRoutes.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
const crypto = require("crypto");
const passport = require("passport");

module.exports = (app) => {
app.get(
"/auth/google",
(req, res, next) => {
if (req.query.return) {
req.session.oauth2return = req.query.return;
app.get("/auth/google", (req, res, next) => {
crypto.randomBytes(128, (err, buff) => {
if (err) {
throw err;
}
next();
},
passport.authenticate("google", { scope: ["email"] }),
);
const state = buff.toString("hex");
app.locals.stateStore.set(state, { returnUrl: req.query.return || "/" });
const authenticator = passport.authenticate("google", {
scope: ["email"],
state,
});
authenticator(req, res, next);
});
});

app.get(
"/auth/google/callback",
passport.authenticate("google"),
(req, res) => {
const redirect = req.session.oauth2return || "/";
delete req.session.oauth2return;
res.redirect(redirect);
const { state } = req.query;
if (app.locals.stateStore.has(state)) {
const { redirectUrl } = app.locals.stateStore.get(state).returnUrl;
app.locals.stateStore.delete(state);
res.redirect(redirectUrl);
} else {
// If we don't have anything in the state store then treat
// the login as invalid
res.sendStatus(403);
}
},
);

Expand Down
7 changes: 5 additions & 2 deletions frontend/webapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

const app = express();

// Initialize the state store
app.locals.stateStore = new Map();

const corsOptions = {
origin: [`${config.get("PROJECT")}.appspot.com`, "localhost:5000"],
allowedHeaders: ["Content-Type"],
Expand All @@ -30,8 +33,8 @@
/* eslint-disable consistent-return */
const authRequired = function authRequired(req, res, next) {
if (!req.user) {
req.session.oauth2return = req.originalUrl;
return res.redirect("/auth/google");
const params = new URLSearchParams({ return: req.originalUrl });
return res.redirect(`/auth/google?${params.toString()}`);
}
next();
};
Expand Down Expand Up @@ -74,5 +77,5 @@

const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}...`);

Check warning on line 80 in frontend/webapp.js

View workflow job for this annotation

GitHub Actions / Frontend Tests

Unexpected console statement
});
Loading