Skip to content

Commit

Permalink
fixes hacky email wxists check
Browse files Browse the repository at this point in the history
  • Loading branch information
pragyakallanagoudar committed May 18, 2024
1 parent 6a59c2b commit 94888fe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
17 changes: 17 additions & 0 deletions src/api/supabase/queries/email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import supabase from '../createClient';

/**
* Checks if the current email is already tied to a user account.
* @returns a Promise of whether the email is already in use
*/
export async function checkEmailExists(email: string): Promise<boolean> {
const { data, error } = await supabase.rpc('check_email_exists', {
p_email: email,
});

if (error) {
throw new Error(`Error checking email existence: ${error.message}`);
}

return data;
}
7 changes: 2 additions & 5 deletions src/utils/AuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { AuthError, AuthResponse, Session } from '@supabase/supabase-js';
import type { UUID } from 'crypto';
import supabase from '@/api/supabase/createClient';
import { checkEmailExists } from '@/api/supabase/queries/email';

export interface AuthContextType {
session?: Session;
Expand Down Expand Up @@ -106,11 +107,7 @@ export default function AuthProvider({
// This code is largely taken from https://github.com/orgs/supabase/discussions/1282#discussioncomment-5230475

// User exists, but is fake. See https://supabase.com/docs/reference/javascript/auth-signup
if (
value.data.user &&
value.data.user.identities &&
value.data.user.identities.length === 0
) {
if (await checkEmailExists(email)) {
const authError = new AuthError(
'A user account with this email already exists',
);
Expand Down

0 comments on commit 94888fe

Please sign in to comment.