Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session ID handling & Reconnection #190

Merged
merged 4 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/iframeWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ export default class IframeWrapper {
)
}

public getSessionID = () => {
const val = window.localStorage.getItem(
`arcana-auth-${this.params.iframeUrl}-sessionID`
)
if (val) {
return JSON.parse(val)
}
return null
}

public setSessionID = (id: string, expiry: number) => {
window.localStorage.setItem(
`arcana-auth-${this.params.iframeUrl}-sessionID`,
JSON.stringify({ id, expiry })
)
}

public setWalletType(appMode: AppMode | undefined) {
this.appMode = appMode ?? AppMode.Full
this.initWalletUI()
Expand Down
38 changes: 37 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
AppConfig,
AppMode,
BearerAuthentication,
ChainConfigInput,
ConstructorParams,
EthereumProvider,
FirebaseBearer,
Expand All @@ -24,7 +25,6 @@
Theme,
ThemeConfig,
UserInfo,
ChainConfigInput,
} from './typings'
import { getAppInfo, getImageUrls } from './appInfo'
import { ArcanaAuthError, ErrorNotInitialized } from './errors'
Expand All @@ -45,7 +45,7 @@
private connectCtrl: ModalController
private _standaloneMode: {
mode: 1 | 2
handler: (...args: any) => void | undefined

Check warning on line 48 in src/index.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/index.ts#L48

Unexpected any. Specify a different type (@typescript-eslint/no-explicit-any)
}
constructor(clientId: string, p?: Partial<ConstructorParams>) {
let network = p?.network
Expand Down Expand Up @@ -290,6 +290,42 @@
throw ErrorNotInitialized
}

/**
* A function to to be called before trying to .reconnect()
*/
public canReconnect() {
const session = this.iframeWrapper.getSessionID()
if (!session) {
return false
}

if (session.exp < Date.now()) {
return false
}

return true
}

/**
* A function to try to reconnect to last login session.
* Should be called on event of click function as it opens a popup.
*/
public async reconnect() {
const session = this.iframeWrapper.getSessionID()
if (session) {
if (session.exp < Date.now()) {
throw new Error('cannot reconnect, session expired')
}
const u = new URL(await this._provider.getReconnectionUrl())
u.searchParams.set('sessionID', session.id)

const popup = new Popup(u.toString())
await popup.open()
return
}
throw new Error('cannot reconnect, no session found')
}

/* Private functions */
/**
* @internal
Expand Down Expand Up @@ -401,7 +437,7 @@

private standaloneMode(
mode: 1 | 2,
handler: (eventName: string, data: any) => void

Check warning on line 440 in src/index.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/index.ts#L440

Unexpected any. Specify a different type (@typescript-eslint/no-explicit-any)
) {
this._standaloneMode = {
mode,
Expand Down
8 changes: 8 additions & 0 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export class ArcanaProvider
triggerPasswordlessLogin: loginFuncs.loginWithLink,
getPopupState: () => this.iframe.getState(),
setIframeStyle: this.iframe.setIframeStyle,
getSessionID: this.iframe.getSessionID,
setSessionID: this.iframe.setSessionID,
getSDKVersion: () => 'v3',
})
this.communication = communication
Expand Down Expand Up @@ -127,6 +129,12 @@ export class ArcanaProvider
return info
}

public async getReconnectionUrl() {
const c = await this.getCommunication('getReconnectionUrl')
const url = await c.getReconnectionUrl()
return url
}

public async getPublicKey(email: string, verifier: string) {
const c = await this.getCommunication('getPublicKey')
const pk = await c.getPublicKey(email, verifier)
Expand Down
3 changes: 3 additions & 0 deletions src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
setToken: string
}
expandWallet: () => Promise<void>
getReconnectionUrl: () => Promise<string>
}

export interface ParentMethods {
Expand All @@ -138,6 +139,8 @@
setIframeStyle: (styles: CSSStyleDeclaration) => void
getWalletPosition: () => Position
getSDKVersion: () => SDKVersion
getSessionID: () => string | null
setSessionID: (id: string, exp: number) => void
}

export interface TypedDataMessage {
Expand Down Expand Up @@ -204,9 +207,9 @@

export interface EthereumProvider {
request(args: RequestArguments): Promise<unknown>
on(eventName: string | symbol, listener: (...args: any[]) => void): this

Check warning on line 210 in src/typings.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/typings.ts#L210

Unexpected any. Specify a different type (@typescript-eslint/no-explicit-any)
removeListener(
eventName: string | symbol,
listener: (...args: any[]) => void

Check warning on line 213 in src/typings.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/typings.ts#L213

Unexpected any. Specify a different type (@typescript-eslint/no-explicit-any)
): this
}
Loading