All 3 auth methods work & update public profile table as well
This commit is contained in:
@ -7,6 +7,7 @@ import * as QueryParams from 'expo-auth-session/build/QueryParams';
|
||||
import { supabase } from '@/lib/supabase';
|
||||
import { ThemedView, ThemedButton, ThemedText } from '@/components/theme';
|
||||
import { Colors } from '@/constants/Colors';
|
||||
import type { updateUser } from '@/constants/Types';
|
||||
|
||||
WebBrowser.maybeCompleteAuthSession();
|
||||
|
||||
@ -27,8 +28,6 @@ const AzureSignIn = () => {
|
||||
const signInWithAzure = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
console.log('Starting Azure sign-in with tenant-specific endpoint');
|
||||
console.log('Redirect URI:', redirectUri);
|
||||
|
||||
// Create the MSAL auth request
|
||||
const request = new AuthSession.AuthRequest({
|
||||
@ -44,31 +43,27 @@ const AzureSignIn = () => {
|
||||
console.log('Generated auth URL:', authUrl);
|
||||
|
||||
// Open the auth URL in a browser
|
||||
const result = await WebBrowser.openAuthSessionAsync(
|
||||
authUrl,
|
||||
redirectUri,
|
||||
{
|
||||
showInRecents: true,
|
||||
}
|
||||
);
|
||||
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);
|
||||
|
||||
|
||||
if (errorCode || params.error) {
|
||||
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(
|
||||
{
|
||||
@ -79,26 +74,54 @@ const AzureSignIn = () => {
|
||||
code_verifier: request.codeVerifier || '',
|
||||
},
|
||||
},
|
||||
discovery
|
||||
discovery,
|
||||
);
|
||||
|
||||
|
||||
console.log('Token exchange successful');
|
||||
|
||||
|
||||
if (!tokenResult.idToken) {
|
||||
throw new Error('No ID token received');
|
||||
}
|
||||
|
||||
|
||||
// Now use the ID token to sign in with Supabase
|
||||
const { data, error } = await supabase.auth.signInWithIdToken({
|
||||
provider: 'azure',
|
||||
token: tokenResult.idToken,
|
||||
});
|
||||
|
||||
|
||||
// Check if profies table already has info (User is signing in, not signing up)
|
||||
const { data: profileData, 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);
|
||||
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)
|
||||
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 {
|
||||
@ -114,16 +137,13 @@ const AzureSignIn = () => {
|
||||
|
||||
return (
|
||||
<ThemedView style={styles.verticallySpaced}>
|
||||
<ThemedButton
|
||||
disabled={loading}
|
||||
onPress={signInWithAzure}
|
||||
>
|
||||
<ThemedButton disabled={loading} onPress={signInWithAzure}>
|
||||
<ThemedText
|
||||
lightColor={Colors.dark.text}
|
||||
darkColor={Colors.light.text}
|
||||
type="defaultSemiBold"
|
||||
type='defaultSemiBold'
|
||||
>
|
||||
{loading ? "Signing in..." : "Sign in with Microsoft"}
|
||||
{loading ? 'Signing in...' : 'Sign in with Microsoft'}
|
||||
</ThemedText>
|
||||
</ThemedButton>
|
||||
</ThemedView>
|
||||
|
Reference in New Issue
Block a user