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