Skip to content

Commit

Permalink
Merge pull request #9 from frontegg/FR-12447-fix-docs
Browse files Browse the repository at this point in the history
FR-13099 - Add Switch Tenant support
  • Loading branch information
frontegg-david authored Aug 17, 2023
2 parents d574e77 + f98a8f7 commit 460a822
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 304 deletions.
10 changes: 0 additions & 10 deletions .github/workflows/onPullRequestMerged.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,6 @@ jobs:
- name: Pack Package
run: |
yarn pack
- name: "Set Generated changelog"
uses: actions/github-script@v6
id: 'generated-changelog'
with:
result-encoding: string
script: |
const { generateChangeLog } = await import(`${process.env.GITHUB_WORKSPACE}/.github/scripts/index.js`);
return generateChangeLog({context, github, core})
- name: "Set incremented version"
uses: actions/github-script@v6
id: 'incremented-version'
Expand All @@ -101,8 +93,6 @@ jobs:
body: |
# v${{ steps.incremented-version.outputs.result }}
${{steps.generated-changelog.outputs.result}}
labels: "Type: Release"
branch: "release/next"

Expand Down
4 changes: 2 additions & 2 deletions FronteggRN.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ Pod::Spec.new do |s|
# See https://github.com/facebook/react-native/blob/febf6b7f33fdb4904669f99d795eba4c0f95d7bf/scripts/cocoapods/new_architecture.rb#L79.
if respond_to?(:install_modules_dependencies, true)
install_modules_dependencies(s)
s.dependency "FronteggSwift", "1.0.4"
s.dependency "FronteggSwift", "1.0.5"
else
s.dependency "React-Core"
s.dependency "FronteggSwift", "1.0.4"
s.dependency "FronteggSwift", "1.0.5"

