apple auth sort of works

This commit is contained in:
Satindar Dhillon 2022-08-28 09:11:25 -07:00
parent f4ebcd5578
commit 9888285556
3 changed files with 23 additions and 100 deletions

View file

@ -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")
// }
// }
//}

View file

@ -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<GoogleSignInAccount>) {
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) {

View file

@ -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")
}
}
}