by Melih Yumak

通过Melih Yumak

如何使用react-native-app-auth设置React Native身份验证 (How to set up React Native authentication with react-native-app-auth)

版本号 (Versions)

Before we start, make sure you have the following versions installed:

在开始之前,请确保已安装以下版本:

“react”: “16.8.3”,“react-native”: “0.59.1”,“react-native-contacts”: “3.1.5”,

“React”:“ 16.8.3”,“React本机”:“ 0.59.1”,“React本机接触”:“ 3.1.5”,

Here’s the link to the Github repo if you want to check it out: https://github.com/FormidableLabs/react-native-app-auth

如果您想检出Github仓库,请点击以下链接 : https : //github.com/FormidableLabs/react-native-app-auth

React-native-app-auth is used to provide authentication in your react-native applications. In my case, I was trying to use it with Google, so here is an explanation how you can install and use it for the versions above.

React-native-app-auth用于在您的react-native应用程序中提供身份验证。 就我而言,我正尝试在Google上使用它,因此这里说明了如何在上述版本中安装和使用它。

In their documentation it’s also explained as a React Native bridge for AppAuth-iOS and AppAuth-Android SDKS for communicating with OAuth 2.0 and OpenID Connect providers.

在他们的文档中,它还被解释为用于AppAuth-iOS和AppAuth-Android SDKS的React Native桥,用于与OAuth 2.0和OpenID Connect提供程序进行通信。

经过测试的OpenID提供程序: (Tested OpenID providers:)

These providers are OpenID compliant, which means you can use autodiscovery:

这些提供程序符合OpenID,这意味着您可以使用自动发现 :

  • Identity Server4 (Example configuration)

    Identity Server4 ( 示例配置 )

  • Identity Server3 (Example configuration)

    Identity Server3 ( 示例配置 )

  • Google (Example configuration)

    Google ( 示例配置 )

  • Okta (Example configuration)

    Okta ( 示例配置 )

  • Keycloak (Example configuration)

    Keycloak ( 示例配置 )

  • Azure Active Directory (Example configuration)

    Azure Active Directory ( 示例配置 )

  • AWS Cognito (Example configuration)

    AWS Cognito ( 示例配置 )

经过测试的OAuth2提供程序: (Tested OAuth2 providers:)

These providers implement the OAuth2 spec, but are not OpenID providers, which means you must configure the authorization and token endpoints yourself.

这些提供程序实现OAuth2规范,但不是OpenID提供程序,这意味着您必须自己配置授权和令牌终结点。

  • Uber (Example configuration)

    Uber ( 示例配置 )

  • Fitbit (Example configuration)

    Fitbit ( 示例配置 )

  • Dropbox (Example configuration)

    Dropbox ( 示例配置 )

  • Reddit (Example configuration)

    Reddit ( 示例配置 )

安装 (Installation)

npm install react-native-app-auth --savereact-native link react-native-app-auth

IOSIn the documentation, there are three ways to implement this state but I prefer CocoaPods.

IOS在文档中,有三种方法可以实现此状态,但我更喜欢CocoaPods。

If you are using CocoaPods for the first time, please complete the steps below:

如果您是第一次使用CocoaPods,请完成以下步骤:

sudo gem install cocoapods

From your root folder open

从您的根文件夹中打开

cd ios
pod init

The pod init command will initialise the Podfile in your iOS directory.

pod init命令将在您的iOS目录中初始化Podfile。

Then add this line below in your Podfile after target ‘your_app’ do

然后,在目标“ your_app”执行完之后,在您的Podfile中添加以下行

pod 'AppAuth', '0.95.0'

注册重定向URL方案 (Register redirect URL scheme)

If you intend to support iOS 10 and older, you need to define the supported redirect URL schemes in your Info.plist as follows:

如果打算支持iOS 10及更早版本,则需要在Info.plist定义支持的重定向URL方案,如下所示:

Note: you will get these values from oauth provider. For google: https://console.developers.google.com/

