Skip to content

Commit

Permalink
make a function also get baseRequestUrl in /
Browse files Browse the repository at this point in the history
  • Loading branch information
gudnuf committed Nov 19, 2024
1 parent 102a44f commit e380882
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
14 changes: 14 additions & 0 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useEffect } from 'react';
import { useRouter } from 'next/router';
import { GetServerSideProps, GetServerSidePropsContext } from 'next/types';
import { getRequestedDomainFromRequest } from '@/utils/url';

export default function Home() {
const router = useRouter();
Expand All @@ -16,3 +18,15 @@ export default function Home() {

return;
}

export const getServerSideProps: GetServerSideProps = async (
context: GetServerSidePropsContext,
) => {
const baseRequestUrl = getRequestedDomainFromRequest(context.req);

return {
props: {
baseRequestUrl,
},
};
};
12 changes: 2 additions & 10 deletions src/pages/wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { useCashuContext } from '@/hooks/contexts/cashuContext';
import { PublicContact, TokenProps, GiftAsset, Currency } from '@/types';
import { findContactByPubkey, isContactsTrustedMint } from '@/lib/contactModels';
import { proofsLockedTo } from '@/utils/cashu';
import { formatUrl } from '@/utils/url';
import { formatUrl, getRequestedDomainFromRequest } from '@/utils/url';
import NotificationDrawer from '@/components/notifications/NotificationDrawer';
import { formatTokenAmount } from '@/utils/formatting';
import { findTokenByTxId } from '@/lib/tokenModels';
Expand Down Expand Up @@ -291,16 +291,8 @@ export const getServerSideProps: GetServerSideProps = async (
let giftPath = null;
let gift: GiftAsset | undefined = undefined;
const txid = context.query.txid as string;
const req = context.req;

let protocol = 'https:';
let host = req?.headers['x-forwarded-host'] || req?.headers.host || 'boardwalkcash.com';

if (host.includes('localhost')) {
protocol = 'http:';
}

const baseRequestUrl = `${protocol}//${host}`;
const baseRequestUrl = getRequestedDomainFromRequest(context.req);

if (txid && !token) {
const tokenEntry = await findTokenByTxId(txid);
Expand Down
10 changes: 10 additions & 0 deletions src/utils/url.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IncomingMessage } from 'http';
import { NextApiRequest } from 'next';
export const normalizeUrl = (url: string): string => {
url = url.trim();
Expand All @@ -22,3 +23,12 @@ export const getBaseURLFromRequest = (req: NextApiRequest) => {
const protocol = req.headers.referer?.split('://')[0] || 'https';
return `${protocol}://${host}`;
};

export const getRequestedDomainFromRequest = (req: IncomingMessage) => {
const host = req.headers.host;
let protocol = req.headers.referer?.split('://')[0] || 'https';
if (host?.includes('localhost') || host?.includes('127.0.0.1')) {
protocol = 'http';
}
return `${protocol}://${host}`;
};

0 comments on commit e380882

Please sign in to comment.