Skip to content
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
3 changes: 2 additions & 1 deletion devjams/app/profile/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { motion } from "../../components/gsap-motion";
import { GDGLockup } from "@/components/portal/GDGLockup";
import {
portalApi,
portalLoginPath,
type AttendanceCheckin,
type AttendanceStatus,
type UserSession,
Expand All @@ -33,7 +34,7 @@ export default function ProfilePage() {
const loadProfile = async () => {
const token = portalApi.getToken();
if (!token && !portalApi.getSession()) {
router.push("/portal");
router.push(portalLoginPath(`${window.location.pathname}${window.location.search}`));
return;
}

Expand Down
5 changes: 2 additions & 3 deletions devjams/app/submissions/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { getCountries, getCountryCallingCode, type CountryCode } from "libphonen
import { GDGLockup } from "@/components/portal/GDGLockup";
import {
portalApi,
portalLoginPath,
type DynamicSubmissionField,
type DynamicSubmissionNote,
type DynamicSubmissionPayload,
type UserSession,
} from "@/services/portalApi";

const SUBMISSION_NOTE_STYLES: Record<DynamicSubmissionNote["type"], string> = {
info: "border-sky-400/30 bg-sky-400/10 text-sky-100",
warning: "border-amber-400/30 bg-amber-400/10 text-amber-100",
Expand Down Expand Up @@ -416,10 +416,9 @@ export default function DynamicSubmissionPage({ params }: { params: Promise<{ sl

useEffect(() => {
if (!session || !portalApi.getToken()) {
router.push("/portal");
router.push(portalLoginPath(`${window.location.pathname}${window.location.search}`));
return;
}

portalApi.fetchSubmissionForm(slug)
.then((nextPayload) => {
setPayload(nextPayload);
Expand Down
4 changes: 2 additions & 2 deletions devjams/app/team/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useEffect, useState } from "react";
import { useRouter } from "next/navigation";
import { motion, AnimatePresence } from "../../components/gsap-motion";
import { GDGLockup } from "@/components/portal/GDGLockup";
import { portalApi, type BackendTeamMember } from "@/services/portalApi";
import { portalApi, portalLoginPath, type BackendTeamMember } from "@/services/portalApi";
import { memberActionFor, shouldWarnSubmittedIdeaRemoval } from "../team-member-actions";

export default function TeamPage() {
Expand All @@ -33,7 +33,7 @@ export default function TeamPage() {
const loadTeamData = async () => {
const token = portalApi.getToken();
if (!token && !portalApi.getSession()) {
router.push("/portal");
router.push(portalLoginPath(`${window.location.pathname}${window.location.search}`));
return;
}

Expand Down
20 changes: 18 additions & 2 deletions devjams/components/portal/PortalLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export function PortalLogin() {

useEffect(() => {
const parameters = new URLSearchParams(window.location.search);
const requestedRedirect = parameters.get("redirect");
if (requestedRedirect) {
portalApi.rememberLoginRedirect(requestedRedirect);
}
const oauthCode = parameters.get("oauth_code");
const authError = parameters.get("auth_error");

Expand All @@ -38,7 +42,11 @@ export function PortalLogin() {
void Promise.resolve().then(() => setLoading(true));
portalApi
.completeGoogleLogin(oauthCode)
.then((session) => router.replace(nextPortalRoute(session)))
.then((session) => {
const redirect = portalApi.getLoginRedirect();
portalApi.clearLoginRedirect();
router.replace(redirect ?? nextPortalRoute(session));
})
.catch((reason: unknown) => {
setError(reason instanceof Error ? reason.message : "Google sign-in could not be completed.");
setLoading(false);
Expand All @@ -50,7 +58,11 @@ export function PortalLogin() {
if (!portalApi.getToken()) return;
try {
const session = await portalApi.fetchMe();
if (session) router.replace(nextPortalRoute(session));
if (session) {
const redirect = portalApi.getLoginRedirect();
portalApi.clearLoginRedirect();
router.replace(redirect ?? nextPortalRoute(session));
}
} catch {
portalApi.logout();
}
Expand All @@ -60,6 +72,10 @@ export function PortalLogin() {

const beginGoogleLogin = (type: "internal" | "external") => {
setError("");
const requestedRedirect = new URLSearchParams(window.location.search).get("redirect");
if (requestedRedirect) {
portalApi.rememberLoginRedirect(requestedRedirect);
}
setPendingType(type);
setLoading(true);
window.location.assign(portalApi.googleOAuthStartUrl(type));
Expand Down
31 changes: 31 additions & 0 deletions devjams/services/portal-api-redirect.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import test from "node:test";
import assert from "node:assert/strict";
import { portalApi, portalLoginPath } from "./portalApi.ts";

const storage = new Map();
globalThis.window = globalThis;
globalThis.localStorage = {
getItem: (key) => storage.get(key) ?? null,
setItem: (key, value) => storage.set(key, value),
removeItem: (key) => storage.delete(key),
};

test("builds a safe login URL for protected participant paths", () => {
assert.equal(portalLoginPath("/profile"), "/portal?redirect=%2Fprofile");
assert.equal(
portalLoginPath("/submissions/project-intake?from=qr"),
"/portal?redirect=%2Fsubmissions%2Fproject-intake%3Ffrom%3Dqr",
);
});

test("rejects external login redirect targets", () => {
assert.equal(portalLoginPath("https://evil.example"), "/portal");
assert.equal(portalLoginPath("//evil.example"), "/portal");
});

test("persists the intended participant redirect through OAuth", () => {
portalApi.rememberLoginRedirect("/team");
assert.equal(portalApi.getLoginRedirect(), "/team");
portalApi.clearLoginRedirect();
assert.equal(portalApi.getLoginRedirect(), null);
});
38 changes: 38 additions & 0 deletions devjams/services/portalApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,20 @@ const STORAGE_KEY_SESSION = "devjams26_portal_session";
const STORAGE_KEY_ONBOARDING = "devjams26_portal_onboarding";
const STORAGE_KEY_SUBMISSION = "devjams26_portal_submission";
const STORAGE_KEY_TEAM = "devjams26_portal_team";
const STORAGE_KEY_LOGIN_REDIRECT = "devjams26_login_redirect";
const pendingRemovedMemberIds = new Set<string>();
function safeLoginRedirect(value?: string | null): string | null {
if (!value || !value.startsWith("/") || value.startsWith("//") || value.includes("\\")) {
return null;
}
return value;
}

export function portalLoginPath(returnTo?: string): string {
const safePath = safeLoginRedirect(returnTo);
return safePath ? `/portal?redirect=${encodeURIComponent(safePath)}` : "/portal";
}


async function persistIdea(
idea: BackendTeamIdea,
Expand Down Expand Up @@ -449,6 +462,31 @@ export const portalApi = {
}
},

rememberLoginRedirect(returnTo: string): void {
if (typeof window === "undefined") return;
const safePath = safeLoginRedirect(returnTo);
if (safePath) {
localStorage.setItem(STORAGE_KEY_LOGIN_REDIRECT, safePath);
} else {
localStorage.removeItem(STORAGE_KEY_LOGIN_REDIRECT);
}
},

getLoginRedirect(): string | null {
if (typeof window === "undefined") return null;
const safePath = safeLoginRedirect(localStorage.getItem(STORAGE_KEY_LOGIN_REDIRECT));
if (!safePath) {
localStorage.removeItem(STORAGE_KEY_LOGIN_REDIRECT);
}
return safePath;
},

clearLoginRedirect(): void {
if (typeof window !== "undefined") {
localStorage.removeItem(STORAGE_KEY_LOGIN_REDIRECT);
}
},

async request<T>(endpoint: string, options: RequestInit = {}): Promise<T> {
const baseUrl = portalApi.getBaseUrl();
const token = portalApi.getToken();
Expand Down
Loading