You can try this. It’s worked for me. Function supabase.auth.signInWithOAuth doesn’t open URL, just get back the object, so you need to take URL from the object and click on it. Rest of code takes a token extract email from it and prints it on the screen, you can change it based on your needs.
import { StatusBar } from “expo-status-bar”;
import { Button, StyleSheet, Text, View } from “react-native”;
import { supabase } from “./lib/supabase”;
import { setupURLPolyfill } from “react-native-url-polyfill”;
import “react-native-url-polyfill/auto”;
import React, { useState, useEffect } from “react”;
import jwtDecode from “jwt-decode”;
import { Linking } from “react-native”;
import * as AuthSession from ‘expo-auth-session’;
import * as Random from ‘expo-random’;
var email;
setupURLPolyfill();
export default function App() {
const [accessToken, setAccessToken] = useState(“”);
const [error, setError] = useState(null);
const loginWithAzure = async () => {
try {
const { data, error } = await supabase.auth.signInWithOAuth({
provider: “azure”,
options: {
scopes: “email”
},
});
if (error) {
console.log("Error signing in with Azure:", error);
setError(error);
} else {
console.log("Logged in with Azure:", data);
// Open the authentication URL in a browser
const url = data.url;
const supported = await Linking.canOpenURL(url);
if (supported) {
await Linking.openURL(url);
// Listen for URL changes
Linking.addEventListener("url", handleURLChange);
} else {
console.log("Don't know how to open URI: " + url);
}
}
} catch (error) {
console.log("Login error:", error);
setError(error);
}
};
const handleURLChange = async (event) => {
// Get the URL from the event object
const url = event.url;
// Check if the URL starts with the redirect URI
const redirectURI = AuthSession.makeRedirectUri();
if (url.startsWith(redirectURI)) {
// Remove the listener to prevent multiple calls
//Linking.removeEventListener("url");
Linking.removeAllListeners("url")
// Parse the URL and get the access token
const queryParams = new URLSearchParams(url.split("#")[1]);
const accessToken = queryParams.get("access_token");
const decodedToken = jwtDecode(accessToken);
email = decodedToken.email;
// Update state with access token
setAccessToken(accessToken);
// Set the access token as a header for Supabase requests
supabase.auth.setSession(accessToken)
}
};
return (
{error ? (
<Text style={[styles.text, styles.error]}>
Error: {error.message || “An unknown error occurred.”}
) : null}
{accessToken ? (
User Email: {email}
) : null}
);
}```