| @@ -2,6 +2,7 @@ · 1 unmodified line |
| 2 | 2 | |
| 3 | 3 | import { createContext, useCallback, useContext, useMemo } from "react"; |
| 4 | 4 | import { ConvexProviderWithAuth } from "convex/react"; |
| 5 | +import { z } from "zod"; |
| 5 | 6 | import { |
| 6 | 7 | AuthKitProvider, |
| 7 | 8 | useAccessToken, |
| @@ -36,6 +37,8 @@ type WorkosAuthProfile = { · 28 unmodified lines |
| 36 | 37 | avatarUrl?: string | null; |
| 37 | 38 | }; |
| 38 | 39 | |
| 40 | +const recordSchema = z.record(z.unknown()); |
| 41 | + |
| 39 | 42 | function readNonEmptyString(value: unknown): string | undefined { |
| 40 | 43 | if (typeof value !== "string") { |
| 41 | 44 | return undefined; |
| @@ -57,11 +60,12 @@ function getRecordString(record: Record<string, unknown>, keys: string[]): strin · 15 unmodified lines |
| 57 | 60 | } |
| 58 | 61 | |
| 59 | 62 | function buildWorkosAuthProfile(user: unknown): WorkosAuthProfile | null { |
| 60 | | − if (!user || typeof user !== "object") { |
| 63 | + const parsedUser = recordSchema.safeParse(user); |
| 64 | + if (!parsedUser.success) { |
| 61 | 65 | return null; |
| 62 | 66 | } |
| 63 | 67 | |
| 64 | | − const userRecord = user as Record<string, unknown>; |
| 68 | + const userRecord = parsedUser.data; |
| 65 | 69 | const firstName = getRecordString(userRecord, ["firstName", "first_name", "givenName", "given_name"]); |
| 66 | 70 | const lastName = getRecordString(userRecord, ["lastName", "last_name", "familyName", "family_name"]); |
| 67 | 71 | const combinedName = [firstName, lastName].filter(Boolean).join(" ").trim(); |