Cleaned up auth. Ensured everything is necessary
This commit is contained in:
		
							
								
								
									
										0
									
								
								assets/fonts/SpaceMono-Regular.ttf
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							
							
						
						
									
										0
									
								
								assets/fonts/SpaceMono-Regular.ttf
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							| @@ -30,6 +30,7 @@ const AppleSignInButton = () => { | |||||||
|           credential.fullName && credential.fullName.givenName && credential.fullName.familyName |           credential.fullName && credential.fullName.givenName && credential.fullName.familyName | ||||||
|             ? `${credential.fullName.givenName} ${credential.fullName.familyName}` |             ? `${credential.fullName.givenName} ${credential.fullName.familyName}` | ||||||
|             : null; |             : null; | ||||||
|  |  | ||||||
|         const { |         const { | ||||||
|           error, |           error, | ||||||
|           data: { user, session }, |           data: { user, session }, | ||||||
| @@ -41,26 +42,25 @@ const AppleSignInButton = () => { | |||||||
|         if (!error && session) { |         if (!error && session) { | ||||||
|           if (email) { |           if (email) { | ||||||
|             const data: updateUser = { |             const data: updateUser = { | ||||||
|  |               id: session?.user.id, | ||||||
|  |               updated_at: new Date(), | ||||||
|               email, |               email, | ||||||
|               full_name: full_name ?? '', |               full_name: full_name ?? '', | ||||||
|  |               provider: 'apple', | ||||||
|             }; |             }; | ||||||
|             const { error: authUpdateError } = await supabase.auth.updateUser({ |             const { error: updateError } = await supabase.auth.updateUser({ data }); | ||||||
|               data, |             if (updateError) Alert.alert('Error updating auth info:', updateError.message); | ||||||
|             }); |             const { error: updateProfileError } = await supabase | ||||||
|             if (authUpdateError) |  | ||||||
|               Alert.alert('Error updating auth info:', authUpdateError.message); |  | ||||||
|             const { error: updateError } = await supabase |  | ||||||
|               .from('profiles') |               .from('profiles') | ||||||
|               .upsert({ |               .upsert({ | ||||||
|                 id: session.user.id, |                 id: session?.user.id ?? '', | ||||||
|                 full_name, |  | ||||||
|                 email, |  | ||||||
|                 provider: 'apple', |  | ||||||
|                 updated_at: new Date(), |                 updated_at: new Date(), | ||||||
|             }); |                 email: email ?? '', | ||||||
|             if (updateError) { |                 full_name: full_name ?? '', | ||||||
|               console.error('Error updating user metadata:', updateError); |                 provider: 'apple', | ||||||
|             } |               }); | ||||||
|  |             if (updateProfileError) | ||||||
|  |               Alert.alert('Error updating profile:', updateProfileError.message); | ||||||
|           } |           } | ||||||
|         } |         } | ||||||
|       } else { |       } else { | ||||||
|   | |||||||
| @@ -58,24 +58,15 @@ const Auth = () => { | |||||||
|     } = await supabase.auth.signUp({ |     } = await supabase.auth.signUp({ | ||||||
|       email, |       email, | ||||||
|       password, |       password, | ||||||
|  |       options: { | ||||||
|  |         data: { | ||||||
|  |           full_name, | ||||||
|  |           provider: 'email', | ||||||
|  |         } | ||||||
|  |       } | ||||||
|     }); |     }); | ||||||
|     if (error) Alert.alert(error.message); |     if (error) Alert.alert(error.message); | ||||||
|     else if (!session) Alert.alert('Please check your inbox for email verification!'); |     else if (!session) Alert.alert('Please check your inbox for email verification!'); | ||||||
|     else { |  | ||||||
|       const { error: updateProfileError } = await supabase |  | ||||||
|         .from('profiles') |  | ||||||
|         .upsert({ |  | ||||||
|           id: session.user.id, |  | ||||||
|           full_name, |  | ||||||
|           email, |  | ||||||
|           provider: 'email', |  | ||||||
|           updated_at: new Date(), |  | ||||||
|       }); |  | ||||||
|       if (updateProfileError) { |  | ||||||
|         Alert.alert('Error updating profile:', updateProfileError.message); |  | ||||||
|         console.error('Error updating profile:', updateProfileError.message); |  | ||||||
|       } |  | ||||||
|     } |  | ||||||
|     setLoading(false); |     setLoading(false); | ||||||
|   }; |   }; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -28,7 +28,7 @@ const AzureSignIn = () => { | |||||||
|   const signInWithAzure = async () => { |   const signInWithAzure = async () => { | ||||||
|     try { |     try { | ||||||
|       setLoading(true); |       setLoading(true); | ||||||
|  |        | ||||||
|       // Create the MSAL auth request |       // Create the MSAL auth request | ||||||
|       const request = new AuthSession.AuthRequest({ |       const request = new AuthSession.AuthRequest({ | ||||||
|         clientId: clientId!, |         clientId: clientId!, | ||||||
| @@ -38,17 +38,12 @@ const AzureSignIn = () => { | |||||||
|         responseType: AuthSession.ResponseType.Code, |         responseType: AuthSession.ResponseType.Code, | ||||||
|       }); |       }); | ||||||
|  |  | ||||||
|       // Generate the auth URL with PKCE |       // Generate the auth URL with PKCE & open in browser | ||||||
|       const authUrl = await request.makeAuthUrlAsync(discovery); |       const authUrl = await request.makeAuthUrlAsync(discovery); | ||||||
|       console.log('Generated auth URL:', authUrl); |  | ||||||
|  |  | ||||||
|       // Open the auth URL in a browser |  | ||||||
|       const result = await WebBrowser.openAuthSessionAsync(authUrl, redirectUri, { |       const result = await WebBrowser.openAuthSessionAsync(authUrl, redirectUri, { | ||||||
|         showInRecents: true, |         showInRecents: true, | ||||||
|       }); |       }); | ||||||
|  |  | ||||||
|       console.log('Auth session result type:', result.type); |  | ||||||
|  |  | ||||||
|       if (result.type === 'success' && result.url) { |       if (result.type === 'success' && result.url) { | ||||||
|         // Parse the URL to get the authorization code |         // Parse the URL to get the authorization code | ||||||
|         const { params, errorCode } = QueryParams.getQueryParams(result.url); |         const { params, errorCode } = QueryParams.getQueryParams(result.url); | ||||||
| @@ -57,13 +52,10 @@ const AzureSignIn = () => { | |||||||
|           const errorMessage = params.error_description || params.error || errorCode; |           const errorMessage = params.error_description || params.error || errorCode; | ||||||
|           throw new Error(`Error during authentication: ${errorMessage}`); |           throw new Error(`Error during authentication: ${errorMessage}`); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         if (!params.code) { |         if (!params.code) { | ||||||
|           throw new Error('No authorization code received'); |           throw new Error('No authorization code received'); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         console.log('Authorization code received'); |  | ||||||
|  |  | ||||||
|         // Exchange the code for tokens |         // Exchange the code for tokens | ||||||
|         const tokenResult = await AuthSession.exchangeCodeAsync( |         const tokenResult = await AuthSession.exchangeCodeAsync( | ||||||
|           { |           { | ||||||
| @@ -76,9 +68,6 @@ const AzureSignIn = () => { | |||||||
|           }, |           }, | ||||||
|           discovery, |           discovery, | ||||||
|         ); |         ); | ||||||
|  |  | ||||||
|         console.log('Token exchange successful'); |  | ||||||
|  |  | ||||||
|         if (!tokenResult.idToken) { |         if (!tokenResult.idToken) { | ||||||
|           throw new Error('No ID token received'); |           throw new Error('No ID token received'); | ||||||
|         } |         } | ||||||
| @@ -88,40 +77,38 @@ const AzureSignIn = () => { | |||||||
|           provider: 'azure', |           provider: 'azure', | ||||||
|           token: tokenResult.idToken, |           token: tokenResult.idToken, | ||||||
|         }); |         }); | ||||||
|  |         console.log(JSON.stringify({ data, error }, null, 2)); | ||||||
|  |  | ||||||
|         // Check if profies table already has info (User is signing in, not signing up) |         const { data: profile, error: profileError } = await supabase | ||||||
|         const { data: profileData, error: profileError } = await supabase |  | ||||||
|           .from('profiles') |           .from('profiles') | ||||||
|           .select('*') |           .select('*') | ||||||
|           .eq('id', data.user?.id) |           .eq('id', data.user?.id) | ||||||
|           .single(); |           .single(); | ||||||
|  |  | ||||||
|         if (profileData.email === '' || !profileData.email && data.session?.user.email) { |         if (profileError) { | ||||||
|           const updateData: updateUser = { |           console.error('Supabase profile error:', profileError); | ||||||
|             email: data.session?.user.email ?? '', |           throw profileError; | ||||||
|           }; |         } | ||||||
|           const { error: updateAuthError } = await supabase.auth.updateUser({ |         console.log(JSON.stringify({ profile, error: profileError }, null, 2)); | ||||||
|             data: updateData, |  | ||||||
|           }); |         if (profile?.provider !== 'azure') { | ||||||
|           if (updateAuthError) |  | ||||||
|             Alert.alert('Error updating auth info:', updateAuthError.message); |  | ||||||
|           const { error: updateProfileError } = await supabase |           const { error: updateProfileError } = await supabase | ||||||
|           .from('profiles') |             .from('profiles') | ||||||
|           .upsert({ |             .upsert({ | ||||||
|             id: data.session?.user.id ?? '', |               id: data.session?.user.id ?? '', | ||||||
|             email: data.session?.user.email ?? '', |               provider: 'azure', | ||||||
|             provider: 'azure', |               updated_at: new Date(), | ||||||
|             updated_at: new Date(), |             }); | ||||||
|           }); |           if (updateProfileError) {  | ||||||
|           if (updateProfileError) |             console.error('Supabase profile error:', updateProfileError); | ||||||
|             Alert.alert('Error updating profile:', updateProfileError.message); |             Alert.alert('Error updating profile:', updateProfileError.message); | ||||||
|  |           } | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         if (error) { |         if (error) { | ||||||
|           console.error('Supabase sign-in error:', error); |           console.error('Supabase sign-in error:', error); | ||||||
|           throw error; |           throw error; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         console.log('Successfully signed in with Azure via Supabase'); |         console.log('Successfully signed in with Azure via Supabase'); | ||||||
|         return data; |         return data; | ||||||
|       } else { |       } else { | ||||||
|   | |||||||
							
								
								
									
										0
									
								
								scripts/files_to_clipboard
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							
							
						
						
									
										0
									
								
								scripts/files_to_clipboard
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							
							
								
								
									
										59
									
								
								scripts/supabase_schema.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								scripts/supabase_schema.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,59 @@ | |||||||
|  | -- Create a table for public profiles | ||||||
|  | create table profiles ( | ||||||
|  |   id uuid references auth.users on delete cascade not null primary key, | ||||||
|  |   updated_at timestamp with time zone, | ||||||
|  |   email text, | ||||||
|  |   full_name text, | ||||||
|  |   avatar_url text, | ||||||
|  |   provider text, | ||||||
|  |  | ||||||
|  |   constraint full_name_length check (char_length(full_name) >= 3 and char_length(full_name) <= 50), | ||||||
|  | ); | ||||||
|  | -- Set up Row Level Security (RLS) | ||||||
|  | -- See https://supabase.com/docs/guides/auth/row-level-security for more details. | ||||||
|  | alter table profiles | ||||||
|  |   enable row level security; | ||||||
|  |  | ||||||
|  | create policy "Public profiles are viewable by everyone." on profiles | ||||||
|  |   for select using (true); | ||||||
|  |  | ||||||
|  | create policy "Users can insert their own profile." on profiles | ||||||
|  |   for insert with check ((select auth.uid()) = id); | ||||||
|  |  | ||||||
|  | create policy "Users can update own profile." on profiles | ||||||
|  |   for update using ((select auth.uid()) = id); | ||||||
|  |  | ||||||
|  | -- This trigger automatically creates a profile entry when a new user signs up via Supabase Auth. | ||||||
|  | -- See https://supabase.com/docs/guides/auth/managing-user-data#using-triggers for more details. | ||||||
|  | create function public.handle_new_user() | ||||||
|  | returns trigger | ||||||
|  | set search_path = '' | ||||||
|  | as $$ | ||||||
|  | begin | ||||||
|  |   insert into public.profiles (id, email, full_name, avatar_url, provider, updated_at) | ||||||
|  |   values ( | ||||||
|  |     new.id, | ||||||
|  |     new.email, | ||||||
|  |     new.raw_user_meta_data->>'full_name', | ||||||
|  |     new.raw_user_meta_data->>'avatar_url' | ||||||
|  |     new.raw_user_meta_data->>'provider', | ||||||
|  |     now() | ||||||
|  |   ); | ||||||
|  |   return new; | ||||||
|  | end; | ||||||
|  | $$ language plpgsql security definer; | ||||||
|  | create trigger on_auth_user_created | ||||||
|  |   after insert on auth.users | ||||||
|  |   for each row execute procedure public.handle_new_user(); | ||||||
|  |  | ||||||
|  | -- Set up Storage! | ||||||
|  | insert into storage.buckets (id, name) | ||||||
|  |   values ('avatars', 'avatars'); | ||||||
|  |  | ||||||
|  | -- Set up access controls for storage. | ||||||
|  | -- See https://supabase.com/docs/guides/storage#policy-examples for more details. | ||||||
|  | create policy "Avatar images are publicly accessible." on storage.objects | ||||||
|  |   for select using (bucket_id = 'avatars'); | ||||||
|  |  | ||||||
|  | create policy "Anyone can upload an avatar." on storage.objects | ||||||
|  |   for insert with check (bucket_id = 'avatars'); | ||||||
		Reference in New Issue
	
	Block a user