注意:您将从oauth provider获得这些值。 对于Google: https : //console.developers.google.com/

<key>CFBundleURLTypes</key><array>  <dict>    <key>CFBundleURLName</key>    <string>com.your.app.identifier</string>    <key>CFBundleURLSchemes</key>    <array>      <string>io.identityserver.demo</string>    </array>  </dict></array>
  • CFBundleURLName is any globally unique string. A common practice is to use your app identifier.

    CFBundleURLName是任何全局唯一的字符串。 一种常见的做法是使用您的应用标识符。

  • CFBundleURLSchemes is an array of URL schemes your app needs to handle. The scheme is the beginning of your OAuth Redirect URL, up to the scheme separator (:) character.

    CFBundleURLSchemes是您的应用程序需要处理的一系列URL方案。 该方案是您的OAuth重定向URL的开始,一直到计划分隔符( : )字符。

在AppDelegate中定义openURL回调 (Define openURL callback in AppDelegate)

You need to retain the auth session in order to continue the authorization flow from the redirect. Follow these steps:

您需要保留身份验证会话,以便继续进行重定向中的授权流程。 跟着这些步骤:

Make AppDelegate conform to RNAppAuthAuthorizationFlowManager with the following changes to AppDelegate.h:

通过对AppDelegate.h进行以下更改,使AppDelegate符合RNAppAuthAuthorizationFlowManager

+ #import "RNAppAuthAuthorizationFlowManager.h"
- @interface AppDelegate : UIResponder <UIApplicationDelegate>+ @interface AppDelegate : UIResponder <UIApplicationDelegate, RNAppAuthAuthorizationFlowManager>
+ @property(nonatomic, weak)id<RNAppAuthAuthorizationFlowManagerDelegate>authorizationFlowManagerDelegate;

Change the following method from UIApplicationDelegate in AppDelegate.m:

AppDelegate.m UIApplicationDelegate更改以下方法:

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *, id> *)options { return [self.authorizationFlowManagerDelegate resumeExternalUserAgentFlowWithURL:url];}

Android

安卓

After successful linking you should add android/app/build.grandle file defaultConfig value as your identifiers redirect url.

成功链接后,您应该添加android / app / build.grandle文件defaultConfig值作为您的标识符重定向网址。

manifestPlaceholders = [
appAuthRedirectScheme: “io.identityserver.demo”
]

用法 (Usage)

import { authorize } from 'react-native-app-auth';
// base configconst config = {  issuer: '<YOUR_ISSUER_URL>',  clientId: '<YOUR_CLIENT_ID>',  redirectUrl: '<YOUR_REDIRECT_URL>',  scopes: ['<YOUR_SCOPES_ARRAY>'],};
// use the client to make the auth request and receive the authStatetry {  const result = await authorize(config);  // result includes accessToken, accessTokenExpirationDate and refreshToken} catch (error) {  console.log(error);}

Happy Coding!

编码愉快!

Thank you for reading this far. If you enjoyed this post, please share, comment, and press that ? a few times (up to 50 times). . . Maybe it will help someone.

感谢您阅读本文。 如果您喜欢这篇文章,请分享,评论并按此? 几次(最多50次)。 也许会帮助某人。

Follow me on Medium or Github if you’re interested in more in-depth and informative write-ups like these in the future. ?

如果您将来对此类更深入,内容更丰富的文章感兴趣,请在Medium或Github上关注我。

翻译自: https://www.freecodecamp.org/news/how-to-set-up-react-native-authentication-with-react-native-app-auth-f6fd66e0e6d0/

