Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: react native origin header #927

Merged
merged 1 commit into from
May 13, 2024
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
8 changes: 8 additions & 0 deletions .changeset/big-tomatoes-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@lens-protocol/api-bindings": patch
"@lens-protocol/react": patch
"@lens-protocol/react-native": patch
"@lens-protocol/react-web": patch
---

**fix:** custom origin header important for react native usecase
21 changes: 13 additions & 8 deletions packages/api-bindings/src/apollo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,25 @@ import { createLensCache } from './cache/createLensCache';
import { AuthLinkArgs, IAccessTokenStorage, createAuthLink, createLensLink } from './links';

export type ApolloClientConfig = AuthLinkArgs & {
uri: string;
connectToDevTools?: boolean;
logger: ILogger;
pollingInterval: number;
connectToDevTools?: boolean;
uri: string;
};

export function createLensApolloClient({
accessTokenStorage,
origin,
uri,
logger,
pollingInterval,
connectToDevTools,
}: ApolloClientConfig) {
const authLink = createAuthLink({ accessTokenStorage, origin });
const authLink = createAuthLink({ accessTokenStorage });

const httpLink = createLensLink({
uri,
logger,
supportedVersion: LENS_API_MINIMAL_SUPPORTED_VERSION,
uri,
});

return new SafeApolloClient({
Expand All @@ -39,14 +38,20 @@ export function createLensApolloClient({
}

export type AuthApolloClientConfig = {
uri: string;
logger: ILogger;
origin?: string;
uri: string;
};

export function createAuthApolloClient({ uri, logger }: AuthApolloClientConfig) {
export function createAuthApolloClient({ logger, origin, uri }: AuthApolloClientConfig) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why only for this ApolloClient and not the authenticated one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the origin overwrite is used only in the auth challenge and is not used later. or is it?
is the API doing anything else based on origin?

return new SafeApolloClient({
cache: createLensCache(),
link: createLensLink({ uri, logger, supportedVersion: LENS_API_MINIMAL_SUPPORTED_VERSION }),
link: createLensLink({
logger,
origin,
supportedVersion: LENS_API_MINIMAL_SUPPORTED_VERSION,
uri,
}),
version: LENS_API_MINIMAL_SUPPORTED_VERSION,
});
}
Expand Down
24 changes: 9 additions & 15 deletions packages/api-bindings/src/apollo/links/AuthLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,28 +116,22 @@ class RefreshTokensLink extends ApolloLink {

export type AuthLinkArgs = {
accessTokenStorage: IAccessTokenStorage;
origin?: string;
};

export function createAuthLink({ accessTokenStorage, origin }: AuthLinkArgs) {
export function createAuthLink({ accessTokenStorage }: AuthLinkArgs) {
const tokenRefreshLink = new RefreshTokensLink(accessTokenStorage);

const authHeaderLink = setContext((_, prevContext) => {
const token = accessTokenStorage.getAccessToken();

if (token) {
return {
...prevContext,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
headers: {
...('headers' in prevContext && prevContext.headers),
Authorization: `Bearer ${token}`,
...(origin && { Origin: origin }),
},
};
}

return prevContext;
return {
...prevContext,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
headers: {
...('headers' in prevContext && prevContext.headers),
...(token && { Authorization: `Bearer ${token}` }),
},
};
});

return from([tokenRefreshLink, authHeaderLink]);
Expand Down
6 changes: 5 additions & 1 deletion packages/api-bindings/src/apollo/links/LensLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ function wrapFetch(logger: ILogger, supportedVersion: SemVer, fetch: Fetch): Fet
export type LensLinkArgs = {
fetch?: Fetch;
logger: ILogger;
origin?: string;
supportedVersion: SemVer;
uri: string;
};

export function createLensLink({
fetch: preferredFetch,
logger,
origin,
supportedVersion,
uri,
}: LensLinkArgs) {
Expand All @@ -66,7 +68,9 @@ export function createLensLink({

return new HttpLink({
uri,

fetch: wrapFetch(logger, supportedVersion, currentFetch),
headers: {
...(origin && { origin: origin }),
},
});
}
2 changes: 1 addition & 1 deletion packages/react/src/shared.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export function createSharedDependencies(userConfig: BaseConfig): SharedDependen
const anonymousApolloClient = createAuthApolloClient({
uri: config.environment.backend,
logger: config.logger,
origin: config.origin,
});
const authApi = new AuthApi(anonymousApolloClient);

Expand All @@ -79,7 +80,6 @@ export function createSharedDependencies(userConfig: BaseConfig): SharedDependen
accessTokenStorage: credentialsStorage,
pollingInterval: config.environment.timings.pollingInterval,
logger: config.logger,
origin: config.origin,
});

// infrastructure
Expand Down
Loading