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
132 changes: 68 additions & 64 deletions apps/dashboard/src/app/login/auth-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
LoginPayload,
VerifyLoginPayloadParams,
} from "thirdweb/auth";
import { isVercel } from "../../lib/vercel-utils";

export async function getLoginPayload(
params: GenerateLoginPayloadParams,
Expand Down Expand Up @@ -45,80 +46,83 @@ export async function doLogin(
throw new Error("API_SERVER_SECRET is not set");
}

if (!turnstileToken) {
return {
error: "Missing Turnstile token.",
};
}
// only validate the turnstile token if we are in a vercel environment
if (isVercel()) {
if (!turnstileToken) {
return {
error: "Missing Turnstile token.",
};
}

// get the request headers
const requestHeaders = await headers();
if (!requestHeaders) {
return {
error: "Failed to get request headers. Please try again.",
};
}
// CF header, fallback to req.ip, then X-Forwarded-For
const [ip, errors] = (() => {
let ip: string | null = null;
const errors: string[] = [];
try {
ip = requestHeaders.get("CF-Connecting-IP") || null;
} catch (err) {
console.error("failed to get IP address from CF-Connecting-IP", err);
errors.push("failed to get IP address from CF-Connecting-IP");
// get the request headers
const requestHeaders = await headers();
if (!requestHeaders) {
return {
error: "Failed to get request headers. Please try again.",
};
}
if (!ip) {
// CF header, fallback to req.ip, then X-Forwarded-For
const [ip, errors] = (() => {
let ip: string | null = null;
const errors: string[] = [];
try {
ip = ipAddress(requestHeaders) || null;
ip = requestHeaders.get("CF-Connecting-IP") || null;
} catch (err) {
console.error(
"failed to get IP address from ipAddress() function",
err,
);
errors.push("failed to get IP address from ipAddress() function");
console.error("failed to get IP address from CF-Connecting-IP", err);
errors.push("failed to get IP address from CF-Connecting-IP");
}
}
if (!ip) {
try {
ip = requestHeaders.get("X-Forwarded-For");
} catch (err) {
console.error("failed to get IP address from X-Forwarded-For", err);
errors.push("failed to get IP address from X-Forwarded-For");
if (!ip) {
try {
ip = ipAddress(requestHeaders) || null;
} catch (err) {
console.error(
"failed to get IP address from ipAddress() function",
err,
);
errors.push("failed to get IP address from ipAddress() function");
}
}
}
return [ip, errors];
})();
if (!ip) {
try {
ip = requestHeaders.get("X-Forwarded-For");
} catch (err) {
console.error("failed to get IP address from X-Forwarded-For", err);
errors.push("failed to get IP address from X-Forwarded-For");
}
}
return [ip, errors];
})();

if (!ip) {
return {
error: "Could not get IP address. Please try again.",
context: errors,
};
}
if (!ip) {
return {
error: "Could not get IP address. Please try again.",
context: errors,
};
}

// https://developers.cloudflare.com/turnstile/get-started/server-side-validation/
// Validate the token by calling the "/siteverify" API endpoint.
const result = await fetch(
"https://challenges.cloudflare.com/turnstile/v0/siteverify",
{
body: JSON.stringify({
secret: process.env.TURNSTILE_SECRET_KEY,
response: turnstileToken,
remoteip: ip,
}),
method: "POST",
headers: {
"Content-Type": "application/json",
// https://developers.cloudflare.com/turnstile/get-started/server-side-validation/
// Validate the token by calling the "/siteverify" API endpoint.
const result = await fetch(
"https://challenges.cloudflare.com/turnstile/v0/siteverify",
{
body: JSON.stringify({
secret: process.env.TURNSTILE_SECRET_KEY,
response: turnstileToken,
remoteip: ip,
}),
method: "POST",
headers: {
"Content-Type": "application/json",
},
},
},
);
);

const outcome = await result.json();
if (!outcome.success) {
return {
error: "Could not validate captcha.",
};
const outcome = await result.json();
if (!outcome.success) {
return {
error: "Could not validate captcha.",
};
}
}

const cookieStore = await cookies();
Expand Down
7 changes: 5 additions & 2 deletions apps/dashboard/src/lib/vercel-utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { isBrowser } from "utils/isBrowser";

export function isVercel() {
return !!(process.env.vercel || process.env.NEXT_PUBLIC_VERCEL_ENV);
}

export function getVercelEnv() {
const onVercel = process.env.vercel || process.env.NEXT_PUBLIC_VERCEL_ENV;
if (!onVercel) {
if (!isVercel()) {
return "development";
}
return (process.env.VERCEL_ENV ||
Expand Down
Loading