如何使用react-native-app-auth设置React Native身份验证相关推荐

  1. 利用 Create React Native App 快速创建 React Native 应用

    React Native App简介 打开React Native官方文档你会发现,在Getting Started章节下新添加一个Quick Start Tab页.Quick Start是在v0.4 ...

  2. 如何使用Google Authenticator在ASP.NET Core中设置两因素身份验证

    介绍 (Introduction) In this article, we are going to learn how to perform two-factor authentication in ...

  3. 如何在Raspberry Pi上设置两因素身份验证

    Kiklas/ShutterstockKiklas /快门 The Raspberry Pi is everywhere now, which is why it's caught the eye o ...

  4. aws mfa 认证_如何为您的AWS账户设置多因素身份验证(MFA)

    aws mfa 认证 第1步 : 转到AWS控制台并使用您的用户名密码登录. 第2步 : 转到服务-> IAM 第三步: 单击您的根帐户上的激活MFA 第4步 : 在步骤3中,点击屏幕上的管理M ...

  5. HttpWebRequest(未授权设置) 即需要身份验证设置

    1.第一步:开启IIS基本身份验证 2.第二步:如下图设置(ZT158代表域名),如果本机账号,则直接是机器器即可 ybcxj是账号,aa00000是密码

  6. 在设置iis windows身份验证,出错:登录失败:用户帐户限制。可能的原因包括不允许空密码登录时间限制或强制的策略限制。

    方法一:在开始-运行-输入gpedit.msc,打开组策略,然后计算机配置-windows设置-本地策略-安全选项,在右边的倒数第三个选项就可以看到 帐户: 使用空白密码的本地帐户只允许进行控制台登录 ...

  7. 如何为您的AWS账户设置多因素身份验证(MFA)

    步骤1 : 转到AWS控制台并使用您的用户名密码登录. 第2步 : 转到服务-> IAM 第三步: 单击您的根帐户上的激活MFA 第四步 : 在步骤3中,点击屏幕上的管理MFA按钮. 步骤5: ...

  8. Web App与Native App

    结合网上相关资料,以及自己项目中的经验,收集汇总了iOS Webapp相关的开发知识,如下. WebApp是一种新出现的基于WEB形式的类应用程序,运行在高端的移动终端设备上,其应用范围会越来越广. ...

  9. 详解WebApp与Native App的区别

    一篇真的很棒关于html5的Web App与Native App的技术分析 ! 自Iphone和Android这两个牛逼的手机操作系统发布以来,在互联网界从此就多了一个新的名词-WebApp(意为基于 ...

  10. 波卡的验证人节点_轻松节点身份验证:设置和本地

    波卡的验证人节点 here. 在这里. Edit 11/18/2017: Updated to reflect Facebook API changes. Updating dependencies ...

最新文章

  1. 华为交换机SSH登录失败原因
  2. HDU2222 Keywords Search(AC自动机模板)
  3. Spring依赖注入:注解注入总结
  4. 补码到底是个什么东西
  5. 【转载保存】mapreduce优秀文章
  6. python制作图片墙_利用python生成照片墙的示例代码
  7. sublime基本命令和使用
  8. java人员工作建议_给JAVA设计开发新手的一些建议和意见(1)
  9. matlab 中的textscan
  10. android怎实现拼图功能,基于Android的趣味拼图的实现
  11. scratch实现秋天的画
  12. 国际贸易术语_Incoterm
  13. V831学习日记之串口通信
  14. 什么是库存周转率周转天数?
  15. 修改计算机用户名bat,修改计算机名.bat
  16. php外语文献有哪些,外语论文参考文献
  17. MYSQL中概念模型的基本概念_数据库基本概念
  18. JS判断选择的时间是否大于当前时间
  19. Ceph 线程池与工作队列
  20. 郭敬明唯美悲伤的短句分享

热门文章

  1. 计算机小学期实践报告,小学期实践报告
  2. 记录ide中tomcat部署的位置
  3. 那些年,追过的开源软件和技术
  4. c语言片段,C语言经典程序片段.docx
  5. ue4 android 贴图,【腾讯GAD】做一张用于UE4实时渲染的写实级别面部贴图
  6. 矩阵求逆引理(Matrix inversion lemma)推导
  7. 微信小程序头脑王者辅助神器
  8. 浙大计算机应用基础作业1,2016浙大远程教育计算机应用基础作业
  9. 项目管理小故事之小矮人的故事
  10. 项目管理故事(收集)