Cleaned up auth. Ensured everything is necessary

This commit is contained in:
2025-03-11 10:05:31 -05:00
parent 6c0a275ee0
commit 86d1df3558
6 changed files with 99 additions and 62 deletions

View File

@ -28,7 +28,7 @@ const AzureSignIn = () => {
const signInWithAzure = async () => {
try {
setLoading(true);
// Create the MSAL auth request
const request = new AuthSession.AuthRequest({
clientId: clientId!,
@ -38,17 +38,12 @@ const AzureSignIn = () => {
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);
console.log('Generated auth URL:', authUrl);
// Open the auth URL in a browser
const result = await WebBrowser.openAuthSessionAsync(authUrl, redirectUri, {
showInRecents: true,
});
console.log('Auth session result type:', result.type);
if (result.type === 'success' && result.url) {
// Parse the URL to get the authorization code
const { params, errorCode } = QueryParams.getQueryParams(result.url);
@ -57,13 +52,10 @@ const AzureSignIn = () => {
const errorMessage = params.error_description || params.error || errorCode;
throw new Error(`Error during authentication: ${errorMessage}`);
}
if (!params.code) {
throw new Error('No authorization code received');
}
console.log('Authorization code received');
// Exchange the code for tokens
const tokenResult = await AuthSession.exchangeCodeAsync(
{
@ -76,9 +68,6 @@ const AzureSignIn = () => {
},
discovery,
);
console.log('Token exchange successful');
if (!tokenResult.idToken) {
throw new Error('No ID token received');
}
@ -88,40 +77,38 @@ const AzureSignIn = () => {
provider: 'azure',
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: profileData, error: profileError } = await supabase
const { data: profile, error: profileError } = await supabase
.from('profiles')
.select('*')
.eq('id', data.user?.id)
.single();
if (profileData.email === '' || !profileData.email && data.session?.user.email) {
const updateData: updateUser = {
email: data.session?.user.email ?? '',
};
const { error: updateAuthError } = await supabase.auth.updateUser({
data: updateData,
});
if (updateAuthError)
Alert.alert('Error updating auth info:', updateAuthError.message);
if (profileError) {
console.error('Supabase profile error:', profileError);
throw profileError;
}
console.log(JSON.stringify({ profile, error: profileError }, null, 2));
if (profile?.provider !== 'azure') {
const { error: updateProfileError } = await supabase
.from('profiles')
.upsert({
id: data.session?.user.id ?? '',
email: data.session?.user.email ?? '',
provider: 'azure',
updated_at: new Date(),
});
if (updateProfileError)
.from('profiles')
.upsert({
id: data.session?.user.id ?? '',
provider: 'azure',
updated_at: new Date(),
});
if (updateProfileError) {
console.error('Supabase profile error:', updateProfileError);
Alert.alert('Error updating profile:', updateProfileError.message);
}
}
if (error) {
console.error('Supabase sign-in error:', error);
throw error;
}
console.log('Successfully signed in with Azure via Supabase');
return data;
} else {