Explorar o código

zen: add context for login errors

Frank hai 3 meses
pai
achega
52b99622ad

+ 31 - 23
packages/console/app/src/routes/auth/callback.ts

@@ -5,28 +5,36 @@ import { useAuthSession } from "~/context/auth.session"
 
 
 export async function GET(input: APIEvent) {
 export async function GET(input: APIEvent) {
   const url = new URL(input.request.url)
   const url = new URL(input.request.url)
-  const code = url.searchParams.get("code")
-  if (!code) throw new Error("No code found")
-  const result = await AuthClient.exchange(code, `${url.origin}${url.pathname}`)
-  if (result.err) {
-    throw new Error(result.err.message)
-  }
-  const decoded = AuthClient.decode(result.tokens.access, {} as any)
-  if (decoded.err) throw new Error(decoded.err.message)
-  const session = await useAuthSession()
-  const id = decoded.subject.properties.accountID
-  await session.update((value) => {
-    return {
-      ...value,
-      account: {
-        ...value.account,
-        [id]: {
-          id,
-          email: decoded.subject.properties.email,
+  try {
+    const code = url.searchParams.get("code")
+    if (!code) throw new Error("No code found")
+    const result = await AuthClient.exchange(code, `${url.origin}${url.pathname}`)
+    if (result.err) throw new Error(result.err.message)
+    const decoded = AuthClient.decode(result.tokens.access, {} as any)
+    if (decoded.err) throw new Error(decoded.err.message)
+    const session = await useAuthSession()
+    const id = decoded.subject.properties.accountID
+    await session.update((value) => {
+      return {
+        ...value,
+        account: {
+          ...value.account,
+          [id]: {
+            id,
+            email: decoded.subject.properties.email,
+          },
         },
         },
-      },
-      current: id,
-    }
-  })
-  return redirect("/auth")
+        current: id,
+      }
+    })
+    return redirect("/auth")
+  } catch (e: any) {
+    return new Response(
+      JSON.stringify({
+        error: e.message,
+        cause: Object.fromEntries(url.searchParams.entries()),
+      }),
+      { status: 500 },
+    )
+  }
 }
 }

+ 5 - 1
packages/console/function/src/auth.ts

@@ -123,7 +123,11 @@ export default {
             },
             },
           }).then((x) => x.json())) as any
           }).then((x) => x.json())) as any
           subject = user.id.toString()
           subject = user.id.toString()
-          email = emails.find((x: any) => x.primary && x.verified)?.email
+
+          const primaryEmail = emails.find((x: any) => x.primary)
+          if (!primaryEmail) throw new Error("No primary email found for GitHub user")
+          if (!primaryEmail.verified) throw new Error("Primary email for GitHub user not verified")
+          email = primaryEmail.email
         } else if (response.provider === "google") {
         } else if (response.provider === "google") {
           if (!response.id.email_verified) throw new Error("Google email not verified")
           if (!response.id.email_verified) throw new Error("Google email not verified")
           subject = response.id.sub as string
           subject = response.id.sub as string