Skip to content

Commit

Permalink
Merge branch 'master' of github.com:torusresearch/OpenLoginSdk into f…
Browse files Browse the repository at this point in the history
…eat/auth-mpc
  • Loading branch information
arch1995 committed Sep 26, 2023
2 parents d678f0e + bbdf03d commit b583bf6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
8 changes: 7 additions & 1 deletion examples/vue-example/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<label for="mpc">Enable MPC</label>
<input type="checkbox" id="mpc" name="mpc" v-model="useMpc" />
</div>
<select v-model="selectedBuildEnv" class="select">
<option :key="login" v-for="login in Object.values(BUILD_ENV)" :value="login">{{ login }}</option>
</select>
<select v-model="selectedOpenloginNetwork" class="select">
<option :key="login" v-for="login in Object.values(OPENLOGIN_NETWORK)" :value="login">{{ login }}</option>
</select>
Expand Down Expand Up @@ -116,6 +119,7 @@ import {
UX_MODE_TYPE,
OPENLOGIN_NETWORK,
OPENLOGIN_NETWORK_TYPE,
BUILD_ENV,
} from "@toruslabs/openlogin-utils";
import loginConfig from "./lib/loginConfig";
import { keccak256 } from "ethereum-cryptography/keccak";
Expand Down Expand Up @@ -145,8 +149,10 @@ export default defineComponent({
UX_MODE: UX_MODE,
selectedUxMode: UX_MODE.REDIRECT as UX_MODE_TYPE,
OPENLOGIN_NETWORK: OPENLOGIN_NETWORK,
BUILD_ENV: BUILD_ENV,
selectedOpenloginNetwork: OPENLOGIN_NETWORK.SAPPHIRE_DEVNET as OPENLOGIN_NETWORK_TYPE,
useMpc: true,
selectedBuildEnv: BUILD_ENV.PRODUCTION,
};
},
async created() {
Expand Down Expand Up @@ -187,8 +193,8 @@ export default defineComponent({
uxMode: this.selectedUxMode,
whiteLabel: this.isWhiteLabelEnabled ? whitelabel : {},
loginConfig: loginConfig,
buildEnv: "development",
useMpc: this.useMpc,
buildEnv: this.selectedBuildEnv,
// sdkUrl: "https://staging.openlogin.com",
});
op.init();
Expand Down
11 changes: 11 additions & 0 deletions packages/openlogin/src/OpenLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class OpenLogin {

private _storageBaseKey = "openlogin_store";

private dappState: string;

constructor(options: OpenLoginOptions) {
if (!options.clientId) throw InitializationError.invalidParams("clientId is required");
if (!options.network) options.network = OPENLOGIN_NETWORK.SAPPHIRE_MAINNET;
Expand Down Expand Up @@ -106,6 +108,10 @@ class OpenLogin {
return this.options.sessionNamespace || "";
}

get appState(): string {
return this.state.userInfo.appState || this.dappState || "";
}

private get baseUrl(): string {
// testing and develop don't have versioning
if (this.options.buildEnv === BUILD_ENV.DEVELOPMENT || this.options.buildEnv === BUILD_ENV.TESTING) return `${this.options.sdkUrl}`;
Expand Down Expand Up @@ -142,6 +148,7 @@ class OpenLogin {
}

if (params.error) {
this.dappState = params.state;
throw LoginError.loginFailed(params.error);
}

Expand Down Expand Up @@ -185,6 +192,10 @@ class OpenLogin {

const result = await this.openloginHandler(`${this.baseUrl}/start`, dataObject, getTimeout(params.loginProvider));
if (this.options.uxMode === UX_MODE.REDIRECT) return undefined;
if (result.error) {
this.dappState = result.state;
throw LoginError.loginFailed(result.error);
}
this.sessionManager.sessionId = result.sessionId;
this.options.sessionNamespace = result.sessionNamespace;
this.currentStorage.set("sessionId", result.sessionId);
Expand Down
8 changes: 5 additions & 3 deletions packages/openlogin/src/PopupHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { getPopupFeatures } from "./utils";
export interface PopupResponse {
sessionId?: string;
sessionNamespace?: string;
state?: string;
error?: string;
}
class PopupHandler extends EventEmitter {
url: string;
Expand Down Expand Up @@ -71,16 +73,16 @@ class PopupHandler extends EventEmitter {
}

async listenOnChannel(loginId: string): Promise<PopupResponse> {
return new Promise<PopupResponse>((resolve, reject) => {
const bc = new BroadcastChannel<{ error?: string; data?: PopupResponse }>(loginId, {
return new Promise<PopupResponse>((resolve, _reject) => {
const bc = new BroadcastChannel<{ error?: string; data?: PopupResponse; state?: string }>(loginId, {
webWorkerSupport: false,
type: "server",
});
bc.addEventListener("message", (ev) => {
this.close();
bc.close();
if (ev.error) {
reject(new Error(ev.error));
resolve({ error: ev.error, state: ev.state });
} else {
resolve(ev.data);
}
Expand Down
1 change: 1 addition & 0 deletions packages/openlogin/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type HashQueryParamResult = {
sessionId?: string;
sessionNamespace?: string;
error?: string;
state?: string;
};

export function getHashQueryParams(replaceUrl = false): HashQueryParamResult {
Expand Down

0 comments on commit b583bf6

Please sign in to comment.