Skip to content

Commit

Permalink
fix: patch credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
capJavert committed Nov 22, 2024
1 parent 2be5bbe commit 2edee40
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 19 deletions.
3 changes: 2 additions & 1 deletion packages/shared/src/components/GrowthBookProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { apiUrl } from '../lib/config';
import { useRequestProtocol } from '../hooks/useRequestProtocol';
import { feature as localFeature, Feature } from '../lib/featureManagement';
import { useViewSize, ViewSize } from '../hooks/useViewSize';
import { withCredentials } from '../lib/withCredentials';

const ServerError = dynamic(
() => import(/* webpackChunkName: "serverError" */ './errors/ServerError'),
Expand Down Expand Up @@ -81,7 +82,7 @@ export const GrowthBookProvider = ({
experiment_id: data.experimentId,
variation_id: data.variationId,
}),
credentials: 'include',
credentials: withCredentials('include'),
headers: {
'content-type': 'application/json',
},
Expand Down
3 changes: 2 additions & 1 deletion packages/shared/src/components/modals/NewSourceModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { ReputationAlert } from './ReputationAlert';
import { RequestKey } from '../../lib/query';
import { ProfileImageSize } from '../ProfilePicture';
import { gqlClient } from '../../graphql/common';
import { withCredentials } from '../../lib/withCredentials';

interface RSS {
url: string;
Expand Down Expand Up @@ -136,7 +137,7 @@ export default function NewSourceModal(props: ModalProps): ReactElement {
`${apiUrl}/scrape/source?url=${url}`,
20000,
{
credentials: 'same-origin',
credentials: withCredentials('same-origin'),
},
);
return res.json();
Expand Down
3 changes: 2 additions & 1 deletion packages/shared/src/graphql/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { QueryKey, UseInfiniteQueryOptions } from '@tanstack/react-query';
import { GraphQLError } from 'graphql-request/dist/types';
import type { PublicProfile, UserShortProfile } from '../lib/user';
import { graphqlUrl } from '../lib/config';
import { withCredentials } from '../lib/withCredentials';
// GraphQL Relay pagination types

export type ConnectionCursor = string;
Expand Down Expand Up @@ -140,7 +141,7 @@ export interface ResponseError {
}

export const gqlClient = new GraphQLClient(graphqlUrl, {
credentials: 'include',
credentials: withCredentials('include'),
fetch: globalThis.fetch,
});

Expand Down
5 changes: 3 additions & 2 deletions packages/shared/src/hooks/log/useLogQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { MutableRefObject, useMemo, useRef } from 'react';
import { apiUrl } from '../../lib/config';
import useDebounceFn from '../useDebounceFn';
import { ExtensionMessageType } from '../../lib/extension';
import { withCredentials } from '../../lib/withCredentials';

export interface LogEvent extends Record<string, unknown> {
visit_id?: string;
Expand Down Expand Up @@ -37,7 +38,7 @@ export default function useLogQueue({
const res = await fetchMethod(LOG_ENDPOINT, {
method: 'POST',
body: JSON.stringify({ events }),
credentials: 'include',
credentials: withCredentials('include'),
headers: {
'content-type': 'application/json',
},
Expand Down Expand Up @@ -84,7 +85,7 @@ export default function useLogQueue({
type: ExtensionMessageType.FetchRequest,
args: {
body: JSON.stringify({ events }),
credentials: 'include',
credentials: withCredentials('include'),
method: 'POST',
headers: {
'content-type': 'application/json',
Expand Down
3 changes: 2 additions & 1 deletion packages/shared/src/hooks/useAutomation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
useMutation,
} from '@tanstack/react-query';
import { HttpError } from '../lib/errors';
import { withCredentials } from '../lib/withCredentials';

export enum Automation {
Roaster = 'roaster',
Expand All @@ -28,7 +29,7 @@ export function useAutomation<
const url = `${process.env.NEXT_PUBLIC_API_URL}/auto/${name}`;
const res = await fetch(url, {
method: 'POST',
credentials: 'include',
credentials: withCredentials('include'),
headers: { 'content-type': 'application/json' },
body: JSON.stringify(vars),
keepalive: true,
Expand Down
3 changes: 2 additions & 1 deletion packages/shared/src/lib/boot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Squad } from '../graphql/sources';
import { decrypt } from '../components/crypto';
import { MarketingCta } from '../components/marketingCta/common';
import { Feed } from '../graphql/feed';
import { withCredentials } from './withCredentials';

interface NotificationsBootData {
unreadNotificationsCount: number;
Expand Down Expand Up @@ -94,7 +95,7 @@ export async function getBootData(app: string, url?: string): Promise<Boot> {

const res = await fetch(`${apiUrl}/boot${appRoute}?${params}`, {
method: 'GET',
credentials: 'include',
credentials: withCredentials('include'),
headers: { app, 'Content-Type': 'application/json' },
});
const result = await res.json();
Expand Down
17 changes: 9 additions & 8 deletions packages/shared/src/lib/kratos.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { withCredentials } from './withCredentials';
import { authUrl, heimdallUrl } from './constants';

export type EmptyObjectLiteral = Record<string, never | string>;
Expand Down Expand Up @@ -221,7 +222,7 @@ export const initializeKratosFlow = async (
): Promise<InitializationData> => {
const search = new URLSearchParams(params);
const res = await fetch(`${authUrl}/self-service${flow}/browser?${search}`, {
credentials: 'include',
credentials: withCredentials('include'),
headers: { Accept: 'application/json' },
});

Expand All @@ -233,7 +234,7 @@ export const getKratosSettingsFlow = async (
id: string,
): Promise<InitializationData> => {
const res = await fetch(`${authUrl}/self-service${flow}/flows?id=${id}`, {
credentials: 'include',
credentials: withCredentials('include'),
headers: { Accept: 'application/json' },
});
return res.json();
Expand All @@ -244,15 +245,15 @@ export const getKratosFlow = async <T = InitializationData>(
id: string,
): Promise<T> => {
const res = await fetch(`${authUrl}/self-service${flow}?flow=${id}`, {
credentials: 'include',
credentials: withCredentials('include'),
headers: { Accept: 'application/json' },
});
return res.json();
};

export const getKratosError = async (id: string): Promise<ErrorData> => {
const res = await fetch(`${authUrl}/self-service/errors?id=${id}`, {
credentials: 'include',
credentials: withCredentials('include'),
headers: { Accept: 'application/json' },
});
return res.json();
Expand Down Expand Up @@ -296,7 +297,7 @@ export const getKratosProviders = async (
): Promise<KratosProviderData> => {
const search = flow ? new URLSearchParams({ flow }) : '';
const res = await fetch(`${heimdallUrl}/api/list_providers?${search}`, {
credentials: 'include',
credentials: withCredentials('include'),
method: flow ? 'GET' : 'POST',
});
return res.json();
Expand All @@ -313,7 +314,7 @@ export const submitKratosFlow = async <
}: KratosFormParams<T>): Promise<RequestResponse<R, E>> => {
const res = await fetch(action, {
method,
credentials: 'include',
credentials: withCredentials('include'),
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': params.csrf_token,
Expand Down Expand Up @@ -350,7 +351,7 @@ interface KratosSession {

export const getKratosSession = async (): Promise<KratosSession> => {
const res = await fetch(`${heimdallUrl}/api/whoami`, {
credentials: 'include',
credentials: withCredentials('include'),
});

if (res.status === 401) {
Expand All @@ -363,7 +364,7 @@ export const getKratosSession = async (): Promise<KratosSession> => {
export const getVerificationSession =
async (): Promise<VerificationSession> => {
const res = await fetch(`${heimdallUrl}/api/get_verification_flow`, {
credentials: 'include',
credentials: withCredentials('include'),
});

if (!res.ok) {
Expand Down
9 changes: 5 additions & 4 deletions packages/shared/src/lib/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import type { Company } from './userCompany';
import type { ContentPreference } from '../graphql/contentPreference';
import type { TopReader } from '../components/badges/TopReaderBadge';
import { withCredentials } from './withCredentials';

export enum Roles {
Moderator = 'moderator',
Expand Down Expand Up @@ -152,14 +153,14 @@ export async function logout(reason: string): Promise<void> {
const urlParams = reason ? `?${new URLSearchParams({ reason })}` : '';
await fetch(`${apiUrl}/v1/users/logout${urlParams}`, {
method: 'POST',
credentials: 'include',
credentials: withCredentials('include'),
});
}

export async function deleteAccount(): Promise<void> {
await fetch(`${apiUrl}/v1/users/me`, {
method: 'DELETE',
credentials: 'include',
credentials: withCredentials('include'),
});
}

Expand All @@ -175,7 +176,7 @@ const getProfileRequest = async (method = fetch, id: string) => {
id,
},
}),
credentials: 'include',
credentials: withCredentials('include'),
});
if (userRes.status === 404) {
throw new Error('not found');
Expand All @@ -200,7 +201,7 @@ const getProfileV2ExtraRequest = async (
id,
},
}),
credentials: 'include',
credentials: withCredentials('include'),
});
if (userRes.status === 404) {
throw new Error('not found');
Expand Down
11 changes: 11 additions & 0 deletions packages/shared/src/lib/withCredentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const isCredentialsSupported = 'credentials' in Request.prototype;

export const withCredentials = (
credentials: RequestInit['credentials'],
): RequestInit['credentials'] | undefined => {
if (!isCredentialsSupported) {
return undefined;
}

return credentials;
};

0 comments on commit 2edee40

Please sign in to comment.