Skip to content

Commit

Permalink
chore(chrome-extension): snapshot test
Browse files Browse the repository at this point in the history
  • Loading branch information
tmilewski committed Sep 19, 2024
1 parent 2242fe2 commit de23c83
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
10 changes: 8 additions & 2 deletions packages/chrome-extension/src/internal/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export async function createClerkClient({
syncHost = process.env.CLERK_SYNC_HOST,
syncSessionWithTab = false,
}: CreateClerkClientOptions): Promise<Clerk> {
console.log('createClerkClient (props):', { publishableKey, scope, storageCache, syncHost, syncSessionWithTab });

if (clerk) {
return clerk;
}
Expand All @@ -52,12 +54,16 @@ export async function createClerkClient({
});

// Set up JWT handler and attempt to get JWT from storage on initialization
const jwt = JWTHandler(storageCache, {

const jwtOptions = {
frontendApi: key.frontendApi,
name: isProd ? CLIENT_JWT_KEY : DEV_BROWSER_JWT_KEY,
sync: syncSessionWithTab,
url: syncHost || isProd ? `https://${key.frontendApi}` : DEFAULT_LOCAL_HOST_PERMISSION,
});
};

console.log('JWTHandler (options):', jwtOptions);
const jwt = JWTHandler(storageCache, jwtOptions);

// Create Clerk instance
clerk = new Clerk(publishableKey);
Expand Down
12 changes: 11 additions & 1 deletion packages/chrome-extension/src/internal/utils/jwt-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ export function JWTHandler(store: StorageCache, params: JWTHandlerParams) {
* @param value: JWT generally from the cookie or authorization header
*/
const set = async (value: string): Promise<void> => {
console.log('JWTHandler.set:', CACHE_KEY, value);
return await store.set(CACHE_KEY, value).catch(errorLogger);
};

/**
* Remove the JWT value
*/
const remove = async (): Promise<void> => {
console.log('JWTHandler.remove:', CACHE_KEY);
return await store.remove(CACHE_KEY).catch(errorLogger);
};

Expand All @@ -44,19 +46,27 @@ export function JWTHandler(store: StorageCache, params: JWTHandlerParams) {
* If not set, attempt to get it from the synced session and save for later use.
*/
const get = async () => {
console.log('JWTHandler.get:', CACHE_KEY);
console.log('JWTHandler.get (shouldSync):', shouldSync(sync, cookieParams));

if (shouldSync(sync, cookieParams)) {
console.dir({ cookieParams, sync, shouldSync: true });
// Get client cookie from browser
const syncedJWT = await getClientCookie(cookieParams).catch(errorLogger);

console.log('JWTHandler.get (syncedJWT):', syncedJWT);
if (syncedJWT) {
// Set client cookie in StorageCache
await set(syncedJWT.value);
return syncedJWT.value;
}
}

const value = await store.get<string>(CACHE_KEY);
console.log('JWTHandler.get (value):', value);

// Get current JWT from StorageCache
return await store.get<string>(CACHE_KEY);
return value;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export function requestHandler(jwtHandler: JWTHandler) {

requestInit.url?.searchParams.append('_is_native', '1');
(requestInit.headers as Headers).set(AUTH_HEADER, `Bearer ${currentJWT}`);

console.log('requestHandler (searchParams):', requestInit.url?.searchParams.toString());
console.log('requestHandler (authHeader):', AUTH_HEADER, `Bearer ${currentJWT}`);
};

return handler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export function responseHandler(jwtHandler: JWTHandler) {
const handler: Handler = async (_, response) => {
const authHeader = response?.headers.get(AUTH_HEADER);

console.log('responseHandler (authHeader):', authHeader);

if (authHeader?.startsWith('Bearer')) {
const newJWT = authHeader.split(' ')[1] || undefined;

Expand Down

0 comments on commit de23c83

Please sign in to comment.