FormidableLabs / react-native-app-auth

React native bridge for AppAuth - an SDK for communicating with OAuth2 providers
https://commerce.nearform.com/open-source/react-native-app-auth
MIT License
2k stars 438 forks source link

0.72 React Native Guide #867

Closed ishan-flek-ai closed 1 year ago

ishan-flek-ai commented 1 year ago

Documentation Feedback

Guide for 0.72 React Native - Sample google auth

Adding google auth

https://github.com/FormidableLabs/react-native-app-auth/blob/main/docs/config-examples/google.md

const config = {
issuer: 'https://accounts.google.com',
clientId:
'[key].apps.googleusercontent.com',
redirectUrl:
'com.googleusercontent.apps.[key]:/oauth2redirect/google',
scopes: ['openid', 'profile'],
};

const result = await authorize(config);

https://github.com/FormidableLabs/react-native-app-auth https://github.com/FormidableLabs/react-native-app-auth/issues/801

AppDelegate.h

#import <RCTAppDelegate.h>
#import <UIKit/UIKit.h>
+ #import <React/RCTLinkingManager.h>
+ #import "RNAppAuthAuthorizationFlowManager.h"

+ @interface AppDelegate : RCTAppDelegate <RNAppAuthAuthorizationFlowManager>
+ @property(nonatomic, weak) id<RNAppAuthAuthorizationFlowManagerDelegate> authorizationFlowManagerDelegate;

@end

https://github.com/FormidableLabs/react-native-app-auth/issues/453 What to do if provider requires redirectUrl to be https?

AppDelegate.mm

#import "AppDelegate.h"

#import <React/RCTBundleURLProvider.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  self.moduleName = @"ember";
  // You can add your custom initial props in the dictionary below.
  // They will be passed down to the ViewController used by React Native.
  self.initialProps = @{};

  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

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

- (BOOL) application: (UIApplication *) application
continueUserActivity: (nonnull NSUserActivity *)userActivity
  restorationHandler: (nonnull void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler
{
  if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
    if (self.authorizationFlowManagerDelegate) {
      BOOL resumableAuth = [self.authorizationFlowManagerDelegate resumeExternalUserAgentFlowWithURL:userActivity.webpageURL];
      if (resumableAuth) {
        return YES;
      }
    }
  }
  return [RCTLinkingManager application:application continueUserActivity:userActivity restorationHandler:restorationHandler];
}

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
  return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
#else
  return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}

@end
ishan-flek-ai commented 1 year ago

Everyone is having the google redirect issue,

Android

    namespace "com.ember"
    defaultConfig {
        applicationId "com.ember"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
        manifestPlaceholders = [
            appAuthRedirectScheme: 'com.googleusercontent.apps.[id]',
        ]
    }

appAuthRedirectScheme has to be google URL to be secure that is the issue everyone is facing. that is because they are using invalid appAuthRedirectScheme, it is not com.[packageName] but what google has given

rcidt commented 1 year ago

@ishan-flek-ai should the appAuthRedirectScheme literally be "com.googleusercontent.apps.[id]", or should [id] be some arbitrary string?

ishan-flek-ai commented 1 year ago

@rcidt that is your oauth client id that you created on google cloud console