Skip to content

Commit

Permalink
Remove console log and refactor username check to use GraphQL
Browse files Browse the repository at this point in the history
  • Loading branch information
jamalsoueidan committed Jul 12, 2024
1 parent d038d7a commit 87cb279
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
1 change: 0 additions & 1 deletion app/routes/account_.authorize.tsx
Original file line number Diff line number Diff line change
@@ -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();
}
22 changes: 13 additions & 9 deletions app/routes/api.check-username.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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;
};

0 comments on commit 87cb279

Please sign in to comment.