From 6f6f403547d4a57fd9b7297c23fb16da64442c91 Mon Sep 17 00:00:00 2001 From: Hafiz Mohsin Ayoob Date: Sun, 10 Nov 2024 12:54:40 +0500 Subject: [PATCH 1/3] fix: resolves the build and redirect issue --- src/api.ts | 3 ++- src/pages/auth.tsx | 13 ++++++++----- src/utils/api.ts | 3 ++- src/utils/apiPaths.ts | 1 + src/utils/auth/api.ts | 24 ++++++++++++++++++++++-- src/utils/url.ts | 12 +++++++++--- 6 files changed, 44 insertions(+), 12 deletions(-) diff --git a/src/api.ts b/src/api.ts index d0ae10e21c..620902f1f5 100644 --- a/src/api.ts +++ b/src/api.ts @@ -2,6 +2,8 @@ import { camelizeKeys } from 'humps'; import { NextApiRequest } from 'next'; +import { isStaticBuild } from './utils/api'; + import { SearchRequestParams, SearchMode } from '@/types/Search/SearchRequestParams'; import NewSearchResponse from '@/types/Search/SearchResponse'; import { @@ -65,7 +67,6 @@ export const X_AUTH_SIGNATURE = 'x-auth-signature'; export const X_TIMESTAMP = 'x-timestamp'; export const X_INTERNAL_CLIENT = 'x-internal-client'; -const isStaticBuild = process.env.IS_BUILD_TIME === 'true'; export const fetcher = async function fetcher( input: RequestInfo, init: RequestInit = {}, diff --git a/src/pages/auth.tsx b/src/pages/auth.tsx index fd82368123..05b19cd4a6 100644 --- a/src/pages/auth.tsx +++ b/src/pages/auth.tsx @@ -12,6 +12,8 @@ interface AuthProps { error?: string; } +const LOGIN_URL = '/login'; + const Auth: React.FC = ({ error }) => { const router = useRouter(); const toast = useToast(); @@ -37,14 +39,14 @@ const Auth: React.FC = ({ error }) => { * * @param {GetServerSidePropsContext} context - The context object containing request and response information. * @param {string} token - The token used for authentication and redirection. - * @param {string} redirectUrl - The URL to redirect the user to after successful token handling. + * @param {string} destination - The URL to redirect the user to after successful token handling. * @returns {Promise>} - A promise that resolves to the server-side props result, * which includes either a redirection or an error message. */ const handleTokenRedirection = async ( context: GetServerSidePropsContext, token: string, - redirectUrl: string, + destination: string, ): Promise> => { try { const baseUrl = getBaseUrl(context); @@ -59,7 +61,7 @@ const handleTokenRedirection = async ( return { props: {}, redirect: { - destination: redirectUrl, + destination, permanent: false, }, }; @@ -128,15 +130,16 @@ const setProxyCookies = (response: Response, context: GetServerSidePropsContext) export const getServerSideProps: GetServerSideProps = async (context) => { const { r, token } = context.query; const redirectUrl = (r || '/') as string; + const destination = redirectUrl === LOGIN_URL ? '/' : redirectUrl; if (token) { - return handleTokenRedirection(context, token as string, redirectUrl); + return handleTokenRedirection(context, token as string, destination); } return { props: {}, redirect: { - destination: redirectUrl, + destination, permanent: false, }, }; diff --git a/src/utils/api.ts b/src/utils/api.ts index d5b74c1264..b913cd4614 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -1,6 +1,7 @@ import { decamelizeKeys } from 'humps'; import stringify from './qs-stringify'; +// eslint-disable-next-line import/no-cycle import { getBasePath } from './url'; import { Mushaf, MushafLines, QuranFont, QuranFontMushaf } from 'types/QuranReader'; @@ -17,7 +18,7 @@ export const API_HOST = process.env.NEXT_PUBLIC_VERCEL_ENV === 'production' ? PRODUCTION_API_HOST : STAGING_API_HOST; const { API_GATEWAY_URL } = process.env; -const isStaticBuild = process.env.IS_BUILD_TIME === 'true'; +export const isStaticBuild = process.env.IS_BUILD_TIME === 'true'; /** * Generates a url to make an api call to our backend diff --git a/src/utils/apiPaths.ts b/src/utils/apiPaths.ts index 547f73a520..39f9ffa5f0 100644 --- a/src/utils/apiPaths.ts +++ b/src/utils/apiPaths.ts @@ -1,5 +1,6 @@ import { decamelizeKeys } from 'humps'; +// eslint-disable-next-line import/no-cycle import { getDefaultWordFields, getMushafId, ITEMS_PER_PAGE, makeUrl } from './api'; import stringify from './qs-stringify'; diff --git a/src/utils/auth/api.ts b/src/utils/auth/api.ts index ea9954aa0c..967e787555 100644 --- a/src/utils/auth/api.ts +++ b/src/utils/auth/api.ts @@ -1,12 +1,16 @@ /* eslint-disable max-lines */ +import { NextApiRequest } from 'next'; import { configureRefreshFetch } from 'refresh-fetch'; +import { isStaticBuild } from '../api'; import { getTimezone } from '../datetime'; import { prepareGenerateMediaFileRequestData } from '../media/utils'; +import generateSignature from './signature'; import BookmarkByCollectionIdQueryParams from './types/BookmarkByCollectionIdQueryParams'; import GetAllNotesQueryParams from './types/Note/GetAllNotesQueryParams'; +import { fetcher, X_AUTH_SIGNATURE, X_INTERNAL_CLIENT, X_TIMESTAMP } from '@/api'; import { FilterActivityDaysParams, QuranActivityDay, @@ -72,7 +76,6 @@ import { makeGetMediaFileProgressUrl, makeGetMonthlyMediaFilesCountUrl, } from '@/utils/auth/apiPaths'; -import { fetcher } from 'src/api'; import CompleteAnnouncementRequest from 'types/auth/CompleteAnnouncementRequest'; import { GetBookmarkCollectionsIdResponse } from 'types/auth/GetBookmarksByCollectionId'; import PreferenceGroup from 'types/auth/PreferenceGroup'; @@ -88,7 +91,6 @@ import { Collection } from 'types/Collection'; import CompleteSignupRequest from 'types/CompleteSignupRequest'; type RequestData = Record; - const IGNORE_ERRORS = [ MediaRenderError.MediaVersesRangeLimitExceeded, MediaRenderError.MediaFilesPerUserLimitExceeded, @@ -445,6 +447,23 @@ export const withCredentialsFetcher = async ( init?: RequestInit, ): Promise => { try { + let additionalHeaders = {}; + if (isStaticBuild) { + const req: NextApiRequest = { + url: typeof input === 'string' ? input : input.url, + method: init.method || 'GET', + body: init.body, + headers: init.headers, + query: {}, + } as NextApiRequest; + + const { signature, timestamp } = generateSignature(req, req.url); + additionalHeaders = { + [X_AUTH_SIGNATURE]: signature, + [X_TIMESTAMP]: timestamp, + [X_INTERNAL_CLIENT]: process.env.INTERNAL_CLIENT_ID, + }; + } const data = await fetcher(input, { ...init, credentials: 'include', @@ -452,6 +471,7 @@ export const withCredentialsFetcher = async ( ...init?.headers, // eslint-disable-next-line @typescript-eslint/naming-convention 'x-timezone': getTimezone(), + ...additionalHeaders, }, }); return data; diff --git a/src/utils/url.ts b/src/utils/url.ts index eca2f7c570..1697c19302 100644 --- a/src/utils/url.ts +++ b/src/utils/url.ts @@ -1,3 +1,6 @@ +// eslint-disable-next-line import/no-cycle +import { isStaticBuild } from './api'; + const getLocalePostfix = (locale: string) => (locale !== 'en' ? `/${locale}` : ''); export const getCurrentPath = () => { @@ -43,7 +46,7 @@ export const navigateToExternalUrl = (url: string) => { * @returns {string} */ export const getBasePath = (): string => - `${process.env.NEXT_PUBLIC_VERCEL_ENV === 'development' ? 'http' : 'https'}://${ + `${process.env.NEXT_PUBLIC_VERCEL_ENV === 'development' ? 'https' : 'https'}://${ process.env.NEXT_PUBLIC_VERCEL_URL }`; @@ -54,6 +57,9 @@ export const getBasePath = (): string => * @returns {string} */ export const getAuthApiPath = (path: string): string => { - const BASE_PATH = getBasePath(); - return `${BASE_PATH}/api/proxy/auth/${path}`; + const PROXY_PATH = '/api/proxy/auth/'; + const BASE_PATH = isStaticBuild + ? `${process.env.API_GATEWAY_URL}/auth/` + : `${getBasePath()}${PROXY_PATH}`; + return `${BASE_PATH}${path}`; }; From c6d438add9288059b713d8fe8bd92f95de1f8281 Mon Sep 17 00:00:00 2001 From: Osama Sayed Date: Sun, 10 Nov 2024 22:20:01 +0800 Subject: [PATCH 2/3] Fix cyclic dependency issue --- src/api.ts | 3 +-- src/utils/api.ts | 5 ++--- src/utils/auth/api.ts | 2 +- src/utils/build.ts | 2 ++ src/utils/url.ts | 3 +-- 5 files changed, 7 insertions(+), 8 deletions(-) create mode 100644 src/utils/build.ts diff --git a/src/api.ts b/src/api.ts index 620902f1f5..06f3f93786 100644 --- a/src/api.ts +++ b/src/api.ts @@ -2,8 +2,6 @@ import { camelizeKeys } from 'humps'; import { NextApiRequest } from 'next'; -import { isStaticBuild } from './utils/api'; - import { SearchRequestParams, SearchMode } from '@/types/Search/SearchRequestParams'; import NewSearchResponse from '@/types/Search/SearchResponse'; import { @@ -32,6 +30,7 @@ import { makeWordByWordTranslationsUrl, } from '@/utils/apiPaths'; import generateSignature from '@/utils/auth/signature'; +import { isStaticBuild } from '@/utils/build'; import { SearchRequest, AdvancedCopyRequest, PagesLookUpRequest } from 'types/ApiRequests'; import { TranslationsResponse, diff --git a/src/utils/api.ts b/src/utils/api.ts index b913cd4614..715c2f6387 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -1,10 +1,10 @@ import { decamelizeKeys } from 'humps'; import stringify from './qs-stringify'; -// eslint-disable-next-line import/no-cycle import { getBasePath } from './url'; -import { Mushaf, MushafLines, QuranFont, QuranFontMushaf } from 'types/QuranReader'; +import type { Mushaf, MushafLines, QuranFont, QuranFontMushaf } from '@/types/quran'; +import { isStaticBuild } from '@/utils/build'; export const ITEMS_PER_PAGE = 10; @@ -18,7 +18,6 @@ export const API_HOST = process.env.NEXT_PUBLIC_VERCEL_ENV === 'production' ? PRODUCTION_API_HOST : STAGING_API_HOST; const { API_GATEWAY_URL } = process.env; -export const isStaticBuild = process.env.IS_BUILD_TIME === 'true'; /** * Generates a url to make an api call to our backend diff --git a/src/utils/auth/api.ts b/src/utils/auth/api.ts index 967e787555..a9f6b6e8a0 100644 --- a/src/utils/auth/api.ts +++ b/src/utils/auth/api.ts @@ -2,7 +2,6 @@ import { NextApiRequest } from 'next'; import { configureRefreshFetch } from 'refresh-fetch'; -import { isStaticBuild } from '../api'; import { getTimezone } from '../datetime'; import { prepareGenerateMediaFileRequestData } from '../media/utils'; @@ -76,6 +75,7 @@ import { makeGetMediaFileProgressUrl, makeGetMonthlyMediaFilesCountUrl, } from '@/utils/auth/apiPaths'; +import { isStaticBuild } from '@/utils/build'; import CompleteAnnouncementRequest from 'types/auth/CompleteAnnouncementRequest'; import { GetBookmarkCollectionsIdResponse } from 'types/auth/GetBookmarksByCollectionId'; import PreferenceGroup from 'types/auth/PreferenceGroup'; diff --git a/src/utils/build.ts b/src/utils/build.ts new file mode 100644 index 0000000000..6e7ef2dc9c --- /dev/null +++ b/src/utils/build.ts @@ -0,0 +1,2 @@ +// eslint-disable-next-line import/prefer-default-export +export const isStaticBuild = process.env.IS_BUILD_TIME === 'true'; diff --git a/src/utils/url.ts b/src/utils/url.ts index 1697c19302..ac266250fd 100644 --- a/src/utils/url.ts +++ b/src/utils/url.ts @@ -1,5 +1,4 @@ -// eslint-disable-next-line import/no-cycle -import { isStaticBuild } from './api'; +import { isStaticBuild } from '@/utils/build'; const getLocalePostfix = (locale: string) => (locale !== 'en' ? `/${locale}` : ''); From 2a73d28396b3ce20e8382aac7ead0e857c731313 Mon Sep 17 00:00:00 2001 From: Osama Sayed Date: Sun, 10 Nov 2024 22:26:13 +0800 Subject: [PATCH 3/3] fix TS issue --- src/utils/api.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/api.ts b/src/utils/api.ts index 715c2f6387..52a92bc0f7 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -3,8 +3,8 @@ import { decamelizeKeys } from 'humps'; import stringify from './qs-stringify'; import { getBasePath } from './url'; -import type { Mushaf, MushafLines, QuranFont, QuranFontMushaf } from '@/types/quran'; import { isStaticBuild } from '@/utils/build'; +import { Mushaf, MushafLines, QuranFont, QuranFontMushaf } from 'types/QuranReader'; export const ITEMS_PER_PAGE = 10;