Skip to content

Commit dd371ce

Browse files
author
Musab Hussain
committed
event emitter changed to promise
1 parent 96fc0df commit dd371ce

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

example/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default function App() {
2929
clientId: "BDj1toq1N1xYgDIJ00ADR-QPJ71ESzJcB3ijVHP1TsIX7nsx_lu6uLoJQMPze1vpGDt--Ew95RGxz-RgOh1tcxM",
3030
network: OPENLOGIN_NETWORK.TESTNET,
3131
});
32-
web3auth.current.sessionResponse.on("login", function(state: State) {
32+
web3auth.current.init().then(function(state) {
3333
setKey(state.privKey || "no key");
3434
setUserInfo(state);
3535
});

example_general/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const App = () => {
3737
clientId: "BDj1toq1N1xYgDIJ00ADR-QPJ71ESzJcB3ijVHP1TsIX7nsx_lu6uLoJQMPze1vpGDt--Ew95RGxz-RgOh1tcxM",
3838
network: OPENLOGIN_NETWORK.TESTNET,
3939
});
40-
web3auth.current.sessionResponse.on("login", function(state: State) {
40+
web3auth.current.init().then(function(state) {
4141
setKey(state.privKey || "no key");
4242
setUserInfo(state);
4343
});

src/Web3Auth.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import KeyStore from "./session/KeyStore";
1818
class Web3Auth {
1919
initParams: SdkInitParams;
2020
webBrowser: IWebBrowser;
21-
sessionResponse: EventEmitter;
2221
keyStore: KeyStore;
2322

2423
constructor(webBrowser: IWebBrowser, storage: SecureStore|EncryptedStorage, initParams: SdkInitParams) {
@@ -27,10 +26,11 @@ class Web3Auth {
2726
this.initParams.sdkUrl = "https://sdk.openlogin.com";
2827
}
2928
this.webBrowser = webBrowser;
30-
this.sessionResponse = new EventEmitter();
3129
this.keyStore = new KeyStore(storage);
30+
}
3231

33-
this.authorizeSession();
32+
async init(): Promise<State> {
33+
return this.authorizeSession();
3434
}
3535

3636
async login(options: SdkLoginParams): Promise<State> {
@@ -107,7 +107,7 @@ class Web3Auth {
107107
return this.webBrowser.openAuthSessionAsync(url.href, redirectUrl);
108108
}
109109

110-
async authorizeSession() {
110+
async authorizeSession(): Promise<State> {
111111
const sessionId = await this.keyStore.get("sessionId");
112112
if (sessionId && sessionId.length > 0) {
113113
var pubKey = getPublic(Buffer.from(sessionId, "hex")).toString("hex");
@@ -128,10 +128,10 @@ class Web3Auth {
128128
if (!web3AuthResponse.error) {
129129
web3AuthResponse = web3AuthResponse as State;
130130
if (web3AuthResponse.privKey && web3AuthResponse.privKey.trim('0').length > 0) {
131-
this.sessionResponse.emit("login", web3AuthResponse);
131+
return Promise.resolve(web3AuthResponse);
132132
}
133133
} else {
134-
throw new Error(`session recovery failed with error ${web3AuthResponse.error}`);
134+
return Promise.reject(`session recovery failed with error ${web3AuthResponse.error}`);
135135
}
136136
}
137137
}
@@ -166,7 +166,7 @@ class Web3Auth {
166166
this.keyStore.remove("ephemPublicKey");
167167
this.keyStore.remove("ivKey");
168168
this.keyStore.remove("mac");
169-
169+
170170
if (this.initParams.loginConfig) {
171171
var loginConfigItem = Object.values(this.initParams.loginConfig)[0];
172172
if (loginConfigItem) {

0 commit comments

Comments
 (0)