From 96ba6fd53137abe0ebd36ea2a77a179bc54100de Mon Sep 17 00:00:00 2001 From: Allen Zhou <46854522+allenzhou101@users.noreply.github.com> Date: Wed, 5 Feb 2025 12:38:26 -0800 Subject: [PATCH] Convert OAuthMetadata and OAuthTokens to zod --- client/src/lib/auth.ts | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/client/src/lib/auth.ts b/client/src/lib/auth.ts index 918ac4d..040d35a 100644 --- a/client/src/lib/auth.ts +++ b/client/src/lib/auth.ts @@ -1,16 +1,21 @@ import pkceChallenge from "pkce-challenge"; import { SESSION_KEYS } from "./constants"; +import { z } from "zod"; -export interface OAuthMetadata { - authorization_endpoint: string; - token_endpoint: string; -} +export const OAuthMetadataSchema = z.object({ + authorization_endpoint: z.string(), + token_endpoint: z.string() +}); -export interface OAuthTokens { - access_token: string; - refresh_token?: string; - expires_in?: number; -} +export type OAuthMetadata = z.infer; + +export const OAuthTokensSchema = z.object({ + access_token: z.string(), + refresh_token: z.string().optional(), + expires_in: z.number().optional() +}); + +export type OAuthTokens = z.infer; export async function discoverOAuthMetadata( serverUrl: string, @@ -93,8 +98,7 @@ export async function handleOAuthCallback( throw new Error("Token exchange failed"); } - const data = await response.json(); - return data; + return await response.json(); } export async function refreshAccessToken( @@ -122,6 +126,5 @@ export async function refreshAccessToken( throw new Error("Token refresh failed"); } - const data = await response.json(); - return data; + return await response.json(); }