Skip to content
This repository has been archived by the owner on Jul 18, 2020. It is now read-only.

Commit

Permalink
Merge pull request #142 from auth0/add-browser-support-for-auth
Browse files Browse the repository at this point in the history
Add browser support for Web Auth
  • Loading branch information
hzalaz authored Apr 9, 2017
2 parents 24990c6 + 4ad520d commit d862cc2
Show file tree
Hide file tree
Showing 44 changed files with 4,388 additions and 1,130 deletions.
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,74 @@ And you'll see our native login screen

[![Lock.png](https://cdn.auth0.com/mobile-sdk-lock/lock-ios-default.png)](https://auth0.com)

### Avoid WebView for Social Auth

In order to make Lock use the OS browser to perform Web Auth you will need to pass the attribute `useBrowser` when you build Lock like

```js
var lock = new Auth0Lock({clientId: "YOUR_CLIENT_ID", domain: "YOUR_DOMAIN", useBrowser: true});
```

Then you need to configure both your iOS and Android project following the instructions detailed below.

> **IMPORTANT**: This feature only works with iOS 9 or later.
### iOS

In the file `AppDelegate.m` add the following import on the top (next to the other imports of the file)

```objc
#import "A0LockReact.h"
```

and then inside the `AppDelegate` implementation add the following method

```objc
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
return [[A0LockReact sharedInstance] handleURL:url sourceApplication:nil];
}
```

And finally in the file `Info.plist` add the following entry

```xml
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>auth0</string>
<key>CFBundleURLSchemes</key>
<array>
<string>{Bundle Identifier}</string>
</array>
</dict>
</array>
```

Where the Bundle identifier can be found in the same file under the key `CFBundleIdentifier` like

```xml
<key>CFBundleIdentifier</key>
<string>org.reactjs.native.example.$(PRODUCT_NAME:rfc1034identifier)</string>
```

### Android

In the file `AndroidManifest` find where the `LockActivity` is declared and insde the `activity` tags add the following

```xml
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="a0{your lowercase auth0 client id}" android:host="{your auth0 domain}"/>
</intent-filter>
```

And make sure Lock's activity launch mode is `singleTask`

### TouchID (iOS Only)

```js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void init(ReadableMap options) {

Telemetry telemetry = new Telemetry("lock.react-native.android", initOptions.getLibraryVersion(), com.auth0.lock.BuildConfig.VERSION_NAME, null);
lockBuilder = new Lock.Builder()
.useWebView(true)
.useWebView(!initOptions.useBrowser())
.telemetry(telemetry)
.clientId(initOptions.getClientId())
.domainUrl(initOptions.getDomain())
Expand Down
13 changes: 13 additions & 0 deletions android/src/main/java/com/auth0/lock/react/bridge/InitOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,23 @@
import android.support.annotation.Nullable;

import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableArray;
import java.util.List;
import java.util.ArrayList;

public class InitOptions {

private static final String CLIENT_ID_KEY = "clientId";
private static final String DOMAIN_KEY = "domain";
private static final String CONFIGURATION_DOMAIN_KEY = "configurationDomain";
private static final String LIBRARY_VERSION_KEY = "libraryVersion";
private static final String USE_BROWSER = "useBrowser";

private String clientId;
private String domain;
private String configurationDomain;
private String libraryVersion;
private boolean useBrowser;

public InitOptions(@Nullable ReadableMap options) {
if (options == null) {
Expand All @@ -58,6 +63,10 @@ public InitOptions(@Nullable ReadableMap options) {
this.configurationDomain = options.getString(CONFIGURATION_DOMAIN_KEY);
}

if (options.hasKey(USE_BROWSER)) {
this.useBrowser = options.getBoolean(USE_BROWSER);
}

this.libraryVersion = options.hasKey(LIBRARY_VERSION_KEY) ? options.getString(LIBRARY_VERSION_KEY) : "0.0.0";
}

Expand All @@ -76,4 +85,8 @@ public String getConfigurationDomain() {
public String getLibraryVersion() {
return libraryVersion;
}

public boolean useBrowser() {
return useBrowser;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.auth0.lock.react.BuildConfig;
import com.facebook.react.bridge.SimpleMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.SimpleArray;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -57,11 +58,13 @@ public void testAll() throws Exception {
options.putString("clientId", "client-id-value");
options.putString("domain", "domain-value");
options.putString("configurationDomain", "configuration-domain-value");
options.putBoolean("useBrowser", true);

InitOptions initOptions = new InitOptions(options);
assertThat(initOptions.getClientId(), is(equalTo("client-id-value")));
assertThat(initOptions.getConfigurationDomain(), is(equalTo("configuration-domain-value")));
assertThat(initOptions.getDomain(), is(equalTo("domain-value")));
assertThat(initOptions.useBrowser(), is(true));
}

@Test
Expand Down
3 changes: 2 additions & 1 deletion auth0-lock.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class Auth0Lock {
domain: domain,
style: style,
configurationDomain: options.configurationDomain,
libraryVersion: VERSION
libraryVersion: VERSION,
useBrowser: options.useBrowser
};
this.nativeIntegrations = options.integrations;
this.auth0 = new Auth0(domain);
Expand Down
11 changes: 7 additions & 4 deletions bin/cocoapods.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ if [ -f $podfile ]
echo "Add the following pods":
echo ""
echo ""
cat $template
echo "pod 'Lock', '~> 1.29'"
echo "pod 'Lock/TouchID'"
echo "pod 'Lock/SMS'"
echo "pod 'Lock/Email'"
echo "pod 'Lock/Safari'"
echo ""
echo ""
echo "and run 'pod install' to install Lock for iOS"
Expand All @@ -42,16 +46,15 @@ cat >ios/Podfile <<EOL
target '${project_name}' do
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!
# Pods for tonypoc
end
# Auth0 Lock
pod 'Lock', '~> 1.26'
pod 'Lock', '~> 1.29'
pod 'Lock/TouchID'
pod 'Lock/SMS'
pod 'Lock/Email'
pod 'Lock/Safari'
EOL

echo "Installing Pods"
Expand Down
2 changes: 1 addition & 1 deletion ios/A0RNLock/A0LockReactModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// THE SOFTWARE.

#import <Foundation/Foundation.h>
#import "RCTBridgeModule.h"
#import <React/RCTBridgeModule.h>

@interface A0LockReactModule : NSObject<RCTBridgeModule>

Expand Down
11 changes: 10 additions & 1 deletion ios/A0RNLock/A0LockReactModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#import "A0LockReact.h"
#import "A0ThemeReact.h"
#import <Lock/Lock.h>
#import <Lock/A0SafariAuthenticator.h>

#if __has_include(<Lock-Facebook/A0FacebookAuthenticator.h>)
#define FACEBOOK_ENABLED 1
Expand All @@ -48,7 +49,15 @@ @implementation A0LockReactModule
NSString *clientId = values[@"clientId"];
NSString *domain = values[@"domain"];
if (clientId && domain) {
[[A0LockReact sharedInstance] configureLockWithClientId:clientId domain:domain version:values[@"libraryVersion"]];
A0LockReact *lockReact = [A0LockReact sharedInstance];
[lockReact configureLockWithClientId:clientId domain:domain version:values[@"libraryVersion"]];
BOOL useBrowser = [values[@"useBrowser"] boolValue];
if (useBrowser) {
A0Lock *lock = [lockReact lock];
[[lock identityProviderAuthenticator] registerDefaultAuthenticationProvider:^A0BaseAuthenticator * _Nonnull(A0Lock * _Nonnull lock, NSString * _Nonnull connectionName) {
return [[A0SafariAuthenticator alloc] initWithLock:lock connectionName:connectionName useUniversalLink:NO];
}];
}
}
NSDictionary *style = values[@"style"];
if (style) {
Expand Down
2 changes: 2 additions & 0 deletions ios/A0RNLock/Core/A0LockReact.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ typedef void(^A0LockCallback)(NSArray *parameters);

- (void)configureLockWithClientId:(NSString *)clientId domain:(NSString *)domain version:(NSString *)version;

- (BOOL)handleURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication;

+ (instancetype)sharedInstance;

@end
4 changes: 4 additions & 0 deletions ios/A0RNLock/Core/A0LockReact.m
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ - (void)showWithOptions:(NSDictionary *)options callback:(A0LockCallback)callbac
self.shown = YES;
}

- (BOOL)handleURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication {
return [self.lock handleURL:url sourceApplication:sourceApplication];
}

- (void)authenticateWithConnectionName:(NSString *)connectionName options:(NSDictionary *)options callback:(A0LockCallback)callback {
A0IdentityProviderAuthenticator *authenticator = [self.lock identityProviderAuthenticator];
void(^success)(A0UserProfile *, A0Token *) = ^(A0UserProfile *profile, A0Token *token) {
Expand Down
Loading

0 comments on commit d862cc2

Please sign in to comment.