diff --git a/articles/quickstart/native/react-native-beta/00-login.md b/articles/quickstart/native/react-native-beta/00-login.md new file mode 100644 index 0000000000..7b05b4afaa --- /dev/null +++ b/articles/quickstart/native/react-native-beta/00-login.md @@ -0,0 +1,250 @@ +--- +title: Login +description: This tutorial demonstrates how to add user login to a React Native application using Auth0. +budicon: 448 +topics: + - quickstarts + - native + - react-native +github: + path: 00-Login-Hooks +contentType: tutorial +useCase: quickstart +--- + + + +This Quickstart is for the React Native framework. To integrate Auth0 into your Expo application, please refer to the [Expo Quickstart](https://auth0.com/docs/quickstart/native/react-native-expo/interactive) + +<%= include('../_includes/_getting_started', { library: 'React Native'}) %> + +## Install Dependencies + +In this section, you will install the React Native Auth0 module. + +::: note +Please refer to the [official documentation](https://facebook.github.io/react-native/) for additional details on React Native. +::: + +### Yarn + +```bash +yarn add react-native-auth0 +``` + +::: note +For further reference on yarn, check [their official documentation](https://yarnpkg.com/en/docs). +::: + +### npm + +```bash +npm install react-native-auth0 --save +``` + +### Additional iOS step: install the module Pod + +CocoaPods is the iOS package management tool the React Native framework uses to install itself into your project. For the iOS native module to work with your iOS app, first install the library Pod. If you're familiar with older React Native SDK versions, this is similar to the previous _linking a native module_. The process is now simplified: + +Change directory into the `ios` folder and run `pod install`. + +```bash +cd ios +pod install +``` + +First, you must provide a way for your users to log in. We recommend using the Auth0 hosted [login page](/hosted-pages/login). + +
Universal Login
+ +## Integrate Auth0 in Your Application + +### Configure Android + +Open your app's `build.gradle` file (typically at `android/app/build.gradle`) and add the following manifest placeholders. The value for `auth0Domain` should contain your Auth0 application settings [as configured above](#get-your-application-keys). + +```groovy +android { + defaultConfig { + // Add the next line + manifestPlaceholders = [auth0Domain: "${account.namespace}", auth0Scheme: "<%= "${applicationId}" %>"] + } + ... +} +``` + +::: note +At runtime, the `applicationId` value will automatically update with your application's package name or ID (e.g. `com.example.app`). You can change this value from the `build.gradle` file. You can also check it at the top of your `AndroidManifest.xml` file. +::: + +### Configure iOS + +In the file `ios//AppDelegate.mm` add the following: + +```objc +#import + +- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url + options:(NSDictionary *)options +{ + return [RCTLinkingManager application:app openURL:url options:options]; +} +``` + +::: note +This file will be `ios//AppDelegate.m` on applications using the [old architecture](https://reactnative.dev/docs/next/new-architecture-app-intro#ios---use-objective-c-mm-extension). +::: + +Next, add a URLScheme using your App's bundle identifier. + +In the `ios` folder, open the `Info.plist` and locate the value for `CFBundleIdentifier` + +```xml +CFBundleIdentifier +$(PRODUCT_BUNDLE_IDENTIFIER) +``` + +Below this value, register a URL type entry using the value of `CFBundleIdentifier` as the value for the `CFBundleURLSchemes`. + +```xml +CFBundleURLTypes + + + CFBundleTypeRole + None + CFBundleURLName + auth0 + CFBundleURLSchemes + + $(PRODUCT_BUNDLE_IDENTIFIER) + + + +``` + +::: note +If your application was generated using the React Native CLI, the default value of `$(PRODUCT_BUNDLE_IDENTIFIER)` dynamically matches `org.reactjs.native.example.$(PRODUCT_NAME:rfc1034identifier)`. For the sample app, this value matches `com.auth0samples`. +::: + +Note this value as you'll be using it to define the callback URLs below. If desired, you can change it using XCode in the following way: + +- Open the `ios/.xcodeproj` file or run `xed ios` on a Terminal from the app root. +- Open your project's or desired target's Build Settings tab and find the section that contains "Bundle Identifier". +- Replace the "Bundle Identifier" value with your desired application's bundle identifier name. + +For additional information please read [react native docs](https://facebook.github.io/react-native/docs/linking). + + +### Configure Callback and Logout URLs + +The callback and logout URLs are the URLs that Auth0 invokes to redirect back to your application. Auth0 invokes the callback URL after authenticating the user, and the logout URL after removing the session cookie. + +If the callback and logout URLs are not set, users will be unable to log in and out of the application and will get an error. + +Go to the settings page of your [Auth0 application](${manage_url}/#/applications/${account.clientId}/settings) and add the corresponding URL to **Allowed Callback URLs** and **Allowed Logout URLs**, according to the platform of your application. If you are using a [custom domain](/customize/custom-domains), use the value of your custom domain instead of the Auth0 domain from the settings page. + +#### iOS +```text +BUNDLE_IDENTIFIER.auth0://${account.namespace}/ios/BUNDLE_IDENTIFIER/callback +``` +#### Android +```text +PACKAGE_NAME.auth0://${account.namespace}/android/PACKAGE_NAME/callback +``` + +::: note +If you are following along with our sample project, set this +- for iOS - `com.auth0samples.auth0://${account.namespace}/ios/com.auth0samples/callback` +- for Android - `com.auth0samples.auth0://${account.namespace}/android/com.auth0samples/callback` +::: + +## Add login to your app + +Import the `useAuth0` hook and the `Auth0Provider` component from the `react-native-auth0` package. + +```js +import {useAuth0, Auth0Provider} from 'react-native-auth0'; +``` + +Next, wrap your application in the `Auth0Provider` component, providing your Auth0 domain and Client ID values: + +```js +const App = () => { + return ( + + {/* your application */} + + ); +}; +``` + +Finally, present the hosted login screen using the `authorize` method from the `useAuth0` hook. See this usage example showing logging in on a button click: + +```js +const LoginButton = () => { + const {authorize} = useAuth0(); + + const onPress = async () => { + try { + await authorize(); + } catch (e) { + console.log(e); + } + }; + + return