Skip to content

Commit a662f07

Browse files
committed
fix(logout problem): fix the logout fail problem and remove the cache of SDK
1 parent 633f860 commit a662f07

File tree

3 files changed

+53
-67
lines changed

3 files changed

+53
-67
lines changed

example/basic-dapp/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ async function walletInitialized() {
556556
method: 'cfx_accounts',
557557
params: [
558558
{
559-
availableNetwork: [1, 1029],
559+
availableNetwork: [1029],
560560
scopes: ['baseInfo', 'identity'],
561561
},
562562
],
@@ -588,7 +588,7 @@ async function walletInitialized() {
588588
method: 'cfx_accounts',
589589
params: [
590590
{
591-
availableNetwork: [1, 1029],
591+
availableNetwork: [1],
592592
scopes: ['baseInfo', 'identity'],
593593
},
594594
],

src/provider.ts

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import {
1515
import {
1616
callIframe,
1717
isIncluded,
18-
readCache,
19-
removeCache,
20-
setCache,
18+
readStorage,
19+
removeStorage,
20+
writeStorage,
2121
} from './utils/common'
2222
import config from '../package.json'
2323
import { AddressType, getAddressType } from './utils/address'
@@ -57,8 +57,12 @@ export class Provider implements IProvider {
5757
}
5858
this.logger = logger
5959
this.appId = appId
60-
readCache(this)
61-
60+
const state: {
61+
reAuth: boolean
62+
} = readStorage('state')
63+
if (state.reAuth) {
64+
this.reAuth = true
65+
}
6266
// bind functions (to prevent consumers from making unbound calls)
6367
this.request = this.request.bind(this)
6468
this.call = this.call.bind(this)
@@ -75,6 +79,24 @@ export class Provider implements IProvider {
7579
}
7680
}
7781

82+
setState(data: IAuthResult) {
83+
this.address = data.address
84+
this.networkId = data.networkId
85+
this.chainId = data.chainId
86+
this.url = data.url
87+
this.oauthToken = data.oauthToken
88+
this.scopes = data.scopes
89+
}
90+
91+
reset() {
92+
this.address = []
93+
this.networkId = -1
94+
this.chainId = -1
95+
this.url = ''
96+
this.oauthToken = undefined
97+
this.scopes = []
98+
}
99+
78100
/**
79101
* Deprecated: use `request` instead
80102
* @param arg
@@ -162,20 +184,18 @@ export class Provider implements IProvider {
162184
const scopes: string[] =
163185
(params && 'scopes' in paramsObj ? paramsObj['scopes'] : []) || []
164186
console.log('paramsObj', paramsObj)
165-
console.log('scopes', scopes, this.scopes)
166187
if (this.address.length > 0) {
167188
if (isIncluded(this.scopes, scopes)) {
168189
if (scopes.length === 0) {
169190
return this.address
170191
} else {
171192
return {
172193
address: this.address,
173-
code: this.oauthToken,
174194
scopes: scopes,
175195
}
176196
}
177197
} else {
178-
removeCache(this)
198+
this.reset()
179199
}
180200
}
181201
const result = (await callIframe(
@@ -192,7 +212,8 @@ export class Provider implements IProvider {
192212
)) as IAuthResult
193213
result.scopes = scopes
194214
this.reAuth = false
195-
setCache(result, this)
215+
removeStorage('state')
216+
this.setState(result)
196217
this.events.onAccountsChanged &&
197218
this.events.onAccountsChanged(result.address)
198219
this.events.onChainChanged &&
@@ -266,15 +287,11 @@ export class Provider implements IProvider {
266287
)
267288
case 'anyweb_logout':
268289
try {
269-
removeCache(this)
290+
this.reset()
291+
writeStorage('state', {
292+
reAuth: true,
293+
})
270294
this.reAuth = true
271-
// sendMessageToApp({
272-
// type: 'event',
273-
// data: {
274-
// type: 'logout',
275-
// appId: this.appId,
276-
// },
277-
// })
278295
} catch (e) {
279296
return e
280297
}

src/utils/common.ts

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import * as forge from 'node-forge'
66
import { BASE_URL } from '../config'
77
import {
8-
IAuthResult,
98
IIframeData,
109
IIframeEventData,
1110
IIframeOptions,
@@ -318,60 +317,30 @@ export const callIframe = async (
318317
}
319318
}
320319

321-
export const readCache = (provider: Provider) => {
322-
try {
323-
const result = JSON.parse(
324-
(window.localStorage && window.localStorage.getItem('anyweb_info')) ||
325-
'{}'
326-
)
327-
if (
328-
Object.keys(result).length > 0 &&
329-
Object.keys(result).includes('address') &&
330-
Object.keys(result).includes('networkId') &&
331-
Object.keys(result).includes('chainId') &&
332-
Object.keys(result).includes('expires') &&
333-
Object.keys(result).includes('oauthToken') &&
334-
Object.keys(result).includes('scopes') &&
335-
result.expires > new Date().getTime()
336-
) {
337-
provider.address = result.address
338-
provider.networkId = result.networkId
339-
provider.chainId = result.chainId
340-
provider.url = result.url
341-
provider.oauthToken = result.oauthToken
342-
provider.scopes = result.scopes
343-
}
344-
} catch (e) {
345-
provider.logger.error(e)
346-
}
347-
}
348-
349-
export const setCache = (data: IAuthResult, provider: Provider) => {
320+
export const writeStorage = (
321+
key: string,
322+
content: Record<string, unknown>,
323+
expiresTime: number = 5 * 60 * 1000
324+
) => {
350325
window.localStorage &&
351326
window.localStorage.setItem(
352-
'anyweb_info',
327+
`anyweb_${key}`,
353328
JSON.stringify({
354-
...data,
355-
expires: 60 * 1000 + new Date().getTime(),
329+
...content,
330+
expires: expiresTime + new Date().getTime(),
356331
})
357332
)
358-
provider.address = data.address || provider.address
359-
provider.networkId = data.networkId || provider.networkId
360-
provider.chainId = data.chainId || provider.chainId
361-
provider.url = data.url
362-
provider.oauthToken = data.oauthToken || provider.oauthToken
363-
provider.scopes = data.scopes || provider.scopes
364-
return data
365333
}
366334

367-
export const removeCache = (provider: Provider) => {
368-
window.localStorage && window.localStorage.removeItem('anyweb_info')
369-
provider.address = []
370-
provider.networkId = -1
371-
provider.chainId = -1
372-
provider.url = ''
373-
provider.oauthToken = undefined
374-
provider.scopes = []
335+
export const readStorage = (key: string) => {
336+
return JSON.parse(
337+
(window.localStorage && window.localStorage.getItem(`anyweb_${key}`)) ||
338+
'{}'
339+
)
340+
}
341+
342+
export const removeStorage = (key: string) => {
343+
window.localStorage && window.localStorage.removeItem(`anyweb_${key}`)
375344
}
376345

377346
export const isArrEqual = <T>(arr1: T[], arr2: T[]) => {

0 commit comments

Comments
 (0)