React Native,Node js 身份验证

React Native, Node js authentication

对于节点 js + react-native 身份验证,我应该使用哪些最好的库? 我正在为 login/register 页面使用带有 ejs 模板的护照本地策略,因为我正在构建一个网络应用程序。 现在我正在开发移动应用程序,我不知道这将如何在移动应用程序上运行。 关于如何开始这个有什么想法吗?

提前致谢。

以下是我如何在我的 react-native 应用程序中使用节点 js 后端实现身份验证。我使用 jwt tokens, where I store the user's token inside the device using expo's secure storage library. Then following the recommended auth flow by react-native-navigation documentation,每当应用程序启动时,我都会检查令牌是否存在以及 return 相应的导航堆栈。参考下面的代码实现。

LoginScreen.js/RegisterScreen.js

//...

// Get token from your backend and save it in secure storage
const userToken = request.data.token
try {
    await SecureStore.setItemAsync("token", userToken);
  } catch (error) {
    console.warn(error);
 }
 
 // ...

App.js

export default function App() {
  const [isReady, setIsReady] = useState(false)
  const [userToken, setUserToken] = useState();
  
  // Fetch token and set state
  const restoreUser = async () => {
     const token = await SecureStore.getItemAsync("token")
     setUserToken(token)
  }
  
  if (!isReady)
    // Show splash screen while checking for userToken
    return (
      <AppLoading
        startAsync={restoreUser}
        onFinish={() => setIsReady(true)}
        onError={console.warn}
      />
    );
  
  return (
  <Stack.Navigator>
    {userToken == null ? (
      // No token found, user isn't signed in
      <Stack.Screen
        name="SignIn"
        component={SignInScreen}
        options={{
          title: 'Sign in',
          // When logging out, a pop animation feels intuitive
          // You can remove this if you want the default 'push' animation
          animationTypeForReplace: state.isSignout ? 'pop' : 'push',
        }}
      />
    ) : (
      // User is signed in
      <Stack.Screen name="Home" component={HomeScreen} />
    )}
  </Stack.Navigator>
);
  
}

在后端查看 jsonwebtoken npm 包以了解如何创建 jwt 令牌。祝你好运!