I am using expo-auth-session
within a bare expo app.
I can’t get the the async code within the following useEffect block to run, I get the ‘pre await’ log in console and then it sits and waits until I tap the screen.
useEffect(() => {
if (result && result.type === 'success') {
(async () => {
var codeVerifier = request?.codeVerifier || '';
console.log('pre await')
var res = await fetch('http://google.com');
console.log('post await')
})()
}
}, [result]);
This only seems to happen when the app is in debug mode - if I disable debug then the code runs fine.
Full code below:
import React, { useContext, useEffect, useState } from 'react';
import { Text, TouchableOpacity, View } from 'react-native';
import * as AuthSession from 'expo-auth-session';
type LoginButtonProps = {
}
const LoginButton: React.FC<LoginButtonProps> = props => {
const discoveryDocument = AuthSession.useAutoDiscovery('https://example.com/auth');
var [isLoggingIn, setIsLoggingIn] = useState(false);
const useProxy = false;
const redirectUri = AuthSession.makeRedirectUri({
native: 'myapp://login',
useProxy,
});
const [request, result, promptAsync] = AuthSession.useAuthRequest(
{
clientId: Constants.auth.clientId,
redirectUri,
scopes: Constants.auth.scopes,
prompt: AuthSession.Prompt.Login
},
discoveryDocument
);
useEffect(() => {
if (result && result.type === 'success') {
(async () => {
var codeVerifier = request?.codeVerifier || '';
console.log('pre await')
var res = await fetch('http://google.com');
console.log('post await')
})()
}
}, [result]);
const openLoginPage = async () => {
setIsLoggingIn(true);
await promptAsync({
useProxy
});
}
if (!request) {
return null;
}
return <TouchableOpacity onPress={openLoginPage} style={{ flexDirection: 'row', justifyContent: 'center' }} >
<Text>Login</Text>
</TouchableOpacity>
}
export default LoginButton;