最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Setting up Auth0 in React NativeExpo - Stack Overflow

programmeradmin1浏览0评论

I set up Auth0 in React Native (using Expo iOS app Xcode simulator). When "login" is invoked, it doesn't run "authorize()". It's not taking me to the Auth0 login page. For my callback URL in Auth0, I set it to localhost:19000 (which is the same as my expo-client).

import React from 'react';
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Button, Text, View } from 'react-native';
import {useAuth0, Auth0Provider} from 'react-native-auth0';

const Home = () => {
  const {authorize, clearSession, user} = useAuth0();

  const login = async () => {
    try {
      await authorize();
    } catch (err) {
      console.log(err);
    }
  }

  const onLogout = async () => {
    try {
      await clearSession();
    } catch (err) {
      console.log('log out canceled');
    }
  }

  return (
    <View style={styles.container}>
      {!user && <Button onPress={login} title="Log in" />}
      {user && <Text>Logged in as {user.name}</Text>}
    </View>
  )
}

const App = () => {
  return (
    <Auth0Provider domain={"ihavemydomainhere} clientId={"ihavemyclientidhere"}>
      <Home />
    </Auth0Provider>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#F5FCFF',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

export default App;

I set up Auth0 in React Native (using Expo iOS app Xcode simulator). When "login" is invoked, it doesn't run "authorize()". It's not taking me to the Auth0 login page. For my callback URL in Auth0, I set it to localhost:19000 (which is the same as my expo-client).

import React from 'react';
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Button, Text, View } from 'react-native';
import {useAuth0, Auth0Provider} from 'react-native-auth0';

const Home = () => {
  const {authorize, clearSession, user} = useAuth0();

  const login = async () => {
    try {
      await authorize();
    } catch (err) {
      console.log(err);
    }
  }

  const onLogout = async () => {
    try {
      await clearSession();
    } catch (err) {
      console.log('log out canceled');
    }
  }

  return (
    <View style={styles.container}>
      {!user && <Button onPress={login} title="Log in" />}
      {user && <Text>Logged in as {user.name}</Text>}
    </View>
  )
}

const App = () => {
  return (
    <Auth0Provider domain={"ihavemydomainhere} clientId={"ihavemyclientidhere"}>
      <Home />
    </Auth0Provider>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#F5FCFF',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

export default App;
Share Improve this question edited Jan 11, 2023 at 23:03 Phil 165k25 gold badges262 silver badges267 bronze badges asked Jan 11, 2023 at 22:58 kirasam72kirasam72 1652 silver badges10 bronze badges 4
  • It seems impossible for login() to not run authorize(). What makes you think that is what's happening? – Phil Commented Jan 11, 2023 at 23:06
  • When I open my Expo Go App and press on “login”, it doesn’t do anything. – kirasam72 Commented Jan 11, 2023 at 23:24
  • Does it log anything at all? Perhaps you could add some debugging logs, eg console.log("Starting login") – Phil Commented Jan 11, 2023 at 23:25
  • Did you manage to fix it? I think running eas build and running the development build should work. I'm asking because when it works, even after authorize(), it redirects but { user } always just remains null – username-yj Commented Apr 10, 2023 at 11:21
Add a ment  | 

1 Answer 1

Reset to default 5

react-native-auth0 use native code which is not patible with Expo Go. That’s why is not doing anything. Try building a dev build, that should work.

Source: https://auth0./docs/quickstart/native/react-native-expo/interactive (the WARN message states “ This SDK is not patible with "Expo Go" app. It is patible only with Custom Dev Client and EAS builds.”)

Hope that helps

发布评论

评论列表(0)

  1. 暂无评论