From 87cb279ba2e04b540a3c99c1b70765691f8e5d72 Mon Sep 17 00:00:00 2001 From: Jamal Soueidan Date: Fri, 12 Jul 2024 05:58:58 +0200 Subject: [PATCH] Remove console log and refactor username check to use GraphQL --- app/routes/account_.authorize.tsx | 1 - app/routes/api.check-username.ts | 22 +++++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/app/routes/account_.authorize.tsx b/app/routes/account_.authorize.tsx index 05a04f54..f7a8723e 100644 --- a/app/routes/account_.authorize.tsx +++ b/app/routes/account_.authorize.tsx @@ -1,6 +1,5 @@ import type {LoaderFunctionArgs} from '@shopify/remix-oxygen'; export async function loader({context}: LoaderFunctionArgs) { - console.log('called this before redirect'); return context.customerAccount.authorize(); } diff --git a/app/routes/api.check-username.ts b/app/routes/api.check-username.ts index 0e12286b..142c786c 100644 --- a/app/routes/api.check-username.ts +++ b/app/routes/api.check-username.ts @@ -3,18 +3,23 @@ import { json, type LoaderFunctionArgs, } from '@shopify/remix-oxygen'; -import {getBookingShopifyApi} from '~/lib/api/bookingShopifyApi'; -import {type UserUsernameTakenResponsePayload} from '~/lib/api/model'; +import {USER_METAOBJECT_QUERY} from '~/graphql/fragments/UserMetaobject'; -export async function loader({request}: LoaderFunctionArgs) { +export async function loader({request, context}: LoaderFunctionArgs) { + const {storefront} = context; const url = new URL(request.url); const searchParams = new URLSearchParams(url.search); const username = searchParams.get('username') || ''; + if (username.length <= 3) return true; - const {payload: usernameTaken} = - await getBookingShopifyApi().userUsernameTaken(username); + const {metaobject: user} = await storefront.query(USER_METAOBJECT_QUERY, { + variables: { + username, + }, + cache: context.storefront.CacheShort(), + }); - return json(usernameTaken); + return json(user !== null); } export const isUsernameUnique = @@ -24,7 +29,6 @@ export const isUsernameUnique = const response = await fetch( `${url.origin}/api/check-username?username=${username}`, ); - const data: UserUsernameTakenResponsePayload = - (await response.json()) as any; - return data.usernameTaken; + const data = await response.json(); + return data as boolean; };