Skip to content

Commit

Permalink
Merge pull request #2 from derniercri/feat/gigya
Browse files Browse the repository at this point in the history
Add gigya implementation
  • Loading branch information
martinfrouin authored Oct 20, 2023
2 parents fd61a32 + 42fc75d commit a22a15d
Show file tree
Hide file tree
Showing 30 changed files with 262 additions and 33 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# React Native WebView

Forked from `react-native-webview/react-native-webview`
Add Gigya implementation with the prop `gigyaCredentials`

![star this repo](https://img.shields.io/github/stars/react-native-webview/react-native-webview?style=flat-square)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
[![NPM Version](https://img.shields.io/npm/v/react-native-webview.svg?style=flat-square)](https://www.npmjs.com/package/react-native-webview)
Expand Down
2 changes: 2 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ repositories {
}

dependencies {
implementation 'com.google.code.gson:gson:2.8.9'
implementation 'com.facebook.react:react-native:+'
implementation "org.jetbrains.kotlin:kotlin-stdlib:${safeExtGet('kotlinVersion')}"
implementation "androidx.webkit:webkit:${safeExtGet('webkitVersion')}"
implementation files('libs/gigya-android-sdk-core-v7.0.5.aar')
}

if (isNewArchitectureEnabled()) {
Expand Down
Binary file added android/libs/gigya-android-sdk-core-v7.0.5.aar
Binary file not shown.
56 changes: 56 additions & 0 deletions android/src/main/java/com/reactnativecommunity/webview/RNCGigya.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.reactnativecommunity.webview

import android.app.Application
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import com.gigya.android.sdk.Gigya
import com.gigya.android.sdk.GigyaPluginCallback
import com.gigya.android.sdk.account.models.GigyaAccount
import com.gigya.android.sdk.session.SessionInfo
import com.gigya.android.sdk.ui.plugin.IGigyaWebBridge

class RNCGigya(context: Application, apiKey: String, apiDomain: String) {
private var gigya: Gigya<GigyaAccount>

init {
Gigya.setApplication(context)
gigya = Gigya.getInstance(GigyaAccount::class.java)
gigya.init(apiKey, apiDomain)
}

fun initialize(sessionToken: String, sessionSecret: String, webview: WebView) {
attachBridge(webview)
logUser(sessionToken = sessionToken, sessionSecret = sessionSecret)
}

private fun logUser(sessionToken: String, sessionSecret: String) {
val session = SessionInfo(sessionSecret, sessionToken)
gigya.setSession(session)
}


private fun attachBridge(webview: WebView) {
var webBridge: IGigyaWebBridge<GigyaAccount>? = null

/*
Make sure you enable javascript for your WebView instance.
*/
val webSettings = webview.settings
webSettings.javaScriptEnabled = true

webBridge = gigya.createWebBridge()
webBridge?.attachTo(webview, object: GigyaPluginCallback<GigyaAccount>() {}, null)

/*
Make sure to attach the GigyaWebBridge to your WebViewClient instance.
*/
webview.webViewClient = (object: WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
val uri = request?.url
val uriString = uri.toString()
return webBridge?.invoke(uriString) ?: false
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.reactnativecommunity.webview;

class RNCGigyaCredentials {
String sessionToken;
String sessionSecret;
String apiKey;
String apiDomain;

RNCGigyaCredentials(String sessionToken, String sessionSecret, String apiKey, String apiDomain) {
this.sessionToken = sessionToken;
this.sessionSecret = sessionSecret;
this.apiKey = apiKey;
this.apiDomain = apiDomain;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public void setBasicAuthCredential(RNCBasicAuthCredential credential) {
mRNCWebViewClient.setBasicAuthCredential(credential);
}

public void setGigyaCredentials(RNCGigyaCredentials credential) {
mRNCWebViewClient.setGigyaCredentials(credential);
}

public void setSendContentSizeChangeEvents(boolean sendContentSizeChangeEvents) {
this.sendContentSizeChangeEvents = sendContentSizeChangeEvents;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class RNCWebViewClient extends WebViewClient {
protected RNCWebView.ProgressChangedFilter progressChangedFilter = null;
protected @Nullable String ignoreErrFailedForThisURL = null;
protected @Nullable RNCBasicAuthCredential basicAuthCredential = null;
protected @Nullable RNCGigyaCredentials gigyaCredentials = null;

public void setIgnoreErrFailedForThisURL(@Nullable String url) {
ignoreErrFailedForThisURL = url;
Expand All @@ -51,6 +52,10 @@ public void setBasicAuthCredential(@Nullable RNCBasicAuthCredential credential)
basicAuthCredential = credential;
}

public void setGigyaCredentials(@Nullable RNCGigyaCredentials credential) {
gigyaCredentials = credential;
}

@Override
public void onPageFinished(WebView webView, String url) {
super.onPageFinished(webView, url);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.reactnativecommunity.webview

import android.app.Application
import android.app.DownloadManager
import android.content.pm.ActivityInfo
import android.graphics.Bitmap
Expand Down Expand Up @@ -90,6 +91,7 @@ class RNCWebViewManagerImpl {
if (ReactBuildConfig.DEBUG) {
WebView.setWebContentsDebuggingEnabled(true)
}

webView.setDownloadListener(DownloadListener { url, userAgent, contentDisposition, mimetype, contentLength ->
webView.setIgnoreErrFailedForThisURL(url)
val module = webView.themedReactContext.getNativeModule(RNCWebViewModule::class.java) ?: return@DownloadListener
Expand Down Expand Up @@ -268,6 +270,28 @@ class RNCWebViewManagerImpl {
viewWrapper.webView.setBasicAuthCredential(basicAuthCredential)
}

fun setGigyaCredentials(viewWrapper: RNCWebViewWrapper, credential: ReadableMap?) {
var gigyaCredentials: RNCGigyaCredentials? = null
if (credential != null) {
if (credential.hasKey("sessionToken") && credential.hasKey("sessionSecret") && credential.hasKey("apiKey") && credential.hasKey("apiDomain")) {
val sessionToken = credential.getString("sessionToken")
val sessionSecret = credential.getString("sessionSecret")
val apiKey = credential.getString("apiKey")
val apiDomain = credential.getString("apiDomain")

gigyaCredentials = RNCGigyaCredentials(sessionToken, sessionSecret, apiKey, apiDomain)

if (sessionToken != null && sessionSecret != null && apiKey != null && apiDomain != null) {
val application = viewWrapper.webView.themedReactContext.currentActivity?.application as Application
val gigya = RNCGigya(application, apiKey, apiDomain)

gigya.initialize(sessionToken, sessionSecret, viewWrapper.webView)
}
}
}
viewWrapper.webView.setGigyaCredentials(gigyaCredentials)
}

fun onDropViewInstance(viewWrapper: RNCWebViewWrapper) {
val webView = viewWrapper.webView
webView.themedReactContext.removeLifecycleEventListener(webView)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ public void setBasicAuthCredential(RNCWebViewWrapper view, @Nullable ReadableMap
mRNCWebViewManagerImpl.setBasicAuthCredential(view, value);
}

@Override
@ReactProp(name = "gigyaCredentials")
public void setGigyaCredentials(RNCWebViewWrapper view, @Nullable ReadableMap value) {
mRNCWebViewManagerImpl.setGigyaCredentials(view, value);
}

@Override
@ReactProp(name = "cacheEnabled")
public void setCacheEnabled(RNCWebViewWrapper view, boolean value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public void setBasicAuthCredential(RNCWebViewWrapper view, @Nullable ReadableMap
mRNCWebViewManagerImpl.setBasicAuthCredential(view, value);
}

@ReactProp(name = "gigyaCredentials")
public void setGigyaCredentials(RNCWebViewWrapper view, @Nullable ReadableMap value) {
mRNCWebViewManagerImpl.setGigyaCredentials(view, value);
}

@ReactProp(name = "cacheEnabled")
public void setCacheEnabled(RNCWebViewWrapper view, boolean value) {
mRNCWebViewManagerImpl.setCacheEnabled(view, value);
Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Component } from 'react';
// eslint-disable-next-line
import { IOSWebViewProps, AndroidWebViewProps, WindowsWebViewProps } from './lib/WebViewTypes';
import { IOSWebViewProps, AndroidWebViewProps, WindowsWebViewProps } from './src/WebViewTypes';

export { FileDownload, WebViewMessageEvent, WebViewNavigation } from "./lib/WebViewTypes";
export { FileDownload, WebViewMessageEvent, WebViewNavigation } from "./src/WebViewTypes";

export type WebViewProps = IOSWebViewProps & AndroidWebViewProps & WindowsWebViewProps;

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import WebView from './lib/WebView';
import WebView from './src/WebView';

export { WebView };
export default WebView;
39 changes: 39 additions & 0 deletions ios/RNCGigya.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// RNCGigya.swift
// RNCWebView
//
// Created by Martin Frouin on 19/10/2023.
// Copyright © 2023 Facebook. All rights reserved.
//

import Foundation
import UIKit
import WebKit
import Gigya

@objc public class RNCGigya: NSObject {
let gigya = Gigya.sharedInstance()

@objc public func initialize(controller: UIViewController, webview: WKWebView, sessionToken: String, sessionSecret: String, apiKey: String, apiDomain: String) {
gigya.initFor(apiKey: apiKey, apiDomain: apiDomain)

self.attachBridge(controller: controller, webview: webview)
self.logUser(sessionToken: sessionToken, sessionSecret: sessionSecret)
}

func attachBridge(controller: UIViewController, webview: WKWebView) {
let webBridge = gigya.createWebBridge()

webBridge.attachTo(webView: webview, viewController: controller) { _ in }

}

func logUser(sessionToken: String, sessionSecret: String) {
let session = GigyaSession(sessionToken: sessionToken, secret: sessionSecret)

if (session != nil) {
gigya.setSession(session!)
}
}

}
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions ios/RNCWebView-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//

File renamed without changes.
10 changes: 10 additions & 0 deletions apple/RNCWebView.mm → ios/RNCWebView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,16 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
@"password": RCTNSStringFromString(newViewProps.basicAuthCredential.password)
}];
}

if (oldViewProps.gigyaCredentials.sessionToken != newViewProps.gigyaCredentials.sessionToken || oldViewProps.gigyaCredentials.sessionSecret != newViewProps.gigyaCredentials.sessionSecret || oldViewProps.sessionSecret.apiKey != newViewProps.sessionSecret.apiKey || oldViewProps.sessionSecret.apiDomain != newViewProps.sessionSecret.apiDomain) {
[_view setBasicAuthCredential: @{
@"sessionToken": RCTNSStringFromString(newViewProps.gigyaCredentials.sessionToken),
@"sessionSecret": RCTNSStringFromString(newViewProps.gigyaCredentials.sessionSecret),
@"apiKey": RCTNSStringFromString(newViewProps.gigyaCredentials.apiKey),
@"apiDomain": RCTNSStringFromString(newViewProps.gigyaCredentials.apiDomain)
}];
}

if (oldViewProps.contentInsetAdjustmentBehavior != newViewProps.contentInsetAdjustmentBehavior) {
if (newViewProps.contentInsetAdjustmentBehavior == RNCWebViewContentInsetAdjustmentBehavior::Never) {
[_view setContentInsetAdjustmentBehavior: UIScrollViewContentInsetAdjustmentNever];
Expand Down
Loading

0 comments on commit a22a15d

Please sign in to comment.