Skip to content

Commit

Permalink
Finish singup, change README
Browse files Browse the repository at this point in the history
  • Loading branch information
mrruby committed Mar 25, 2024
1 parent 53d8440 commit 6729ad5
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 73 deletions.
73 changes: 30 additions & 43 deletions holo-key-manager-extension/scripts/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ const handleError = (sendResponse: SendResponseWithSender) => {
sendResponse({ action: GENERIC_ERROR });
};

const createWindowProperties = (actionPayload: ActionPayload): WindowProperties => {
const queryParams =
actionPayload.action === SIGN_UP
? createQueryParams({
happName: actionPayload.payload.happName,
happId: actionPayload.payload.happId
})
: '';
const createWindowProperties = ({
happId,
happName
}: {
happId?: string;
happName?: string;
}): WindowProperties => {
const queryParams = createQueryParams({ happId, happName });
const urlSuffix = queryParams ? `?${queryParams}` : '';
return {
url: `sign-up-key/setup.html${urlSuffix}`,
url: `webapp-extension/setup.html${urlSuffix}`,
type: 'popup',
height: 500,
width: 375,
Expand All @@ -46,21 +46,23 @@ const createWindowProperties = (actionPayload: ActionPayload): WindowProperties
};
};

const updateOrCreateWindow = (
const updateOrCreateWindow = async (
successAction: typeof SIGN_UP_STARTED | typeof SUCCESS | typeof NEEDS_SETUP,
sendResponse: SendResponseWithSender,
actionPayload: ActionPayload
happId?: string,
happName?: string
) => {
const windowProperties = createWindowProperties(actionPayload);
const windowProperties = createWindowProperties({ happId, happName });
const handleWindowUpdateOrCreate = () =>
chrome.runtime.lastError ? handleError(sendResponse) : sendResponse({ action: SUCCESS });
chrome.runtime.lastError ? handleError(sendResponse) : sendResponse({ action: successAction });

const updateWindow = (updateWindowId: number) =>
if (typeof windowId === 'number') {
chrome.windows.update(
updateWindowId,
windowId,
{ ...windowProperties, focused: true },
handleWindowUpdateOrCreate
);
const createWindow = () =>
} else {
chrome.windows.create(windowProperties, (newWindow) => {
if (!newWindow) return;
windowId = newWindow.id;
Expand All @@ -69,52 +71,37 @@ const updateOrCreateWindow = (
});
handleWindowUpdateOrCreate();
});

typeof windowId === 'number' ? updateWindow(windowId) : createWindow();
};
const createAndFocusWindow = async (
sendResponse: SendResponseWithSender,
actionPayload: ActionPayload
) => {
if (typeof windowId === 'number') {
updateOrCreateWindow(sendResponse, actionPayload);
return true;
}
return false;
};

const handleAction = async (actionPayload: ActionPayload, sendResponse: SendResponseWithSender) => {
if (!(await createAndFocusWindow(sendResponse, actionPayload))) {
updateOrCreateWindow(sendResponse, actionPayload);
}
sendResponse(actionPayload);
};

const processMessage = async (message: Message, sendResponse: SendResponse) => {
const sendResponseWithSender = (response: ActionPayload) =>
sendResponse({ ...response, sender: SENDER_EXTENSION });

const parsedMessage = MessageWithIdSchema.safeParse(message);
if (!parsedMessage.success) return;

try {
const setupComplete = await isSetupComplete();
if (!setupComplete) {
return handleAction({ action: NEEDS_SETUP }, sendResponseWithSender);
if (!(await isSetupComplete())) {
return updateOrCreateWindow(NEEDS_SETUP, sendResponseWithSender);
}

if (parsedMessage.data.action === SIGN_UP) {
return handleAction({ action: SIGN_UP_STARTED }, sendResponseWithSender);
return updateOrCreateWindow(
SIGN_UP_STARTED,
sendResponseWithSender,
parsedMessage.data.payload.happId,
parsedMessage.data.payload.happName
);
}

const signUpIncomplete =
if (
parsedMessage.data.action === SIGN_IN &&
!(await isAppSignUpComplete(parsedMessage.data.payload.happId));
if (signUpIncomplete) {
!(await isAppSignUpComplete(parsedMessage.data.payload.happId))
) {
return sendResponseWithSender({ action: NO_KEY_FOR_HAPP });
}

return handleAction({ action: SUCCESS }, sendResponseWithSender);
return updateOrCreateWindow(SUCCESS, sendResponseWithSender);
} catch (error) {
handleError(sendResponseWithSender);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
{:else}
<ActionPage
outerWindow={true}
mainAction={() => window.open('/setup-pass/start.html', '_blank')}
mainAction={() => {
window.open('/setup-pass/start.html', '_blank');
window.close();
}}
mainActionLabel="Setup"
title="Setup Required"
subTitle="You are yet to setup Holo Key Manager. Click “start setup” to begin."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,21 @@
/>
<Input label="Name This Key:" bind:value={keyName} extraProps="mb-4" error={errors.keyName} />

{#if $applicationKeyMutation.error}
<div class="mutation-error">
<p>Error: {$applicationKeyMutation.error.message}</p>
</div>
{/if}

<Button
label="Create keys"
onClick={() => {
if (validateAllFields()) {
$applicationKeyMutation.mutate(
{ app_key_name: keyName, happId: $extractHAppDetailsFromUrl.happId },
{ onSuccess: () => console.log('success'), onError: () => console.log('error') }
{
onSuccess: window.close
}
);
}
}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="flex flex-col items-center justify-center">
<h1 class="mt-4 text-2xl font-bold">Login Page</h1>
</div>
2 changes: 1 addition & 1 deletion holo-key-manager-extension/static/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Holo key manager",
"description": "A browser extension to manage holo keys",
"version": "0.0.38",
"version": "0.0.39",
"manifest_version": 3,
"action": {
"default_title": "Holo key manager",
Expand Down
99 changes: 72 additions & 27 deletions holo-key-manager-js-client/README.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,87 @@
# Holo Key Manager JS Client

This is a JavaScript client for the Holo Key Manager.
This JavaScript client library facilitates interactions with the Holo Key Manager, streamlining user signup processes and other key management functionalities. For full functionality, ensure the Holo Key Manager browser extension is installed.

## Install Holo Key Manager
## Prerequisites

To fully utilize the Holo Key Manager JS Client, you need to install the Holo Key Manager extension for your browser. Here are the links for each browser:
Before using this library, install the Holo Key Manager extension for your browser:

- [Chrome](https://chrome.google.com/webstore/detail/holo-key-manager/eggfhkdnfdhdpmkfpihjjbnncgmhihce)
- [Edge](https://microsoftedge.microsoft.com/addons/detail/jfecdgefjljjfcflgbhgfkbeofjenceh)
- [Mozilla](https://addons.mozilla.org/en-US/firefox/addon/holo-key-manager/)
- Chrome: [Install from Chrome Web Store](https://chrome.google.com/webstore/detail/holo-key-manager/eggfhkdnfdhdpmkfpihjjbnncgmhihce)
- Edge: [Install from Microsoft Edge Add-ons](https://microsoftedge.microsoft.com/addons/detail/jfecdgefjljjfcflgbhgfkbeofjenceh)
- Mozilla: [Install from Firefox Browser Add-ons](https://addons.mozilla.org/en-US/firefox/addon/holo-key-manager/)

## Installation and Usage
## Installation

### Adding the Library
1. Download the `tgz` file provided by the developer.
2. Include the library in your project by adding the following to your `package.json`:

To add this library as a dependency, download the `tgz` file from the developer. Then, add the following to your `package.json`:
```json
"dependencies": {
"holo-key-manager-js-client": "file:<path_to_downloaded_tgz_file>"
}
```

```json
"dependencies": {
"holo-key-manager-js-client": "file:<path_to_your_downloaded_tgz_file>"
}
```
## Usage

To use the library, import and initialize the Holo Key Manager JS Client with your application's details. Then, use the `signUp` method to initiate the signup process.

### Testing
### Basic Setup

```javascript
import createHoloKeyManager from 'holo-key-manager-js-client';

// Create a new instance of the Holo Key Manager
const keyManager = createHoloKeyManager();

// Use the openWindow method
keyManager
.openWindow()
.then(() => {
console.log('Window opened successfully');
})
.catch((error) => {
console.error('Failed to open window:', error);
});
const holoKeyManagerConfig = {
happId: 'your-happId',
happName: 'your-happName',
happLogo: 'https://example.com/happLogo.png',
happUiUrl: 'https://example.com/ui',
requireRegistrationCode: true
};

const initiateSignUp = async () => {
const { signUp } = createHoloKeyManager(holoKeyManagerConfig);
try {
const response = await signUp();
handleSignUpResponse(response);
} catch (error) {
handleSignUpError(error);
}
};
```

### Handling Responses

```javascript
const handleSignUpResponse = (response) => {
switch (response.action) {
case 'NeedsSetup':
// Prompt the user to complete the extension setup, then retry signup.
break;
case 'SignUpStarted':
// Notify the user to follow the extension's instructions, then redirect to login on success.
break;
default:
// Handle other actions or statuses.
break;
}
};
```

### Error Handling

```javascript
const handleSignUpError = (error) => {
const errorMessage = getErrorMessage(error);
console.error(errorMessage);
};

const getErrorMessage = (error) => {
if (error.message.includes('permissions are not granted')) {
return 'Ensure the extension is installed and permissions are granted in Firefox.';
} else if (error.message.includes('not installed')) {
return 'Install the Holo Key Manager extension in Chrome/Edge to proceed.';
} else {
return 'An unexpected error occurred. Please try again.';
}
};
```

0 comments on commit 6729ad5

Please sign in to comment.