Skip to content

Commit

Permalink
Merge pull request #585 from liteflow-labs/fix/trigger-sign-modal
Browse files Browse the repository at this point in the history
open sign modal only when auto-login fails
  • Loading branch information
ismailToyran authored Mar 12, 2024
2 parents 6a22ef2 + f37d5ab commit 5d0873f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
11 changes: 8 additions & 3 deletions hooks/useAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type AccountDetail = {
address?: string
jwtToken: string | null
logout: () => Promise<void>
login: (connector: Connector) => Promise<void>
login: (connector: Connector) => Promise<boolean>
sign: (connector: Connector) => Promise<void>
}

Expand Down Expand Up @@ -64,8 +64,12 @@ export default function useAccount(): AccountDetail {
const signer = walletClientToSigner(wallet)
const currentAddress = (await signer.getAddress()).toLowerCase()
if (jwt && currentAddress === jwt.address) {
return setAuthenticationToken(jwt.token)
setAuthenticationToken(jwt.token)
// fully login, nothing to do
return true
}
// not fully login, need to sign
return false
},
[jwt, setAuthenticationToken],
)
Expand All @@ -76,6 +80,7 @@ export default function useAccount(): AccountDetail {
const signer = walletClientToSigner(wallet)
const { jwtToken } = await authenticate(signer)

setAuthenticationToken(jwtToken)
const newJwt = jwtDecode<JwtPayload>(jwtToken)
setCookie(COOKIE_JWT_TOKEN, jwtToken, {
...COOKIE_OPTIONS,
Expand All @@ -87,7 +92,7 @@ export default function useAccount(): AccountDetail {
: {}),
})
},
[authenticate, setCookie],
[authenticate, setAuthenticationToken, setCookie],
)

// Server side
Expand Down
41 changes: 25 additions & 16 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import React, {
Fragment,
JSX,
PropsWithChildren,
useCallback,
useEffect,
useMemo,
useState,
} from 'react'
import { Cookies, CookiesProvider, useCookies } from 'react-cookie'
import {
Connector,
WagmiConfig,
useDisconnect,
useAccount as useWagmiAccount,
Expand Down Expand Up @@ -128,36 +130,43 @@ function AccountProvider({
const { LITEFLOW_API_KEY, BASE_URL } = useEnvironment()
const { jwtToken, login, logout } = useAccount()
const { disconnect } = useDisconnect()
const { isOpen, onOpen, onClose } = useDisclosure()
const { isOpen, onOpen: openSignModal, onClose } = useDisclosure()
const [cookies] = useCookies<string, COOKIES>([COOKIE_JWT_TOKEN])

const { connector } = useWagmiAccount({
async onConnect({ connector }) {
if (!connector) return
try {
await login(connector)
// jwtToken from useAccount is null on refresh, so we need to check the cookies
if (!cookies[COOKIE_JWT_TOKEN]) onOpen()
} catch (e: any) {
disconnect()
}
},
onDisconnect() {
onConnect: useCallback(
async ({ connector }: { connector?: Connector }) => {
if (!connector) return
try {
const isConnected = await login(connector)
if (!isConnected) openSignModal()
} catch (e: any) {
disconnect()
}
},
[disconnect, login, openSignModal],
),
onDisconnect: useCallback(() => {
void logout()
},
}, [logout]),
})

// handle change of account
useEffect(() => {
if (!connector) return
const handleLogin = () => {
login(connector).then(onOpen).catch(onError)
const handleLogin = async () => {
try {
const isConnected = await login(connector)
if (!isConnected) openSignModal()
} catch (error) {
onError(1)
}
}
connector.on('change', handleLogin)
return () => {
connector.off('change', handleLogin)
}
}, [connector, login, onError, onOpen])
}, [connector, cookies, login, onError, openSignModal])

const client = useMemo(
// The client needs to be reset when the jwtToken changes but only on the client as the server will
Expand Down

0 comments on commit 5d0873f

Please sign in to comment.