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

[Fix] Add reconnect functions #192

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 7 additions & 16 deletions src/iframeWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,31 +61,22 @@ export default class IframeWrapper {
}

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

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

public notifyNoStorage = ({
reconnectionURL,
}: {
reconnectionURL: string
}) => {
const sid = this.getSessionID()
if (sid != null) {
const u = new URL(reconnectionURL)
u.searchParams.set('sessionID', sid)
window.open(u.href)
}
}

public setWalletType(appMode: AppMode | undefined) {
this.appMode = appMode ?? AppMode.Full
this.initWalletUI()
Expand Down
38 changes: 38 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,44 @@ class AuthProvider {
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(
`/v1/reconnect/${this.appId}`,
mmjee marked this conversation as resolved.
Show resolved Hide resolved
this.networkConfig.authUrl
)
u.searchParams.set('sessionID', session.id)
const popup = new Popup(u.toString())
await popup.open()
return
}
throw new Error('cannot reconnect, no sessionID found')
}

/* Private functions */
/**
* @internal
Expand Down
1 change: 0 additions & 1 deletion src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ export class ArcanaProvider
setIframeStyle: this.iframe.setIframeStyle,
getSessionID: this.iframe.getSessionID,
setSessionID: this.iframe.setSessionID,
notifyNoStorage: this.iframe.notifyNoStorage,
getSDKVersion: () => 'v3',
})
this.communication = communication
Expand Down
3 changes: 1 addition & 2 deletions src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ export interface ParentMethods {
getWalletPosition: () => Position
getSDKVersion: () => SDKVersion
getSessionID: () => string | null
setSessionID: (id: string) => void
notifyNoStorage: (arg: { reconnectionURL: string }) => void
setSessionID: (id: string, exp: number) => void
}

export interface TypedDataMessage {
Expand Down