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

open sign modal only when auto-login fails #585

Merged
merged 1 commit into from
Mar 12, 2024
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
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
Loading