Skip to content

Commit aee6072

Browse files
fixes hacky email wxists check
1 parent 2aaf68b commit aee6072

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/api/supabase/queries/email.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import supabase from '../createClient';
2+
3+
/**
4+
* Checks if the current email is already tied to a user account.
5+
* @returns a Promise of whether the email is already in use
6+
*/
7+
export async function checkEmailExists(email: string): Promise<boolean> {
8+
const { data, error } = await supabase.rpc('check_email_exists', {
9+
p_email: email,
10+
});
11+
12+
if (error) {
13+
throw new Error(`Error checking email existence: ${error.message}`);
14+
}
15+
16+
return data;
17+
}

src/utils/AuthProvider.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import { AuthError, AuthResponse, Session } from '@supabase/supabase-js';
1212
import type { UUID } from 'crypto';
1313
import supabase from '@/api/supabase/createClient';
14+
import { checkEmailExists } from '@/api/supabase/queries/email';
1415

1516
export interface AuthContextType {
1617
session?: Session;
@@ -104,11 +105,7 @@ export default function AuthProvider({
104105
// This code is largely taken from https://github.com/orgs/supabase/discussions/1282#discussioncomment-5230475
105106

106107
// User exists, but is fake. See https://supabase.com/docs/reference/javascript/auth-signup
107-
if (
108-
value.data.user &&
109-
value.data.user.identities &&
110-
value.data.user.identities.length === 0
111-
) {
108+
if (await checkEmailExists(email)) {
112109
const authError = new AuthError(
113110
'A user account with this email already exists',
114111
);

0 commit comments

Comments
 (0)