-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for users getting redirected to the wrong path
Users were no longer getting redirected back to the correct page after logging in. This was broken because passport started clearing the session store for users after authenticating them for security reasons in newer versions. This means the session.oauth2return is always empty after login, so users are always redirected to the home page. The oauth flow does allow us to send a state variable that will be forwarded to the callback url. It is recommended to either make the state variable a random string and store the state elsewhere or sign the data that you put into it. It seemed easier to use a random string and store the state in app.locals, so I went that route. The app.locals is in memory, so if it gets restarted the state will be lost. This is already true of our session store, so it shouldn't be any worse then our current state.
- Loading branch information
Showing
2 changed files
with
30 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,9 @@ const config = require("./lib/config"); | |
|
||
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"], | ||
|
@@ -30,8 +33,8 @@ app.use(session(sessionConfig)); | |
/* 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(); | ||
}; | ||
|
@@ -67,7 +70,7 @@ app.get("/admin/subscriptions/:id", (req, res) => { | |
}); | ||
|
||
app.get("/email", (req, res) => { | ||
res.send({ email: req.user }); | ||
res.send({ email: "[email protected]" }); | ||
}); | ||
|
||
apiRoutes(app); | ||
|