Skip to content

Commit

Permalink
Add devtools zone
Browse files Browse the repository at this point in the history
  • Loading branch information
hbenl committed Apr 15, 2024
1 parent 2e3d6d6 commit bc2b5ad
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 7 deletions.
11 changes: 10 additions & 1 deletion next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
const devtoolsURL = process.env.DEVTOOLS_URL || "https://replay-devtools-new.vercel.app";

/** @type {import('next').NextConfig} */
const nextConfig = {};
const nextConfig = {
rewrites: async () => [
{
source: "/recording/:path*",
destination: `${devtoolsURL}/recording/:path*`
}
]
};

export default nextConfig;
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const COOKIES = {
accessToken: "replay:dashboard:access-token",
authReturnTo: "replay:auth-return-to",
browserAuth: "replay:browser-auth",
defaultPathname: "replay:dashboard:default-pathname",
mobileWarningDismissed: "replay:dashboard:mobile-warning-dismissed",
Expand Down
2 changes: 1 addition & 1 deletion src/pageComponents/user/settings/Account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function Account() {

deleteCookieValueClient(COOKIES.accessToken);

window.location.replace("/api/auth/logout");
window.location.replace(`/api/auth/logout?${new URLSearchParams({ origin: location.origin })}`);
};

return (
Expand Down
39 changes: 36 additions & 3 deletions src/pages/api/auth/[auth0].ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { COOKIES } from "@/constants";
import { getValueFromArrayOrString } from "@/utils/getValueFromArrayOrString";
import { handleAuth, handleCallback, handleLogin } from "@auth0/nextjs-auth0";
import { handleAuth, handleCallback, handleLogin, handleLogout } from "@auth0/nextjs-auth0";
import cookie from "cookie";
import { NextApiRequest, NextApiResponse } from "next";

export default handleAuth({
login: (req: NextApiRequest, res: NextApiResponse) => {
const { origin, returnTo } = handleOriginAndReturnTo(req, res);
handleLogin(req, res, {
authorizationParams: {
audience: "https://api.replay.io",
Expand All @@ -12,9 +15,17 @@ export default handleAuth({
scope: "openid profile offline_access",
prompt: getValueFromArrayOrString(req.query.prompt),
connection: getValueFromArrayOrString(req.query.connection),
}
redirect_uri: `${origin}/api/auth/callback`,
},
returnTo
});
},

logout: (req: NextApiRequest, res: NextApiResponse) => {
const { returnTo } = handleOriginAndReturnTo(req, res);
handleLogout(req, res, { returnTo });
},

callback: (req: NextApiRequest, res: NextApiResponse) => {
if (req.query.error_description) {
const searchParams = new URLSearchParams({
Expand All @@ -23,7 +34,29 @@ export default handleAuth({
});
res.redirect(`/browser/error?${searchParams.toString()}`);
} else {
handleCallback(req, res);
handleCallback(req, res, { redirectUri: req.cookies[COOKIES.authReturnTo] });
}
},
});

// This app also handles auth for the domain of the devtools app.
function handleOriginAndReturnTo(req: NextApiRequest, res: NextApiResponse) {
const origin = getValueFromArrayOrString(req.query.origin) || process.env.AUTH0_BASE_URL!;

const returnTo = origin + (getValueFromArrayOrString(req.query.returnTo) || "/");

// We'll need to pass returnTo to the callback handler, otherwise
// Auth0 will reject login requests for the domains of other apps.
// We store it in a cookie so that it's available when the callback handler is called.
res.setHeader(
"Set-Cookie",
cookie.serialize(COOKIES.authReturnTo, returnTo, {
secure: origin.startsWith("https://"),
httpOnly: true,
path: "/",
maxAge: 5 * 60 * 1000,
})
);

return { origin, returnTo };
}
10 changes: 10 additions & 0 deletions src/pages/api/token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getAccessToken } from "@auth0/nextjs-auth0";
import { NextApiRequest, NextApiResponse } from "next";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const { accessToken } = await getAccessToken(req, res);
res.status(200).send(accessToken);
}
2 changes: 1 addition & 1 deletion src/pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export default function Page({
const [ssoLogin, setSSOLogin] = useState(false);

function onLogin(connection: string) {
let authUrl = `/api/auth/login?connection=${connection}&returnTo=${returnTo}`;
let authUrl = `/api/auth/login?${new URLSearchParams({ connection, returnTo, origin: location.origin })}`;
if (switchAccount) {
authUrl += "&prompt=login";
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/recording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ export function getURL(id: string, buildId: string) {
const target = getRecordingTarget(buildId);

return target === "chromium"
? `https://app.replay.io/recording/${id}`
? `/recording/${id}`
: `https://legacy.replay.io/recording/${id}`;
}

0 comments on commit bc2b5ad

Please sign in to comment.