Skip to content

Commit

Permalink
manage mfa changes
Browse files Browse the repository at this point in the history
  • Loading branch information
arch1995 committed Jan 24, 2024
1 parent 91234cb commit 21c039c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
18 changes: 11 additions & 7 deletions examples/vue-example/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
<button class="btn" @click="getEd25519Key">Get Ed25519Key</button>
</div>
<div class="flex flex-col sm:flex-row gap-4 bottom-gutter">
<button v-if="!isMFAEnabled" class="btn" @click="enableMFA">Enable MFA</button>
<button v-else class="btn" @click="manageMFA">Manage MFA</button>
<button v-if="isMFAEnabled()" class="btn" @click="manageMFA">Manage MFA</button>
<button v-else class="btn" @click="enableMFA">Enable MFA</button>
</div>
<p class="btn-label">Signing</p>
<div class="flex flex-col sm:flex-row gap-4 bottom-gutter">
Expand Down Expand Up @@ -213,8 +213,10 @@ const vueapp = defineComponent({
},
updated() {
// this is called on each state update
console.log(this.$data);
if (storageAvailable("sessionStorage")) sessionStorage.setItem("state", JSON.stringify(this.$data));
console.log("updated data", this.$data);
const data = {...this.$data};
Reflect.deleteProperty(data, "privKey");
if (storageAvailable("sessionStorage")) sessionStorage.setItem("state", JSON.stringify(data));
},
computed: {
computedLoginProviders(): LOGIN_PROVIDER_TYPE[] {
Expand Down Expand Up @@ -263,9 +265,6 @@ const vueapp = defineComponent({
showEmailFlow(): boolean {
return this.selectedLoginProvider === LOGIN_PROVIDER.EMAIL_PASSWORDLESS;
},
isMFAEnabled(): boolean {
return this.openloginInstance.state.userInfo?.isMfaEnabled || false;
},
},
methods: {
async login() {
Expand Down Expand Up @@ -422,6 +421,11 @@ const vueapp = defineComponent({
}
},
isMFAEnabled() {
if (!this.openloginInstance || !this.openloginInstance.sessionId) return false;
return this.openloginInstance.state?.userInfo?.isMfaEnabled || false;
},
async getUserInfo() {
if (!this.openloginInstance) {
throw new Error("Openlogin is not available.");
Expand Down
2 changes: 0 additions & 2 deletions packages/openlogin-utils/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,7 @@ export type LoginParams = BaseRedirectParams & {
* @defaultValue secp256k1
*/
curve?: SUPPORTED_KEY_CURVES_TYPE;
};

export type ManageMFAParams = LoginParams & {
/**
* Allows the dapp to set a custom redirect url for the manage mfa flow.
*
Expand Down
20 changes: 7 additions & 13 deletions packages/openlogin/src/OpenLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
BUILD_ENV,
jsonToBase64,
LoginParams,
ManageMFAParams,
OPENLOGIN_ACTIONS,
OPENLOGIN_NETWORK,
OpenLoginOptions,
Expand Down Expand Up @@ -292,7 +291,7 @@ class OpenLogin {
return Boolean(this.state.userInfo?.isMfaEnabled);
}

async manageMFA(params: Partial<ManageMFAParams>): Promise<void> {
async manageMFA(params: Partial<LoginParams>): Promise<void> {
if (!this.sessionId) throw LoginError.userNotLoggedIn();
if (!this.state.userInfo.isMfaEnabled) throw LoginError.mfaNotEnabled();

Expand All @@ -307,10 +306,7 @@ class OpenLogin {

const dataObject: OpenloginSessionConfig = {
actionType: OPENLOGIN_ACTIONS.MANAGE_MFA,
options: {
...this.options,
sessionNamespace: this.options.network,
},
options: this.options,
params: {
...defaultParams,
...params,
Expand All @@ -320,12 +316,13 @@ class OpenLogin {
},
appState: jsonToBase64({ loginId }),
},
sessionId: this.sessionId,
};

await this.getLoginId(loginId, dataObject, dataObject.options.sessionTime);
const configParams: BaseLoginParams = {
loginId,
sessionNamespace: this.options.network,
sessionNamespace: "",
};

const loginUrl = constructURL({
Expand Down Expand Up @@ -357,10 +354,7 @@ class OpenLogin {

const result = await this.openloginHandler(`${this.baseUrl}/start`, dataObject);
if (this.options.uxMode === UX_MODE.REDIRECT) return undefined;
this.sessionManager.sessionId = result.sessionId;
this.options.sessionNamespace = result.sessionNamespace;
this.currentStorage.set("sessionId", result.sessionId);
await this.rehydrateSession();
if (result.error) return false;
return true;
}

Expand All @@ -375,8 +369,8 @@ class OpenLogin {
if (!this.sessionManager) throw InitializationError.notInitialized();

const loginSessionMgr = new OpenloginSessionManager<OpenloginSessionConfig>({
sessionServerBaseUrl: this.options.storageServerUrl,
sessionNamespace: this.options.sessionNamespace,
sessionServerBaseUrl: data.options.storageServerUrl,
sessionNamespace: data.options.sessionNamespace,
sessionTime: timeout, // each login key must be used with 10 mins (might be used at the end of popup redirect)
sessionId: loginId,
});
Expand Down

0 comments on commit 21c039c

Please sign in to comment.