我正在使用 expo 开发一个反应本机应用程序。我用过expo-auth-session/providers/google
包登录。它在世博环境中工作正常,但是当我为 Android 平台构建应用程序时,由于 google 登录未完成,因此不会发生任何情况
Here is AuthScreen
code:
...
import decode from 'jwt-decode';
import { ANDROID_CLIENT_ID, EXPO_CLIENT_ID, IOS_CLIENT_ID } from '../../env';
import * as Google from 'expo-auth-session/providers/google';
import * as WebBrowser from 'expo-web-browser';
...
import { useFocusEffect } from '@react-navigation/native';
...
WebBrowser.maybeCompleteAuthSession();
const AuthScreen = () => {
const [isLoading, setIsLoading] = useState(false);
const [isSigningIn, setIsSigningIn] = useState(false);
// eslint-disable-next-line no-unused-vars
const [req, res, promtAsync] = Google.useIdTokenAuthRequest({
expoClientId: EXPO_CLIENT_ID,
androidClientId: ANDROID_CLIENT_ID,
iosClientId: IOS_CLIENT_ID,
scopes: ['profile', 'email'],
});
const dispatch = useDispatch();
useFocusEffect(
useCallback(() => {
setIsLoading(true);
loadFromStorage('user')
.then(() => {
setIsLoading(false);
dispatch(setIsLoggedIn(true));
})
.catch(() => setIsLoading(false));
}, []),
);
const onLogin = () =>
promtAsync()
.then((response) => {
console.log({ res, req });
if (response?.type === 'success') {
const userInfo = decode(response?.params?.id_token);
if (response?.params?.id_token) {
setIsSigningIn(true);
setIsLoading(true);
login({
google_id_token: response?.params?.id_token,
name: userInfo?.name,
profile_image: userInfo?.picture,
agent: Platform.OS,
})
.then((res) => {
const jwtToken = res?.headers?.['jwt-token'];
saveToStorage({
key: 'jwtToken',
value: jwtToken,
});
saveToStorage({ key: 'user', value: userInfo });
setIsSigningIn(false);
setIsLoading(false);
dispatch(setIsLoggedIn(true));
})
.catch(() => {
console.log('Unable to sign-in.');
setIsSigningIn(false);
setIsLoading(false);
});
}
}
})
.catch(() => {
console.log('Unable to sign-in.');
setIsSigningIn(false);
setIsLoading(false);
});
return (
<View style={styles.container}>
{isLoading ? (
<Loader />
) : (
<>
<ErpLogo />
<TouchableNativeFeedback onPress={onLogin}>
<GoogleSignInButton style={styles.googleButton} />
</TouchableNativeFeedback>
</>
)}
{isSigningIn && (
<Text
style={{
position: 'absolute',
bottom: Dimensions.get('screen').height / 30,
fontFamily: fonts.firaSans400,
color: colors.textColor,
}}
>
Signing in...
</Text>
)}
</View>
);
};
export default AuthScreen;
这是我的app.json
file:
{
"expo": {
"name": "X",
"slug": "x",
"version": "1.0.0",
"owner": "xyz",
"orientation": "portrait",
"icon": "./assets/img/erp.png",
"userInterfaceStyle": "light",
"scheme": "com.xxx.xx",
"splash": {
"image": "./assets/splash.png",
"resizeMode": "contain",
"backgroundColor": "#ffffff"
},
"updates": {
"fallbackToCacheTimeout": 0
},
"assetBundlePatterns": ["**/*"],
"ios": {
"supportsTablet": true,
"bundleIdentifier": "com.xxx.xx",
"buildNumber": "1.0.0"
},
"android": {
"package": "com.xxx.xx",
"versionCode": 1,
"adaptiveIcon": {
"foregroundImage": "./assets/adaptive-icon.png",
"backgroundColor": "#FFFFFF"
},
},
"web": {
"favicon": "./assets/favicon.png"
}
}
}