nx: add login flow basics

pull/653/merge
Varun Patil 2023-05-16 01:07:00 -07:00
parent 0a47baf16e
commit 56308aa8aa
3 changed files with 43 additions and 5 deletions

View File

@ -44,4 +44,5 @@ dependencies {
implementation "androidx.media3:media3-exoplayer:$mediaVersion"
implementation "androidx.media3:media3-ui:$mediaVersion"
implementation "androidx.media3:media3-exoplayer-hls:$mediaVersion"
implementation "com.squareup.okhttp3:okhttp:4.10.0"
}

View File

@ -90,7 +90,7 @@
// API is fine, redirect to login page
clearTimeout(timeoutId);
window.nativex.login(data?.baseUrl || getMemoriesUrl().toString());
window.nativex.login(data.baseUrl, data.loginFlowUrl);
} catch (e) {
// CORS request, we know nothing about the server
connError.innerText = `Failed to fetch server info.\nIs Memories installed and enabled?\n${urlStr}`;

View File

@ -1,5 +1,6 @@
package gallery.memories
import android.content.Intent
import android.net.Uri
import android.util.Log
import android.view.SoundEffectConstants
@ -12,6 +13,11 @@ import gallery.memories.mapper.SystemImage
import gallery.memories.service.DownloadService
import gallery.memories.service.ImageService
import gallery.memories.service.TimelineQuery
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
import java.io.ByteArrayInputStream
import java.net.URLDecoder
@ -90,11 +96,42 @@ import java.net.URLDecoder
get() = true
@JavascriptInterface
fun login(server: String?) {
if (server == null) return;
fun toast(message: String) {
mActivity.runOnUiThread {
Toast.makeText(mActivity, server, Toast.LENGTH_LONG).show()
Toast.makeText(mActivity, message, Toast.LENGTH_LONG).show()
}
}
@JavascriptInterface
fun login(baseUrl: String?, loginFlowUrl: String?) {
if (baseUrl == null || loginFlowUrl == null) return;
// Make POST request to login flow URL
val client = OkHttpClient()
val request = Request.Builder()
.url(loginFlowUrl)
.post("".toRequestBody("application/json".toMediaTypeOrNull()))
.build()
val response = client.newCall(request).execute()
// Read response body
val body = response.body?.string()
if (body == null) {
toast("Failed to get login flow response")
return
}
// Parse response body as JSON
val json = JSONObject(body)
try {
val loginUrl = json.getString("login")
toast("Opening login page...")
// Open login page in browser
mActivity.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(loginUrl)))
} catch (e: Exception) {
Log.e(TAG, "login: ", e)
toast("Failed to parse login flow response")
}
}