Skip to content

Commit

Permalink
Merge pull request #58 from frontegg/FR-17074-fix-double-refresh-token
Browse files Browse the repository at this point in the history
FR-17074 - Clear Webview when detached from window
  • Loading branch information
frontegg-david authored Jul 30, 2024
2 parents 610fd1b + f1cac03 commit 6888821
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class EmbeddedAuthActivity : Activity() {
consumeIntent(intent)
}


override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putBoolean(DIRECT_LOGIN_ACTION_LAUNCHED, this.directLoginLaunched);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,19 @@ open class FronteggWebView : WebView {
this.addJavascriptInterface(FronteggNativeBridge(context), "FronteggNativeBridge")
}

fun loadOauthAuthorize() {
val authorizeUrl = AuthorizeUrlGenerator()
val url = authorizeUrl.generate()
this.loadUrl(url.first)


override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
clearWebView()
}

private fun clearWebView() {
clearHistory()
loadUrl("about:blank")
onPause()
removeAllViews()
destroy()
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class NavigationActivity : AppCompatActivity() {

private val authenticatedTabs = AppBarConfiguration(
setOf(
R.id.navigation_home, R.id.navigation_tenants
R.id.navigation_home,
R.id.navigation_tenants,
R.id.navigation_access_token
)
)
private val nonAuthTabs = AppBarConfiguration(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.frontegg.demo.ui.home

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.frontegg.android.FronteggAuth
import com.frontegg.demo.databinding.FragmentAccessTokenBinding

class AccessTokenFragment : Fragment() {

private var _binding: FragmentAccessTokenBinding? = null

// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val homeViewModel = ViewModelProvider(this)[HomeViewModel::class.java]

_binding = FragmentAccessTokenBinding.inflate(inflater, container, false)

val root: View = binding.root

homeViewModel.user.observe(viewLifecycleOwner) {
if (it != null) {

binding.name.text = it.name
binding.email.text = it.email
}
}

homeViewModel.accesstoken.observe(viewLifecycleOwner) {

// this one will be disable in background
// will resume on foreground
Log.w("AccessTokenFragment", "Access token (viewModel): $it")
binding.accessToken.text = it
}

return root
}

override fun onResume() {
super.onResume()

FronteggAuth.instance.accessToken.subscribe {
Log.w("AccessTokenFragment", "Access token(subscribe): ${it.value}")
Log.w("AccessTokenFragment", "Access token(direct): ${FronteggAuth.instance.accessToken.value}")
};
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ package com.frontegg.demo.ui.home

import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.bumptech.glide.Glide
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,17 @@ class HomeViewModel : ViewModel() {
}
}

private val _accesstoken = MutableLiveData<String?>().apply {
FronteggAuth.instance.accessToken.subscribe {
Handler(Looper.getMainLooper()).post {
value = it.value
}
}
}


val user: LiveData<User?> = _user
val accesstoken: LiveData<String?> = _accesstoken


}
62 changes: 62 additions & 0 deletions embedded/src/main/res/layout/fragment_access_token.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.AccessTokenFragment">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:hint="Full Name"
android:textAlignment="textStart"
android:textSize="20sp"
android:textColor="@color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="Email"
android:textAlignment="textStart"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/name" />

<TextView
android:id="@+id/accessToken"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="Access Token"
android:textAlignment="textStart"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/email" />
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
1 change: 1 addition & 0 deletions embedded/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/email" />

<Button
android:id="@+id/logout_button"
android:layout_width="wrap_content"
Expand Down
5 changes: 5 additions & 0 deletions embedded/src/main/res/menu/bottom_nav_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/title_tenants" />

<item
android:id="@+id/navigation_access_token"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/title_access_token" />

</menu>
6 changes: 6 additions & 0 deletions embedded/src/main/res/navigation/navigation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@
android:name="com.frontegg.demo.ui.tenants.TenantsFragment"
android:label="@string/title_tenants"
tools:layout="@layout/fragment_tenants" />

<fragment
android:id="@+id/navigation_access_token"
android:name="com.frontegg.demo.ui.home.AccessTokenFragment"
android:label="@string/title_access_token"
tools:layout="@layout/fragment_access_token" />
</navigation>
1 change: 1 addition & 0 deletions embedded/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
<string name="title_activity_navigation">NavigationActivity</string>
<string name="title_home">Profile</string>
<string name="title_tenants">Tenants</string>
<string name="title_access_token">Access Token</string>
</resources>

0 comments on commit 6888821

Please sign in to comment.