diff --git a/server/src/routes/auth/index.ts b/server/src/routes/auth/index.ts index 1aef5c7..58e38ee 100644 --- a/server/src/routes/auth/index.ts +++ b/server/src/routes/auth/index.ts @@ -157,10 +157,10 @@ authRouter.get("/config", (_, res) => { }); authRouter.get("/google", (req, res, next) => { - const origin = req.headers.origin || req.headers.referer || "/"; + const redirect = (req.query.redirect as string) || "/"; passport.authenticate("google", { scope: ["profile", "email"], - state: Buffer.from(origin).toString("base64"), + state: Buffer.from(redirect).toString("base64"), })(req, res, next); }); @@ -171,25 +171,23 @@ authRouter.get( }), (req, res) => { const state = req.query.state as string; - let redirectUrl = "/"; + let redirectPath = "/"; if (state) { try { const decoded = Buffer.from(state, "base64").toString("utf-8"); - const url = new URL(decoded); - const authPaths = [ - "/login", - "/register", - "/forgot-password", - "/reset-password", - ]; - if (!authPaths.includes(url.pathname)) { - redirectUrl = url.pathname; - } + const routeToPathMap: Record = { + "app-main": "/app", + "app-sequencer": "/app/sequencer", + "app-blog": "/blog", + profile: "/profile", + messages: "/messages", + }; + redirectPath = routeToPathMap[decoded] || "/"; } catch { - redirectUrl = "/"; + redirectPath = "/"; } } - res.redirect(redirectUrl); + res.redirect(redirectPath); }, ); diff --git a/webapp/src/views/auth/LoginView.vue b/webapp/src/views/auth/LoginView.vue index b02e026..cc980fc 100644 --- a/webapp/src/views/auth/LoginView.vue +++ b/webapp/src/views/auth/LoginView.vue @@ -84,7 +84,10 @@ const form = reactive({ }); const googleAuthEnabled = computed(() => authStore.googleAuthEnabled); -const googleAuthUrl = "/api/auth/google"; +const googleAuthUrl = computed(() => { + const baseUrl = "/api/auth/google"; + return redirect ? `${baseUrl}?redirect=${encodeURIComponent(redirect)}` : baseUrl; +}); async function submitForm() { const result = await apiClient.post<{ user: User }>("/auth/login", form);