Giter Site home page Giter Site logo

mukhammadali / react-native-auth0-guardian Goto Github PK

View Code? Open in Web Editor NEW
9.0 2.0 5.0 383 KB

Auth0 Guardian React Native (iOS & Android)

License: MIT License

Ruby 2.46% Java 45.49% JavaScript 20.60% Objective-C 13.38% Swift 15.00% Starlark 3.07%
react-native auth0 react-native-auth0 react-native-auth0-guardian guardian auth0-guardian

react-native-auth0-guardian's Introduction

React Native Auth0 Guardian (iOS & Android)

npm version contributions welcome

Installation

NPM

npm install --save react-native-auth0-guardian

Yarn

yarn add react-native-auth0-guardian

Linking the library

React Native 0.60+: CLI autolink feature links the module while building the app. No any extra steps needed.

React Native <= 0.59: You need to link manually
$ react-native link react-native-auth0-guardian

Use CocoaPods to add the native RNAuth0Guardian to your project:
$ npx pod-install

Manual installation

Show Steps

iOS

  1. In XCode, in the project navigator, right click LibrariesAdd Files to [your project's name]
  2. Go to node_modulesreact-native-auth0-guardian and add RNAuth0Guardian.xcodeproj
  3. In XCode, in the project navigator, select your project. Add libRNAuth0Guardian.a to your project's Build PhasesLink Binary With Libraries
  4. Run your project (Cmd+R)<

Android

  1. Open up android/app/src/main/java/[...]/MainActivity.java
  • Add import com.reactlibrary.RNAuth0GuardianPackage; to the imports at the top of the file
  • Add new RNAuth0GuardianPackage() to the list returned by the getPackages() method
  1. Append the following lines to android/settings.gradle:
    include ':react-native-auth0-guardian'
    project(':react-native-auth0-guardian').projectDir = new File(rootProject.projectDir, 	'../node_modules/react-native-auth0-guardian/android')
    
  2. Insert the following lines inside the dependencies block in android/app/build.gradle:
      implementation project(':react-native-auth0-guardian')
    

Usage

Initialization

import Auth0Guardian from 'react-native-auth0-guardian';

// Before using `Auth0Guardian`, you should call `initialize` method 
// with `auth0Domain` as a parameter. Calling this in the root file is recommended Ex: root index.js or App.js

const auth0Domain = "yourtenant.guardian.auth0.com" // replace it with your own

Auth0Guardian.initialize(auth0Domain)
	.then(result => console.log('Auth0 Guardian initialized 😎'))
	.catch(err => console.log(err))

Enrolling the device

An enrollment is a link between the second factor and an Auth0 account. When an account is enrolled you'll need it to provide the second factor required to verify the identity.

For an enrollment you need the following things, besides your Guardian Domain:

enrollmentUri - this is the url parsed from qr code. You can use react-native-qrcode-scanner library to scan the qrcode and parse it.

deviceToken - unique device token (aka FCM token for android, APNS token for iOS). You can get it using react-native push notification libraries

try {
	const isEnrolled = await Auth0Guardian.enroll(enrollmentUri, deviceToken);
	console.log({ isEnrolled })
} catch (err)  {
	console.log(err)
}

Get TOTP code for current enrollment. (Returns only if the device is enrolled successfully)

You can also get TOTP code and show it inside your application if this use case is required. User will have 2 options: notification based authentication or totp based authentication (like Google Authenticator)

try {
	const totpCode = await Auth0Guardian.getTOTP();
	console.log({ totpCode })
} catch (err) {
	console.log(err)
}

Allow a login request.

Once you have the enrollment in place, you will receive a push notification every time the user has to validate his identity with MFA.

notificationData - body of notification received through push notification libraries (firebase, push-notification-ios or etc.)

try {
	const isAllowed = await Auth0Guardian.allow(notificationData);
	console.log({ isAllowed })
} catch (err) {
	console.log(err)
}

Reject a login request.

To deny an authentication request use the method below.

notificationData - body of notification received through push notification libraries (firebase, push-notification-ios or etc.)

try {
	const isRejected = await Auth0Guardian.reject(notificationData);
	console.log({ isRejected })
} catch (err) {
	console.log(err)
}

Removing device enrollment

If you want to delete an enrollment -for example if you want to disable MFA

try {
	const isUnenrolled = await Auth0Guardian.unenroll();
	console.log({ isUnenrolled })
} catch (err) {
	console.log(err)
}

Native libraries used:

Auth0 Guardian Java
Auth0 Guardian Swift

react-native-auth0-guardian's People

Contributors

dependabot[bot] avatar mukhammadali avatar mukhammadali7 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.