Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
anagstef committed Nov 19, 2024
1 parent ec99c63 commit b45c540
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 5 deletions.
33 changes: 29 additions & 4 deletions packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ import {
EmailLinkError,
EmailLinkErrorCode,
Environment,
isClerkRuntimeError,
Organization,
Waitlist,
} from './resources/internal';
Expand Down Expand Up @@ -190,6 +191,11 @@ export class Clerk implements ClerkInterface {
#pageLifecycle: ReturnType<typeof createPageLifecycle> | null = null;
#touchThrottledUntil = 0;

public __internal_getEnvironment: (() => Promise<EnvironmentResource>) | undefined;
public __internal_getClient: (() => Promise<ClientResource>) | undefined;
public __internal_setEnvironment: ((environment: EnvironmentResource) => Promise<void>) | undefined;
public __internal_setClient: ((client: ClientResource) => Promise<void>) | undefined;

public __internal_createPublicCredentials:
| ((
publicKey: PublicKeyCredentialCreationOptionsWithoutExtensions,
Expand Down Expand Up @@ -1835,10 +1841,29 @@ export class Clerk implements ClerkInterface {
};

#loadInNonStandardBrowser = async (): Promise<boolean> => {
const [environment, client] = await Promise.all([
Environment.getInstance().fetch({ touch: false }),
Client.getInstance().fetch(),
]);
let environment, client;
try {
const [fetchedEnv, fetchedClient] = await Promise.all([
Environment.getInstance().fetch({ touch: false }),
Client.getInstance().fetch(),
]);
environment = fetchedEnv;
client = fetchedClient;
await this.__internal_setEnvironment?.(fetchedEnv);
await this.__internal_setClient?.(fetchedClient);
} catch (err) {
if (isClerkRuntimeError(err) && err.code === 'network_error') {
console.log('Clerk: using cached environment and client');
environment = await this.__internal_getEnvironment?.();
client = await this.__internal_getClient?.();
} else {
throw err;
}
}

if (!environment || !client) {
return false;
}

this.updateClient(client);
this.updateEnvironment(environment);
Expand Down
3 changes: 3 additions & 0 deletions packages/expo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@
"@clerk/expo-passkeys": "0.0.6",
"@clerk/shared": "2.15.0",
"@clerk/types": "4.34.0",
"@react-native-async-storage/async-storage": "^2.1.0",
"base-64": "^1.0.0",
"react-native-url-polyfill": "2.0.0",
"tslib": "2.4.1"
},
"devDependencies": {
"@clerk/eslint-config-custom": "workspace:*",
"@react-native-async-storage/async-storage": "^2.1.0",
"@types/base-64": "^1.0.2",
"@types/node": "^20.11.24",
"@types/react": "18.3.12",
Expand All @@ -83,6 +85,7 @@
"typescript": "*"
},
"peerDependencies": {
"@react-native-async-storage/async-storage": ">=2",
"expo-auth-session": ">=5",
"expo-local-authentication": ">=13.5.0",
"expo-secure-store": ">=12.4.0",
Expand Down
23 changes: 23 additions & 0 deletions packages/expo/src/provider/singleton/createClerkInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
PublicKeyCredentialCreationOptionsWithoutExtensions,
PublicKeyCredentialRequestOptionsWithoutExtensions,
} from '@clerk/types';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { Platform } from 'react-native';

import { MemoryTokenCache } from '../../cache/MemoryTokenCache';
Expand Down Expand Up @@ -81,6 +82,28 @@ export function createClerkInstance(ClerkClass: typeof Clerk) {
__internal_clerk.__internal_isWebAuthnPlatformAuthenticatorSupported = () => {
return Promise.resolve(true);
};

// @ts-expect-error - This is an internal API
__internal_clerk.__internal_setClient = async (client: any) => {
await AsyncStorage.setItem('clerk_client', JSON.stringify(client));
};

// @ts-expect-error - This is an internal API
__internal_clerk.__internal_getClient = async () => {
const client = await AsyncStorage.getItem('clerk_client');
return client ? JSON.parse(client) : null;
};

// @ts-expect-error - This is an internal API
__internal_clerk.__internal_setEnvironment = async (environment: any) => {
await AsyncStorage.setItem('clerk_environment', JSON.stringify(environment));
};

// @ts-expect-error - This is an internal API
__internal_clerk.__internal_getEnvironment = async () => {
const environment = await AsyncStorage.getItem('clerk_environment');
return environment ? JSON.parse(environment) : null;
};
}

// @ts-expect-error - This is an internal API
Expand Down
29 changes: 28 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b45c540

Please sign in to comment.