Skip to content

Commit fce13a5

Browse files
committed
chore: add error handling to auth
1 parent 2e7a3e5 commit fce13a5

File tree

1 file changed

+18
-1
lines changed
  • examples/getting-started/src/components

1 file changed

+18
-1
lines changed

examples/getting-started/src/components/Auth.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1+
import { DeviceFlowTokenExpiredError } from "@navigraph/app"
12
import { DeviceFlowParams } from "@navigraph/auth"
23
import { useCallback, useState } from "react"
34
import { useNavigraphAuth } from "../hooks/useNavigraphAuth"
45

56
export default function Auth() {
7+
const [error, setError] = useState<Error | null>(null)
68
const [params, setParams] = useState<DeviceFlowParams | null>(null)
79
const { user, isInitialized, signIn, signOut } = useNavigraphAuth()
810

9-
const handleSignIn = useCallback(() => signIn(params => setParams(params)).finally(() => setParams(null)), [signIn])
11+
const handleSignIn = useCallback(
12+
() =>
13+
signIn(params => {
14+
setParams(params)
15+
setError(null)
16+
})
17+
.catch(err => setError(err))
18+
.finally(() => setParams(null)),
19+
[signIn],
20+
)
1021

1122
const isLoginInProgress = !!params
1223

@@ -22,6 +33,12 @@ export default function Auth() {
2233
</button>
2334
)}
2435

36+
{error && (
37+
<div className="text-red-500">
38+
{error instanceof DeviceFlowTokenExpiredError ? "Session expired, try again!" : error.message}
39+
</div>
40+
)}
41+
2542
{params?.verification_uri_complete && !user && (
2643
<div className="flex flex-col items-center gap-2">
2744
<a

0 commit comments

Comments
 (0)