Skip to content

Commit

Permalink
🐭 Add better siwe account holders and useCallbacks for functions (#813)
Browse files Browse the repository at this point in the history
* Add useCallbacks and better session holder

* Create siwe-session-improvements.md

Co-authored-by: mateusz <[email protected]>
  • Loading branch information
mj426382 and mateusz authored Jun 7, 2022
1 parent edfd60b commit 1c987ba
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/siwe-session-improvements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@usedapp/siwe": patch
---

🐭 Add better siwe account holders and useCallbacks for functions
28 changes: 17 additions & 11 deletions packages/siwe/src/provider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEthers } from '@usedapp/core'
import React, { useEffect, ReactNode, useState } from 'react'
import React, { useEffect, ReactNode, useState, useCallback } from 'react'
import { createContext, useContext } from 'react'
import { SiweMessage } from 'siwe'
import { SiweFetchers, getFetchers } from './requests'
Expand Down Expand Up @@ -40,14 +40,17 @@ export const SiweProvider = ({ children, backendUrl, api }: SiweProviderProps) =
const [authToken, setAuthToken] = useState<string | undefined | null>(undefined)

useEffect(() => {
setAuthToken(localStorage.getItem('authToken'))
if (!account || !chainId) {
return
}
setAuthToken(localStorage.getItem('authToken' + account + chainId))
if (authToken === null) {
return
}
void getAuth().then((res) => (res.loggedIn ? setLoggedIn(true) : undefined))
}, [authToken, getAuth])
void getAuth(account, chainId).then((res) => (res.loggedIn ? setLoggedIn(true) : undefined))
}, [authToken, getAuth, account, chainId])

const signIn = async (signInOptions?: SignInOptions) => {
const signIn = useCallback(async(signInOptions?: SignInOptions) => {
if (!account || !chainId || !library) {
return
}
Expand All @@ -71,17 +74,20 @@ export const SiweProvider = ({ children, backendUrl, api }: SiweProviderProps) =

const session = JSON.stringify({ signature, message })

localStorage.setItem('authToken', session)
localStorage.setItem('authToken' + account + chainId, session)
setAuthToken(session)

void getAuth().then((res) => (res.loggedIn ? setLoggedIn(true) : undefined))
}
void getAuth(account, chainId).then((res) => (res.loggedIn ? setLoggedIn(true) : undefined))
}, [account, chainId, library, getAuth, setAuthToken, getNonce])

const signOut = async () => {
localStorage.removeItem('authToken')
const signOut = useCallback(async () => {
if (!account || !chainId) {
return
}
localStorage.removeItem('authToken' + account + chainId)
setLoggedIn(false)
setAuthToken(undefined)
}
}, [setLoggedIn, setAuthToken, account, chainId])

const value = {
signIn,
Expand Down
6 changes: 3 additions & 3 deletions packages/siwe/src/requests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface AuthResponse {

export interface SiweFetchers {
getNonce: () => Promise<NonceResponse>
getAuth: () => Promise<AuthResponse>
getAuth: (account: string, chainId: number) => Promise<AuthResponse>
}

const failedAuthResponse = {
Expand All @@ -27,8 +27,8 @@ const failedNonceResponse = {

export const getFetchers = (backendUrl: string): SiweFetchers => {
return {
getAuth: async () => {
const token = localStorage.getItem('authToken')
getAuth: async (account: string, chainId: number) => {
const token = localStorage.getItem('authToken' + account + chainId)

if (token === undefined || token === null) {
return failedAuthResponse
Expand Down

3 comments on commit 1c987ba

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.