diff --git a/android/Omnivore/app/src/main/java/app/omnivore/omnivore/Constants.kt b/android/Omnivore/app/src/main/java/app/omnivore/omnivore/Constants.kt index 9f776c799..db72ef5b7 100644 --- a/android/Omnivore/app/src/main/java/app/omnivore/omnivore/Constants.kt +++ b/android/Omnivore/app/src/main/java/app/omnivore/omnivore/Constants.kt @@ -12,21 +12,8 @@ object DatastoreKeys { object AppleConstants { const val clientId = "app.omnivore" - const val redirectURI = "MY_REDIRECT_URI" + const val redirectURI = "https://api-demo.omnivore.app/api/auth/vercel/apple-redirect" const val scope = "name%20email" const val authUrl = "https://appleid.apple.com/auth/authorize" const val tokenUrl = "https://appleid.apple.com/auth/token" } - -//clientId="app.omnivore" -//scope="name email" -//state="web:login" -//redirectURI={appleAuthRedirectURI} -//responseMode="form_post" -//responseType="code id_token" -//designProp={{ -// color: 'black', -// width: 261, -// height: 40, -// type: 'continue', -//}} 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 cf0dd50ee..5bfd56466 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,31 +1,31 @@ package app.omnivore.omnivore.ui.auth +import android.annotation.SuppressLint +import android.util.Log import android.view.ViewGroup +import android.webkit.WebResourceRequest import android.webkit.WebView import android.webkit.WebViewClient -import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.* import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material.icons.Icons -import androidx.compose.material.icons.filled.Done -import androidx.compose.material3.Icon -import androidx.compose.material3.Surface +import androidx.compose.material.TopAppBar +import androidx.compose.material3.* import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember -import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color -import androidx.compose.ui.res.colorResource import androidx.compose.ui.res.painterResource import androidx.compose.ui.unit.dp import androidx.compose.ui.viewinterop.AndroidView import androidx.compose.ui.window.Dialog +import app.omnivore.omnivore.AppleConstants import app.omnivore.omnivore.R +import java.util.* @Composable fun AppleAuthButton(viewModel: LoginViewModel) { - var showDialog = remember { mutableStateOf(false) } + val showDialog = remember { mutableStateOf(false) } LoadingButtonWithIcon( text = "Continue with Apple", @@ -37,50 +37,55 @@ fun AppleAuthButton(viewModel: LoginViewModel) { ) if (showDialog.value) { - AppleAuthDialog(setShowDialog = { + AppleAuthDialog(onDismiss = { showDialog.value = false + Log.i("Apple payload: ", it ?: "null") }) } } @Composable -fun AppleAuthDialog(setShowDialog: (Boolean) -> Unit) { - Dialog(onDismissRequest = { setShowDialog(false) }) { +fun AppleAuthDialog(onDismiss: (String?) -> Unit) { + Dialog(onDismissRequest = { onDismiss(null) }) { Surface( shape = RoundedCornerShape(16.dp), color = Color.White ) { - AppleAuthWebView() -// Box( -// contentAlignment = Alignment.Center -// ) { -// Column(modifier = Modifier.padding(20.dp)) { -// Row( -// modifier = Modifier.fillMaxWidth(), -// horizontalArrangement = Arrangement.SpaceBetween, -// verticalAlignment = Alignment.CenterVertically -// ) { -// Icon( -// imageVector = Icons.Filled.Done, -// contentDescription = "", -// tint = colorResource(android.R.color.darker_gray), -// modifier = Modifier -// .width(30.dp) -// .height(30.dp) -// .clickable { setShowDialog(false) } -// ) -// } -// -// } -// } + AppleAuthWebContainerView(onDismiss) } } } +@OptIn(ExperimentalMaterial3Api::class) +@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter") @Composable -fun AppleAuthWebView() { - // Declare a string that contains a url - val mUrl = "https://omnivore.app" +fun AppleAuthWebContainerView(onDismiss: (String?) -> Unit) { + Scaffold( + topBar = { TopAppBar(title = { Text("WebView", color = Color.White) }, backgroundColor = Color(0xff0f9d58)) }, + content = { AppleAuthWebView(onDismiss) } + ) +} + +@SuppressLint("SetJavaScriptEnabled") +@Composable +fun AppleAuthWebView(onDismiss: (String?) -> Unit) { + val url = AppleConstants.authUrl + + "?client_id=" + + AppleConstants.clientId + + "&redirect_uri=" + + AppleConstants.redirectURI + + "&response_type=code%20id_token&scope=" + + AppleConstants.scope + + "&response_mode=form_post&state=android:login" + +// clientId="app.omnivore" +// scope="name email" +// state="web:login" +// redirectURI={appleAuthRedirectURI} +// responseMode="form_post" +// responseType="code id_token" +// designProp={{ +// color: 'black', // Adding a WebView inside AndroidView // with layout as full screen @@ -90,10 +95,103 @@ fun AppleAuthWebView() { ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT ) - webViewClient = WebViewClient() - loadUrl(mUrl) +// webViewClient = WebViewClient() + 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 + } + return true + } + } + settings.javaScriptEnabled = true + loadUrl(url) } }, update = { - it.loadUrl(mUrl) + 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") +// } +// } +//}