Skip to content

Commit 15c9486

Browse files
committed
fixes in the logout api
1 parent 48eec9c commit 15c9486

File tree

8 files changed

+66
-38
lines changed

8 files changed

+66
-38
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3auth/react-native-sdk",
3-
"version": "3.8.0",
3+
"version": "3.5.0",
44
"description": "Web3Auth SDK that supports Bare (Standalone) and Expo React Native apps.",
55
"main": "dist/reactNativeSdk.cjs.js",
66
"module": "dist/reactNativeSdk.esm.js",

rn-bare-example/App.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,30 @@ export default function App() {
5353
}
5454
};
5555

56+
const logout = async () => {
57+
if (!web3auth) {
58+
setConsole('Web3auth not initialized');
59+
return;
60+
}
61+
62+
setConsole('Logging out');
63+
const result = await web3auth.logout({
64+
redirectUrl: resolvedRedirectUrl,
65+
});
66+
67+
if (result) {
68+
setUserInfo(undefined);
69+
setKey('');
70+
uiConsole('Logged out');
71+
}
72+
};
73+
5674
useEffect(() => {
5775
const init = async () => {
5876
const auth = new Web3Auth(WebBrowser, EncryptedStorage, {
5977
clientId,
6078
network: OPENLOGIN_NETWORK.TESTNET, // or other networks
61-
useCoreKitKey: true,
79+
useCoreKitKey: false,
6280
loginConfig: {
6381
google: {
6482
verifier: 'w3a-agg-example',
@@ -76,6 +94,7 @@ export default function App() {
7694
uiConsole('Re logged in');
7795
setUserInfo(auth.userInfo);
7896
setKey(auth.privKey);
97+
window.console.log(auth.privKey);
7998
}
8099
};
81100
init();
@@ -121,7 +140,7 @@ export default function App() {
121140
<Button title="Send Transaction" onPress={() => sendTransaction()} />
122141
<Button title="Sign Message" onPress={() => signMessage()} />
123142
<Button title="Get Private Key" onPress={() => uiConsole(key)} />
124-
<Button title="Log Out" onPress={() => setKey('')} />
143+
<Button title="Log Out" onPress={logout} />
125144
</View>
126145
);
127146

rn-bare-example/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import {AppRegistry} from 'react-native';
66
import './globals';
7+
import '@ethersproject/shims';
78
import App from './App';
89
import {name as appName} from './app.json';
910

rn-bare-example/package-lock.json

Lines changed: 27 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rn-bare-example/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
"lint": "eslint ."
1111
},
1212
"dependencies": {
13+
"@ethersproject/shims": "^5.7.0",
1314
"@toruslabs/react-native-web-browser": "^1.1.0",
14-
"@web3auth/react-native-sdk": "../web3auth-react-native-sdk-3.8.0.tgz",
15+
"@web3auth/react-native-sdk": "../web3auth-react-native-sdk-3.5.0.tgz",
1516
"ethers": "^5.7.1",
1617
"react": "18.1.0",
1718
"react-native": "0.70.1",

src/Web3Auth.ts

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import base64url from "base64url";
44
import log from "loglevel";
55
import { URL } from "react-native-url-polyfill";
66

7-
import { ShareMetadata } from "./api/model";
87
import { Web3AuthApi } from "./api/Web3AuthApi";
98
import KeyStore from "./session/KeyStore";
109
import { EncryptedStorage } from "./types/IEncryptedStorage";
@@ -124,7 +123,7 @@ class Web3Auth implements IWeb3Auth {
124123
}
125124

126125
async logout(options: SdkLogoutParams): Promise<boolean> {
127-
this.sessionTimeout();
126+
await this.sessionTimeout();
128127
const result = await this.request("logout", options.redirectUrl, options);
129128
if (result.type !== "success" || !result.url) {
130129
log.error(`[Web3Auth] logout flow failed with error type ${result.type}`);
@@ -175,37 +174,25 @@ class Web3Auth implements IWeb3Auth {
175174
private async sessionTimeout() {
176175
const sessionId = await this.keyStore.get("sessionId");
177176
if (sessionId && sessionId.length > 0) {
178-
const pubKey = getPublic(Buffer.from(sessionId, "hex")).toString("hex");
179-
const response = await Web3AuthApi.authorizeSession(pubKey);
180-
if (!response.success) {
181-
return;
182-
}
183-
const shareMetadata = JSON.parse(response.message) as ShareMetadata;
184-
const encryptedData = await encryptData(sessionId, "");
185-
const encryptedMetadata: ShareMetadata = {
186-
...shareMetadata,
187-
ciphertext: encryptedData,
188-
};
189-
const jsonData = JSON.stringify(encryptedMetadata);
190-
const hashData = keccak256(jsonData);
177+
const privKey = Buffer.from(sessionId.padStart(64, "0"), "hex");
178+
const pubKey = getPublic(privKey).toString("hex");
179+
const encData = await encryptData(sessionId.padStart(64, "0"), {});
191180
try {
192181
await Web3AuthApi.logout({
193-
key: getPublic(Buffer.from(sessionId, "hex")).toString("hex"),
194-
data: jsonData,
195-
signature: (await sign(Buffer.from(sessionId, "hex"), hashData)).toString("hex"),
182+
key: pubKey,
183+
data: encData,
184+
signature: (await sign(privKey, keccak256(encData))).toString("hex"),
196185
timeout: 1,
197186
});
198187

199-
this.keyStore.remove("sessionId");
188+
await this.keyStore.remove("sessionId");
200189

201-
if (this.initParams.loginConfig) {
202-
const loginConfigItem = Object.values(this.initParams.loginConfig)[0];
203-
if (loginConfigItem) {
204-
this.keyStore.remove(loginConfigItem.verifier);
205-
}
190+
if (this.userInfo?.verifier && this.userInfo?.dappShare.length > 0) {
191+
await this.keyStore.remove(this.userInfo.verifier);
206192
}
207-
} catch (ex) {
193+
} catch (ex: unknown) {
208194
log.error(ex);
195+
throw new Error(`Logout failed: ${(ex as Error).message}`);
209196
}
210197
}
211198
}

src/api/model.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export interface StoreApiResponse {
22
message?: string;
3-
success: boolean;
43
}
54

65
export interface LogoutApiRequest {

0 commit comments

Comments
 (0)