Skip to content

Commit 52b9962

Browse files
author
Frank
committed
zen: add context for login errors
1 parent a15397c commit 52b9962

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

packages/console/app/src/routes/auth/callback.ts

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,36 @@ import { useAuthSession } from "~/context/auth.session"
55

66
export async function GET(input: APIEvent) {
77
const url = new URL(input.request.url)
8-
const code = url.searchParams.get("code")
9-
if (!code) throw new Error("No code found")
10-
const result = await AuthClient.exchange(code, `${url.origin}${url.pathname}`)
11-
if (result.err) {
12-
throw new Error(result.err.message)
13-
}
14-
const decoded = AuthClient.decode(result.tokens.access, {} as any)
15-
if (decoded.err) throw new Error(decoded.err.message)
16-
const session = await useAuthSession()
17-
const id = decoded.subject.properties.accountID
18-
await session.update((value) => {
19-
return {
20-
...value,
21-
account: {
22-
...value.account,
23-
[id]: {
24-
id,
25-
email: decoded.subject.properties.email,
8+
try {
9+
const code = url.searchParams.get("code")
10+
if (!code) throw new Error("No code found")
11+
const result = await AuthClient.exchange(code, `${url.origin}${url.pathname}`)
12+
if (result.err) throw new Error(result.err.message)
13+
const decoded = AuthClient.decode(result.tokens.access, {} as any)
14+
if (decoded.err) throw new Error(decoded.err.message)
15+
const session = await useAuthSession()
16+
const id = decoded.subject.properties.accountID
17+
await session.update((value) => {
18+
return {
19+
...value,
20+
account: {
21+
...value.account,
22+
[id]: {
23+
id,
24+
email: decoded.subject.properties.email,
25+
},
2626
},
27-
},
28-
current: id,
29-
}
30-
})
31-
return redirect("/auth")
27+
current: id,
28+
}
29+
})
30+
return redirect("/auth")
31+
} catch (e: any) {
32+
return new Response(
33+
JSON.stringify({
34+
error: e.message,
35+
cause: Object.fromEntries(url.searchParams.entries()),
36+
}),
37+
{ status: 500 },
38+
)
39+
}
3240
}

packages/console/function/src/auth.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,11 @@ export default {
123123
},
124124
}).then((x) => x.json())) as any
125125
subject = user.id.toString()
126-
email = emails.find((x: any) => x.primary && x.verified)?.email
126+
127+
const primaryEmail = emails.find((x: any) => x.primary)
128+
if (!primaryEmail) throw new Error("No primary email found for GitHub user")
129+
if (!primaryEmail.verified) throw new Error("Primary email for GitHub user not verified")
130+
email = primaryEmail.email
127131
} else if (response.provider === "google") {
128132
if (!response.id.email_verified) throw new Error("Google email not verified")
129133
subject = response.id.sub as string

0 commit comments

Comments
 (0)