Skip to content

Commit

Permalink
fix(logout problem): fix the logout fail problem and remove the cache…
Browse files Browse the repository at this point in the history
… of SDK
  • Loading branch information
Littleor committed Apr 2, 2022
1 parent 633f860 commit a662f07
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 67 deletions.
4 changes: 2 additions & 2 deletions example/basic-dapp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ async function walletInitialized() {
method: 'cfx_accounts',
params: [
{
availableNetwork: [1, 1029],
availableNetwork: [1029],
scopes: ['baseInfo', 'identity'],
},
],
Expand Down Expand Up @@ -588,7 +588,7 @@ async function walletInitialized() {
method: 'cfx_accounts',
params: [
{
availableNetwork: [1, 1029],
availableNetwork: [1],
scopes: ['baseInfo', 'identity'],
},
],
Expand Down
51 changes: 34 additions & 17 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import {
import {
callIframe,
isIncluded,
readCache,
removeCache,
setCache,
readStorage,
removeStorage,
writeStorage,
} from './utils/common'
import config from '../package.json'
import { AddressType, getAddressType } from './utils/address'
Expand Down Expand Up @@ -57,8 +57,12 @@ export class Provider implements IProvider {
}
this.logger = logger
this.appId = appId
readCache(this)

const state: {
reAuth: boolean
} = readStorage('state')
if (state.reAuth) {
this.reAuth = true
}
// bind functions (to prevent consumers from making unbound calls)
this.request = this.request.bind(this)
this.call = this.call.bind(this)
Expand All @@ -75,6 +79,24 @@ export class Provider implements IProvider {
}
}

setState(data: IAuthResult) {
this.address = data.address
this.networkId = data.networkId
this.chainId = data.chainId
this.url = data.url
this.oauthToken = data.oauthToken
this.scopes = data.scopes
}

reset() {
this.address = []
this.networkId = -1
this.chainId = -1
this.url = ''
this.oauthToken = undefined
this.scopes = []
}

/**
* Deprecated: use `request` instead
* @param arg
Expand Down Expand Up @@ -162,20 +184,18 @@ export class Provider implements IProvider {
const scopes: string[] =
(params && 'scopes' in paramsObj ? paramsObj['scopes'] : []) || []
console.log('paramsObj', paramsObj)
console.log('scopes', scopes, this.scopes)
if (this.address.length > 0) {
if (isIncluded(this.scopes, scopes)) {
if (scopes.length === 0) {
return this.address
} else {
return {
address: this.address,
code: this.oauthToken,
scopes: scopes,
}
}
} else {
removeCache(this)
this.reset()
}
}
const result = (await callIframe(
Expand All @@ -192,7 +212,8 @@ export class Provider implements IProvider {
)) as IAuthResult
result.scopes = scopes
this.reAuth = false
setCache(result, this)
removeStorage('state')
this.setState(result)
this.events.onAccountsChanged &&
this.events.onAccountsChanged(result.address)
this.events.onChainChanged &&
Expand Down Expand Up @@ -266,15 +287,11 @@ export class Provider implements IProvider {
)
case 'anyweb_logout':
try {
removeCache(this)
this.reset()
writeStorage('state', {
reAuth: true,
})
this.reAuth = true
// sendMessageToApp({
// type: 'event',
// data: {
// type: 'logout',
// appId: this.appId,
// },
// })
} catch (e) {
return e
}
Expand Down
65 changes: 17 additions & 48 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import * as forge from 'node-forge'
import { BASE_URL } from '../config'
import {
IAuthResult,
IIframeData,
IIframeEventData,
IIframeOptions,
Expand Down Expand Up @@ -318,60 +317,30 @@ export const callIframe = async (
}
}

export const readCache = (provider: Provider) => {
try {
const result = JSON.parse(
(window.localStorage && window.localStorage.getItem('anyweb_info')) ||
'{}'
)
if (
Object.keys(result).length > 0 &&
Object.keys(result).includes('address') &&
Object.keys(result).includes('networkId') &&
Object.keys(result).includes('chainId') &&
Object.keys(result).includes('expires') &&
Object.keys(result).includes('oauthToken') &&
Object.keys(result).includes('scopes') &&
result.expires > new Date().getTime()
) {
provider.address = result.address
provider.networkId = result.networkId
provider.chainId = result.chainId
provider.url = result.url
provider.oauthToken = result.oauthToken
provider.scopes = result.scopes
}
} catch (e) {
provider.logger.error(e)
}
}

export const setCache = (data: IAuthResult, provider: Provider) => {
export const writeStorage = (
key: string,
content: Record<string, unknown>,
expiresTime: number = 5 * 60 * 1000
) => {
window.localStorage &&
window.localStorage.setItem(
'anyweb_info',
`anyweb_${key}`,
JSON.stringify({
...data,
expires: 60 * 1000 + new Date().getTime(),
...content,
expires: expiresTime + new Date().getTime(),
})
)
provider.address = data.address || provider.address
provider.networkId = data.networkId || provider.networkId
provider.chainId = data.chainId || provider.chainId
provider.url = data.url
provider.oauthToken = data.oauthToken || provider.oauthToken
provider.scopes = data.scopes || provider.scopes
return data
}

export const removeCache = (provider: Provider) => {
window.localStorage && window.localStorage.removeItem('anyweb_info')
provider.address = []
provider.networkId = -1
provider.chainId = -1
provider.url = ''
provider.oauthToken = undefined
provider.scopes = []
export const readStorage = (key: string) => {
return JSON.parse(
(window.localStorage && window.localStorage.getItem(`anyweb_${key}`)) ||
'{}'
)
}

export const removeStorage = (key: string) => {
window.localStorage && window.localStorage.removeItem(`anyweb_${key}`)
}

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

0 comments on commit a662f07

Please sign in to comment.