From 9888285556d2ee96cec9ed495a79c5c75da22b5a Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Sun, 28 Aug 2022 09:11:25 -0700 Subject: [PATCH] apple auth sort of works --- .../omnivore/omnivore/ui/auth/AppleAuth.kt | 103 ++---------------- .../omnivore/ui/auth/LoginViewModel.kt | 10 +- .../omnivore/ui/save/SaveViewModel.kt | 10 +- 3 files changed, 23 insertions(+), 100 deletions(-) diff --git a/android/Omnivore/app/src/main/java/app/omnivore/omnivore/ui/auth/AppleAuth.kt b/android/Omnivore/app/src/main/java/app/omnivore/omnivore/ui/auth/AppleAuth.kt index f75f32361..1d352b2ed 100644 --- a/android/Omnivore/app/src/main/java/app/omnivore/omnivore/ui/auth/AppleAuth.kt +++ b/android/Omnivore/app/src/main/java/app/omnivore/omnivore/ui/auth/AppleAuth.kt @@ -1,11 +1,10 @@ package app.omnivore.omnivore.ui.auth import android.annotation.SuppressLint +import android.net.Uri import android.util.Log import android.view.ViewGroup -import android.webkit.WebResourceRequest -import android.webkit.WebView -import android.webkit.WebViewClient +import android.webkit.* import androidx.compose.foundation.layout.* import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.TopAppBar @@ -37,9 +36,11 @@ fun AppleAuthButton(viewModel: LoginViewModel) { ) if (showDialog.value) { - AppleAuthDialog(onDismiss = { + AppleAuthDialog(onDismiss = { authToken -> + if (authToken != null ) { + viewModel.saveAuthToken(authToken) + } showDialog.value = false - Log.i("Apple payload: ", it ?: "null") }) } } @@ -76,7 +77,7 @@ fun AppleAuthWebView(onDismiss: (String?) -> Unit) { "&scope=" + AppleConstants.scope + "&response_mode=form_post" + "&state=android:login" - + // Adding a WebView inside AndroidView // with layout as full screen AndroidView(factory = { @@ -87,15 +88,10 @@ fun AppleAuthWebView(onDismiss: (String?) -> Unit) { ) webViewClient = object : WebViewClient() { override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean { - Log.i("Apple payload one: ", request?.url.toString() ?: "null") - if (request?.url.toString().startsWith(AppleConstants.redirectURI)) { -// handleUrl(request?.url.toString()) - onDismiss(request?.url.toString()) - // Close the dialog after getting the authorization code - if (request?.url.toString().contains("success=")) { - onDismiss(null) - } - return true + if (request?.url.toString().contains("client/auth")) { + val uri = Uri.parse(request!!.url.toString()) + val token = uri.getQueryParameter("tok") + onDismiss(token) } return true } @@ -107,80 +103,3 @@ fun AppleAuthWebView(onDismiss: (String?) -> Unit) { it.loadUrl(url) }) } - -//// A client to know about WebView navigation -//// For API 21 and above -//class AppleWebViewClient : WebViewClient() { -// @TargetApi(Build.VERSION_CODES.LOLLIPOP) -// override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean { -// if (request?.url.toString().startsWith(AppleConstants.redirectURI)) { -// handleUrl(request?.url.toString()) -// // Close the dialog after getting the authorization code -// if (request.url.toString().contains("success=")) { -//// appledialog.dismiss() -// } -// return true -// } -// return true -// } - -// // For API 19 and below -// override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean { -// if (url.startsWith(AppleConstants.redirectURI)) { -// handleUrl(url) -// // Close the dialog after getting the authorization code -// if (url.contains("success=")) { -//// appledialog.dismiss() -// } -// return true -// } -// return false -// } - -// @SuppressLint("ClickableViewAccessibility") -// override fun onPageFinished(view: WebView?, url: String?) { -// super.onPageFinished(view, url) -// // retrieve display dimensions -// val displayRectangle = Rect() -// val window = this@AppleWebViewClient.w -// window.decorView.getWindowVisibleDisplayFrame(displayRectangle) -// // Set height of the Dialog to 90% of the screen -// val layoutParams = view?.layoutParams -// layoutParams?.height = (displayRectangle.height() * 0.9f).toInt() -// view?.layoutParams = layoutParams -// } - -// // Check WebView url for access token code or error -// @SuppressLint("LongLogTag") -// private fun handleUrl(url: String) { -// val uri = Uri.parse(url) -// val success = uri.getQueryParameter("success") -// if (success == "true") { -// // Get the Authorization Code from the URL -//// appleAuthCode = uri.getQueryParameter("code") ?: "" -//// Log.i("Apple Code: ", appleAuthCode) -// // Get the Client Secret from the URL -//// appleClientSecret = uri.getQueryParameter("client_secret") ?: "" -//// Log.i("Apple Client Secret: ", appleClientSecret) -// //Check if user gave access to the app for the first time by checking if the url contains their email -// if (url.contains("email")) { -// //Get user's First Name -// val firstName = uri.getQueryParameter("first_name") -// Log.i("Apple User First Name: ", firstName ?: "") -// //Get user's Middle Name -// val middleName = uri.getQueryParameter("middle_name") -// Log.i("Apple User Middle Name: ", middleName ?: "") -// //Get user's Last Name -// val lastName = uri.getQueryParameter("last_name") -// Log.i("Apple User Last Name: ", lastName ?: "") -// //Get user's email -// val email = uri.getQueryParameter("email") -// Log.i("Apple User Email: ", email ?: "Not exists") -// } -// // Exchange the Auth Code for Access Token -//// requestForAccessToken(appleAuthCode, appleClientSecret) -// } else if (success == "false") { -// Log.e("ERROR", "We couldn't get the Auth Code") -// } -// } -//} diff --git a/android/Omnivore/app/src/main/java/app/omnivore/omnivore/ui/auth/LoginViewModel.kt b/android/Omnivore/app/src/main/java/app/omnivore/omnivore/ui/auth/LoginViewModel.kt index 352281598..9735f81be 100644 --- a/android/Omnivore/app/src/main/java/app/omnivore/omnivore/ui/auth/LoginViewModel.kt +++ b/android/Omnivore/app/src/main/java/app/omnivore/omnivore/ui/auth/LoginViewModel.kt @@ -68,6 +68,12 @@ class LoginViewModel @Inject constructor( } } + fun saveAuthToken(authToken: String) { + viewModelScope.launch { + datastoreRepo.putString(DatastoreKeys.omnivoreAuthToken, authToken) + } + } + fun logout() { viewModelScope.launch { datastoreRepo.clear() @@ -84,11 +90,7 @@ class LoginViewModel @Inject constructor( fun handleGoogleAuthTask(task: Task) { val result = task?.getResult(ApiException::class.java) - Log.d(ContentValues.TAG, "server auth code?: ${result.serverAuthCode}") - Log.d(ContentValues.TAG, "is Expired?: ${result.isExpired}") - Log.d(ContentValues.TAG, "granted Scopes?: ${result.grantedScopes}") val googleIdToken = result.idToken - Log.d(ContentValues.TAG, "Google id token?: $googleIdToken") // If token is missing then set the error message if (googleIdToken == null) { diff --git a/android/Omnivore/app/src/main/java/app/omnivore/omnivore/ui/save/SaveViewModel.kt b/android/Omnivore/app/src/main/java/app/omnivore/omnivore/ui/save/SaveViewModel.kt index 4e1acc23c..62a11b9ac 100644 --- a/android/Omnivore/app/src/main/java/app/omnivore/omnivore/ui/save/SaveViewModel.kt +++ b/android/Omnivore/app/src/main/java/app/omnivore/omnivore/ui/save/SaveViewModel.kt @@ -38,9 +38,11 @@ class SaveViewModel @Inject constructor( isLoading = true message = "Saving to Omnivore..." - val apiKey = getAuthToken() + val authToken = getAuthToken() - if (apiKey == null) { + Log.d(ContentValues.TAG, "AuthToken: $authToken") + + if (authToken == null) { message = "You are not logged in. Please login before saving." isLoading = false return@launch @@ -48,7 +50,7 @@ class SaveViewModel @Inject constructor( val apolloClient = ApolloClient.Builder() .serverUrl("${Constants.apiURL}/api/graphql") - .addHttpHeader("Authorization", value = apiKey) + .addHttpHeader("Authorization", value = authToken) .build() val response = apolloClient.mutation( @@ -70,7 +72,7 @@ class SaveViewModel @Inject constructor( "There was an error saving your page" } - Log.d(ContentValues.TAG, "Saved URL?: ${success.toString()}") + Log.d(ContentValues.TAG, "Saved URL?: $success") } } }