Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ ext {
}

buildscript {
ext.kotlin_version = '1.4.20-RC'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 29
Expand Down Expand Up @@ -49,4 +52,6 @@ dependencies {
testImplementation "junit:junit:$junitVersion"
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
implementation "androidx.core:core-ktx:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
8 changes: 8 additions & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.getcapacitor.community.applesignin.applesignin">
<application>
<activity
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
android:name="com.getcapacitor.community.applesignin.LoginActivity"
tools:ignore="WrongManifestParent">
</activity>
</application>
</manifest>

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.getcapacitor.community.applesignin

class ActivityResultCode {
var SUCCESS_RESULT = 0;
var ERROR_RESULT = 1;
var USER_CANCELED = 2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.getcapacitor.community.applesignin

import android.webkit.JavascriptInterface
import android.webkit.WebView

open class JavaScriptInterface(private var loginActivity: LoginActivity, protected var webView: WebView) {
@JavascriptInterface
fun setResult(result: String?) {
loginActivity.onSuccess(result)
}

@JavascriptInterface
fun onError(error: String?) {
loginActivity.onFailed(error)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.getcapacitor.community.applesignin

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import android.webkit.WebView
import com.getcapacitor.community.applesignin.applesignin.R

class LoginActivity : Activity() {
private var activityResultCode = ActivityResultCode()

@SuppressLint("SetJavaScriptEnabled")
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val appleAuthenticationURL = intent.getStringExtra("APPLE_OAUTH_URL")
setContentView(R.layout.bridge_layout_main)
val webView = findViewById<WebView>(R.id.webview)
webView.settings.javaScriptEnabled = true
webView.loadUrl(appleAuthenticationURL)
webView.addJavascriptInterface(JavaScriptInterface(this, webView), "responseHandler")
}

private fun setResult(data: String?, code: Int) {
val intent = Intent()
when (code) {
activityResultCode.SUCCESS_RESULT -> {
intent.putExtra("token", data)
}
else -> {
intent.putExtra("error", data)
}
}
intent.putExtra("code", code)
setResult(code, intent)
finish()
}

fun onSuccess(result: String?) {
println("Login success")
this.setResult(result, activityResultCode.SUCCESS_RESULT)
}

fun onFailed(error: String?) {
println("Login failed")
this.setResult(error, activityResultCode.ERROR_RESULT)
}

override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
if (keyCode == KeyEvent.KEYCODE_BACK) {
this.setResult("user canceled", activityResultCode.USER_CANCELED)
}
return super.onKeyDown(keyCode, event)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.getcapacitor.community.applesignin

import android.content.Intent
import com.getcapacitor.*
import java.io.UnsupportedEncodingException
import java.net.URLEncoder
import kotlin.jvm.Throws

@NativePlugin(
requestCodes = [SignInWithApple.requestCode]
)
class SignInWithApple : Plugin() {
private var baseAuthURL = "https://appleid.apple.com/auth/authorize"
private var activityResultCode = ActivityResultCode()
companion object {
const val requestCode = 1001
}

@PluginMethod
@Throws(UnsupportedEncodingException::class)
fun AuthorizeAndroid(call: PluginCall) {
saveCall(call)
val clientId = call.getString("clientId")
val redirectURI = call.getString("redirectURI")
val responseType = call.getString("responseType")
val scope = call.getString("scope")
val responseMode = call.getString("responseMode")
val queryParams = String.format(
"?client_id=%s&redirect_uri=%s&response_type=%s&scope=%s&response_mode=%s",
clientId,
URLEncoder.encode(redirectURI, "UTF-8"),
responseType,
scope,
responseMode
)
val appleAuthURL = baseAuthURL + queryParams
val intent = Intent(context, LoginActivity::class.java)
intent.putExtra("APPLE_OAUTH_URL", appleAuthURL)
intent.putExtra("REDIRECT_URI", redirectURI)
startActivityForResult(call, intent, requestCode)
}

override fun handleOnActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent
) {
super.handleOnActivityResult(requestCode, resultCode, data)
if (requestCode == SignInWithApple.requestCode) {
val savedCall = savedCall ?: return
when (resultCode) {
activityResultCode.SUCCESS_RESULT -> {
val token = data.getStringExtra("token")
val ret = JSObject()
ret.put("value", token)
savedCall.success(ret)
}
activityResultCode.ERROR_RESULT, activityResultCode.USER_CANCELED -> {
val errorMessage = data.getStringExtra("error")
val errorCode = data.getStringExtra("code")
savedCall.reject(errorMessage, errorCode)
}
}
}
}
}