Post image
Strategy

Is there a way via css (either grid, flex, bootstrap or any …


I want to place images like this, 1 big one and the following stacked 2 per row but I can’t figure out how to do it short of using a table but I want to try to keep it as responsive as possible. Would anybody know?

From: https://www.airbnb.com/rooms/36732625?source_impression_id=p3_1578949259_l7PPYIxiK0NlzvQz

**sorry if this isn’t the sub, seemed ok to me but if it’s not please let me know and point me in the right direction if possible please!

Post image



Source link

Add a Login Feature to a React Native Application
Strategy

Add a Login Feature to a React Native Application


React Native is an open-source mobile application framework for developing native Android and iOS apps. React components translate to native platform components, as opposed to using JavaScript/HTML and a web view. making React Native is ready to use in your current iOS and Android apps. 

In this tutorial, I’ll show you how to use React Native with Okta to implement a login feature in an app within ten minutes. For authorization, we’ll use OAuth 2.0, and for authentication, we’ll use OpenID Connect. Authentication allows us to verify who the user is, and authorization gives the user access to what they’re allowed to do. Okta makes authentication implementation super easy and even more so with React Native and Okta’s React Native SDK.

React Native 0.61 was just released a couple of weeks ago. One of its biggest features is Fast Refresh — a unification of live reloading (reload on save) and hot reloading. Fast Refreshfully supports modern React’s functional components and hooks and recovers after typos and other mistakes. In previous versions of React Native, a common complaint was that “hot reloading” was broken.

You may also like:
Get the Low Down on All Things React and React Native.

Prerequisites:

To install these prerequisites on a Mac, Linux, or Windows Subsystem for Linux (WSL), I recommend using Homebrew.

If you’re not using WSL for Windows, you can use Chocolatey to install everything from the command line:

You’ll also need to download and install IDEs for Android and iOS:

If you’d rather watch a video, I created a screencast of this tutorial.

Create a React Native Application

The React Native CLI is a popular way to get started with React Native development.

Once you have React Native CLI installed, you can create a new application using the init command.

Add Login With OIDC

Okta provides a React Native SDK, which conveniently wraps the Okta native Android OIDC and iOS OIDC libraries.

I’m going to show you two ways to add OIDC-based login with Okta: The fast way with a tool I created and the step-by-step instructions.

This tool is based on Schematics and manipulates your project to install and configure everything.

Install Schematics globally.

Create a Native App in Okta

Log in to your Okta Developer account (or sign up if you don’t have an account).

  • From the Applications page, choose Add Application.
  • On the Create New Application page, select Native as the platform, and click Next.
  • Give your app a memorable name, select Refresh Token as a grant type, and click Done.
  • Click the Edit button and add a Logout redirect URI that matches the default Login redirect URI (e.g., com.okta.dev-123456:/callback).
  • Click Save.

Configuring settings

Configuring settings

In a terminal, navigate into your ReactNativeLogin directory and install OktaDev Schematics:

Note: If you have a React Native 0.60.x app, use @oktadev/schematics@0.9.0. The only difference between the two is the tests.

Run the add-auth schematic in your ReactNativeLogin project.

You will be prompted for an issuer and a clientId. You can find your issuer under API > Authorization Servers on Okta.

The client ID will be on your application screen.

Grabbing Client ID

Grabbing Client ID

This process will take a minute to complete.

Configuring settings

Configuring settings

Configure Your iOS Project to Use Swift

React Native uses Objective-C, but the Okta React Native library uses Swift. Because of this, you have to add a Swift file in your iOS project for it to compile. Run the following command to open your native iOS project in Xcode.

To add a Swift file, complete the following steps:

  1. Right-click on your project and select New File….
  2. Select Swift File, and click Next.
  3. Enter a name (e.g., Polyfill) and click Create.
  4. If prompted for a header file, it is not required to create one.

Then, cd into ReactNativeLogin/ios and run pod install.

Tip: If you don’t have CocoaPoads installed, you can install it with gem install cocoapods.

Run Your React Native App on iOS

Navigate back to the root directory of your app. Start your app, and you should be able to authenticate with Okta. 

Login workflow

Login workflow

Once you’re signed in, you’ll see options to log out, get the user’s information from an ID token, and get the user’s information from the React Native SDK’s  getUser() method (a.k.a. the request).

Login workflow

Login workflow

Note: The prompt when you click Login cannot be avoided. This is an iOS safety mechanism. It also pops up when you log out. See this issue for more information.

Run Your React Native App on Android

The schematic you ran modifies all the necessary files for Android; there are no code modifications required!

You will need to run an AVD (Android Virtual Device) before starting your app, or you can plug in your Android phone and use that. If you have neither, launch Android Studio and go to Tools > AVD Manager. Click Create Virtual Device at the bottom and select a phone of your choice. I chose a Pixel 3 XL with Android 10.

Running AVD

Running AVD

Start your AVD, then your app, and authenticate with Okta. 

Logging in

Logging in

Click the Get User from ID Token button to confirm you can retrieve the user’s information.

Logged in

Logged in

Run Installed React Native Authentication Tests

In addition to integrating login, OktaDev Schematics also installed some tests that verify login and authentication work. Run npm test to see these tests run in your terminal.

Note: OktaDev Schematics puts tests in a tests directory rather than the default __tests__ directory because Angular Schematics uses double underscore as a placeholder.

Using a Custom Login Screen with Okta

This example showed you how to add an OIDC flow that opens a browser when a user logs in and logs out. If you require a smoother login experience that doesn’t pop open a browser, see Okta’s Custom Sign-In Example, for example, code that shows you how to implement that type of flow.

Add OIDC Login the Hard Way

The previous section showed you how to use OktaDev Schematics to quickly add a login feature (and tests!) to a React Native application. However, you might have an existing React Native application that doesn’t have the same structure as a brand new React Native application.

This section shows you everything that OktaDev Schematics does for you, in detail.

Create a project with React Native CLI and install Okta’s SDK.

For iOS, modify ios/Podfile to change it from iOS 9 to iOS 11.

Open your project in Xcode.

Add a Swift file.

  1. Right-click on your project and select New File….
  2. Select Swift File, and click Next.
  3. Enter a name (e.g., Polyfill) and click Create.
  4. If prompted for a header file, it is not required to create one.

Install iOS native dependencies with CocoaPods.

Add Jest and Enzyme to Test Your React Native Login

Jest is a library for testing JavaScript apps, and Enzyme is a library that makes it easier to select and query elements in tests. They’re often used alongside one another.

Install testing dependencies with npm.

Then, change your jest key in package.json to match the following:

Create setupJest.js to polyfill React Native for Okta.

Create Auth.js to handle your authentication code.

You might notice it imports a config file at the top.

Create auth.config with your OIDC settings from Okta.

Create an app on Okta to get the values for $yourClientId and ######.

  • From the Applications page, choose Add Application.
  • On the Create New Application page, select Native as the platform, and click Next.
  • Give your app a memorable name, select Refresh Token as a grant type, and click Done.
  • Click the Edit button and add a Logout redirect URI that matches the default Login redirect URI (e.g., com.okta.dev-123456:/callback).
  • Click Save.

In App.js, import Auth.

Use it in a new <View /> after the Hermes logic.

At this point, your tests will not pass because Okta uses an EventEmitter to communicate between components.

Add React Native Authentication Tests

To mock the native event emitter that Okta uses, add a mock for it in __tests__/App-test.js.

To make sure all the login and authentication logic works, create __tests__/Auth-test.js.

Run npm test to bask in the fruits of your labor!

To run your app on iOS, use react-native run-ios.

Login workflow

Login workflow

To run it on Android, you’ll need to modify your Gradle build files.

Okta’s React Native SDK depends on the Okta OIDC Android library. You have to add this library through Gradle.

  1. Add Okta’s BinTray repo to android/build.gradle, under allprojects -> repositories.
  2. Make sure your minSdkVersion is 19 in android/build.gradle.
  3. Define a redirect scheme to capture the authorization redirect. In android/app/build.gradle, under android -> defaultConfig, add:

Finally, start a virtual device (or plug in your phone), and run react-native run-android.

Final output

Final output

Learn More About React Native and OIDC Login

This tutorial showed you how to add a login feature to a React Native application. You learned that OAuth 2.0 is an authorization protocol, and OIDC is an authentication layer on top of it. You also used PKCE (Public Key Code Exchange) in your implementation, which is the more secure way to implement OAuth 2.0 in mobile applications.

I hope you enjoy your React Native development journey and its fast refresh feature!

You can find the source code for this example on GitHub at oktadeveloper/okta-react-native-login-example.

To learn more about React Native, OIDC, and PKCE, check out these posts:

If you liked this tutorial, follow @oktadev on Twitter and subscribe to our YouTube channel.

Further Reading





Source link