# Don't install the dependencies when we run `pod install` in the old architecture.
if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then
Expand Down
96 changes: 65 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ Frontegg is a first-of-its-kind full-stack user management platform, empowering
- [Setup Android Project](#setup-android-project)
- [Set minimum SDK version](#set-minimum-sdk-version)
- [Configure build config fields](#configure-build-config-fields)
- [Register authentication activity](#register-authentication-activity)
- [Config Android AssetLinks](#config-ios-associated-domain)
- [Usages](#usages)
- [Wrap your app with FronteggProvider](#wrap-your-app-with-fronteggprovider)
- [Login with frontegg](#login-with-frontegg)
- [Check if user is authenticated](#check-if-user-is-authenticated)


## Project Requirements

- Minimum iOS deployment version **=> 14**
Expand All @@ -39,9 +42,11 @@ Copy FronteggDomain to future steps from [Frontegg Portal Domain](https://portal
- Navigate to [Login Method Settings](https://portal.frontegg.com/development/authentication/hosted)
- Toggle Hosted login method
- Add `{{IOS_BUNDLE_IDENTIFIER}}://{{FRONTEGG_BASE_URL}}/ios/oauth/callback`
- Add `{{ANDROID_PACKAGE_NAME}}://{{FRONTEGG_BASE_URL}}/android/oauth/callback`
- Replace `IOS_BUNDLE_IDENTIFIER` with your application identifier
- Replace `FRONTEGG_BASE_URL` with your frontegg base url

- Replace `ANDROID_PACKAGE_NAME` with your android package name
-
### Add frontegg package to the project

Use a package manager npm/yarn to install frontegg React Native library.
Expand All @@ -58,7 +63,6 @@ yarn add @frontegg/react-native

## Setup iOS Project


### Create Frontegg plist file

To setup your SwiftUI application to communicate with Frontegg, you have to create a new file named `Frontegg.plist` under
Expand Down Expand Up @@ -114,16 +118,16 @@ This property will store frontegg hostname (without https) and client id from pr

```groovy
def fronteggDomain = "DOMAIN_HOST.com"
def fronteggClientId = "CLIENT_ID"
def fronteggDomain = "FRONTEGG_DOMAIN_HOST.com" // without protocol https://
def fronteggClientId = "FRONTEGG_CLIENT_ID"
android {
defaultConfig {
manifestPlaceholders = [
package_name : applicationId,
frontegg_domain : fronteggDomain,
frontegg_client_id: fronteggClientId
"package_name" : applicationId,
"frontegg_domain" : fronteggDomain,
"frontegg_client_id": fronteggClientId
]
buildConfigField "String", 'FRONTEGG_DOMAIN', "\"$fronteggDomain\""
Expand All @@ -145,52 +149,48 @@ android {
```


### Register authentication activity
### Add permissions to AndroidManifest.xml

Open the app/src/main/AndroidManifest.xml file and add the following line to the before manifest section:
Add `INTERNET` permission to the app's manifest file.

```xml
<activity android:name="com.frontegg.android.AuthenticationActivity" android:exported="true">
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data
android:host="${frontegg_domain}"
android:pathPrefix="/android/${package_name}/callback"
android:scheme="https"
/>
</intent-filter>
</activity>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
```


### Config Android AssetLinks

Configuring your Android `AssetLinks` is required for Magic Link authentication / Reset Password / Activate Account / login with IdPs.
Configuring your Android `AssetLinks` is required for Magic Link authentication / Reset Password / Activate Account /
login with IdPs.

To add your `AssetLinks` to your Frontegg application, you will need to update in each of your integrated Frontegg
Environments the `AssetLinks` that you would like to use with that Environment. Send a POST request
to `https://api.frontegg.com/vendors/resources/associated-domains/v1/android` with the following payload:

To add your `AssetLinks` to your Frontegg application, you will need to update in each of your integrated Frontegg Environments the `AssetLinks` that you would like to use with that Environment. Send a POST request to `https://api.frontegg.com/vendors/resources/associated-domains/v1/android` with the following payload:
```
{
"packageName": "YOUR_APPLICATION_PACKAGE_NAME",
"sha256CertFingerprints": ["YOUR_KEYSTORE_CERT_FINGERPRINTS"]
}
```

Each Android app has multiple certificate fingerprint, to get your `DEBUG` sha256CertFingerprint you have to run the following command:
Each Android app has multiple certificate fingerprint, to get your `DEBUG` sha256CertFingerprint you have to run the
following command:

For Debug mode, run the following command and copy the `SHA-256` value

NOTE: make sure to choose the Variant and Config equals to `debug`

```bash
./gradlew signingReport

###################
# Example Output:
###################

# Variant: debugAndroidTest
# Variant: debug
# Config: debug
# Store: /Users/davidfrontegg/.android/debug.keystore
# Alias: AndroidDebugKey
Expand All @@ -201,13 +201,14 @@ For Debug mode, run the following command and copy the `SHA-256` value

```


For Release mode, Extract the SHA256 using keytool from your `Release` keystore file:

```bash
keytool -list -v -keystore /PATH/file.jks -alias YourAlias -storepass *** -keypass ***
```

In order to use our API’s, follow [this guide](https://docs.frontegg.com/reference/getting-started-with-your-api) to generate a vendor token.
In order to use our API’s, follow [this guide](https://docs.frontegg.com/reference/getting-started-with-your-api) to
generate a vendor token.


## Usages
Expand Down Expand Up @@ -272,6 +273,39 @@ export function MyScreen() {

```

### Switch tenant frontegg

To switch tenant, get switchTenant from the `useAuth` hook:

```tsx
import { useCallback } from 'react';
import { View, Button } from 'react-native';
import { useAuth } from '@frontegg/react-native';


export function MyScreen() {
const { switchTenant, user } = useAuth();

// user avaiable tenants from user.tenants
console.log("user tenants", user?.tenants)

const handleSwitchTenant = useCallback(() => {
const tenantId = 'TENANT_ID'; // get tenant id from your app state


switchTenant(tenantId).then(() => {
console.log('Tenant switched successfully');
}).catch((error) => {
console.log('Failed to switch tenant', error);
});
}, [ switchTenant ]);

return <View>
<Button title={'Switch Tenant'} onPress={handleSwitchTenant} />
</View>;
}

```

### Check if user is authenticated

Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "androidx.browser:browser:1.5.0"
implementation 'io.reactivex.rxjava3:rxkotlin:3.0.1'
implementation 'com.frontegg.sdk:android:1.0.5'
implementation 'com.frontegg.sdk:android:1.0.6'
implementation 'com.google.code.gson:gson:2.8.9'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ class FronteggRNModule(val reactContext: ReactApplicationContext) :
AuthenticationActivity.authenticateUsingBrowser(activity!!)
}

@ReactMethod
fun switchTenant(tenantId: String, promise: Promise) {
FronteggAuth.instance.switchTenant(tenantId) {
promise.resolve(tenantId)
}
}


override fun getConstants(): MutableMap<String, Any> {
val packageName = reactContext.packageName
Expand Down
10 changes: 5 additions & 5 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ PODS:
- FlipperKit/Core
- FlipperKit/FlipperKitNetworkPlugin
- fmt (6.2.1)
- FronteggRN (1.0.0):
- FronteggSwift (= 1.0.4)
- FronteggRN (1.0.2):
- FronteggSwift (= 1.0.5)
- RCT-Folly (= 2021.07.22.00)
- React-Core
- FronteggSwift (1.0.4)
- FronteggSwift (1.0.5)
- glog (0.3.5)
- hermes-engine (0.72.1):
- hermes-engine/Pre-built (= 0.72.1)
Expand Down Expand Up @@ -687,8 +687,8 @@ SPEC CHECKSUMS:
Flipper-PeerTalk: 116d8f857dc6ef55c7a5a75ea3ceaafe878aadc9
FlipperKit: 2efad7007d6745a3f95e4034d547be637f89d3f6
fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9
FronteggRN: 1b823fa7de13b7df67015fb9c11c36edd89eeb27
FronteggSwift: f3b758de35f2e6ec47b32e35f8867cd97d28b790
FronteggRN: 2f3e77c24b62f01233108a6abfc03ecfb158e8ce
FronteggSwift: 4a464f68fde341dfd10a571750c4a084b843946d
glog: 04b94705f318337d7ead9e6d17c019bd9b1f6b1b
hermes-engine: 9df83855a0fd15ef8eb61694652bae636b0c466e
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
Expand Down
43 changes: 42 additions & 1 deletion example/src/HomeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import * as React from 'react';

import { StyleSheet, View, Text, Button } from 'react-native';
import { useAuth } from '@frontegg/react-native';
import { switchTenant } from '../../src/FronteggNative';
import { useState } from 'react';
import type { ITenantsResponse } from '@frontegg/rest-api';

export default function HomeScreen() {
const [switching, setSwitching] = useState<string>('');
const {
showLoader,
initializing,
Expand All @@ -22,6 +26,7 @@ export default function HomeScreen() {
<Text>initializing: {initializing ? 'true' : 'false'}</Text>
<Text>isLoading: {isLoading ? 'true' : 'false'}</Text>
<Text>isAuthenticated: {isAuthenticated ? 'true' : 'false'}</Text>
<Text>Active Tenant: {user?.activeTenant.name}</Text>
<Text>refreshToken: {refreshToken}</Text>
<Text>
accessToken:{' '}
Expand All @@ -38,6 +43,31 @@ export default function HomeScreen() {
}}
/>
</View>

<Text style={styles.tenantsTitle}>Tenants</Text>

{(user?.tenants ?? [])
.sort((a: any, b: any) => a.name.localeCompare(b.name))
.map((tenant: ITenantsResponse) => (
<View key={tenant.tenantId} style={styles.tenantRow}>
<Button
title={`${tenant.name} ${
tenant.tenantId === switching
? ' (switching...)'
: tenant.tenantId === user?.activeTenant.tenantId
? ' (active)'
: ''
}`.trim()}
onPress={() => {
console.log(tenant.tenantId, user?.activeTenant.tenantId);
setSwitching(tenant.tenantId);
switchTenant(tenant.tenantId).then(() => {
setSwitching('');
});
}}
/>
</View>
))}
</View>
);
}
Expand All @@ -48,12 +78,23 @@ const styles = StyleSheet.create({
},
container: {
flex: 1,
alignItems: 'center',
padding: 20,
alignItems: 'flex-start',
justifyContent: 'center',
},
box: {
width: 60,
height: 60,
marginVertical: 20,
},
tenantsTitle: {
fontSize: 20,
marginTop: 20,
marginBottom: 20,
alignSelf: 'flex-start',
},
tenantRow: {
marginBottom: 8,
alignSelf: 'flex-start',
},
});
Loading

0 comments on commit 460a822

Please sign in to comment.