The Piano ID Android SDK is available as an AAR via Maven Central. To add dependencies, open your project’s build.gradle/build.gradle.kts and update the dependencies block as follows:
Kotlin DSL
dependencies {
implementation("io.piano.android:id:$VERSION")
}
PianoId.ENDPOINT_PRODUCTION - Production endpoint
PianoId.ENDPOINT_PRODUCTION_EUROPE - Europe production endpoint
PianoId.ENDPOINT_PRODUCTION_AUSTRALIA - Australia production endpoint
PianoId.ENDPOINT_PRODUCTION_ASIA_PACIFIC - Asia/Pacific production endpoint
PianoId.ENDPOINT_SANDBOX - Sandbox endpoint
Kotlin
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
PianoId.init(PianoId.ENDPOINT_PRODUCTION, BuildConfig.PIANO_AID)
}
}
Register processing for auth result (see here for more info)
Kotlin
private val authResult = registerForActivityResult(PianoIdAuthResultContract()) { r ->
when (r) {
null -> { /* user cancelled Authorization process */ }
is PianoIdAuthSuccessResult -> {
val token = r.token
val isNewUserRegistered = r.isNewUser
if (token.emailConfirmationRequired) {
// process enabled Double opt-in
}
// process successful authorization
}
is PianoIdAuthFailureResult -> {
val e = r.exception
// Authorization failed, check e.cause for details
}
}
}
Start auth process: Check authorization result in your onActivityResult:
Kotlin
authResult.launch(
PianoId.signIn()
// ... configure "Sign In" ...
)
Note:
The activity loads preconfigured template from dashboard. Links in the template are processed differently:
- normal link - opens within app instead current page, user can return back via system button (page will be reloaded)
- link with
target="_blank"
- opens externally in browser
Kotlin
PianoId.signout(accessToken)
// or
PianoId.signout(accessToken) {
// callback code here
}
Kotlin
PianoId.refreshToken(refreshToken) {
// callback code here
}
Google: Kotlin DSL
dependencies {
implementation("io.piano.android:id-oauth-google:$VERSION")
}
Facebook: Kotlin DSL
dependencies {
implementation("io.piano.android:id-oauth-facebook:$VERSION")
}
Configure your project for Google
Kotlin
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
PianoId.init(...).with(GoogleOAuthProvider())
}
}
Configure your project for Facebook
Kotlin
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
PianoId.init(...).with(FacebookOAuthProvider())
}
}
Add for your "log in" activity in AndroidManifest.xml
:
<activity android:name=".MyExistingLoginActivity">
<intent-filter
android:autoVerify="true"
tools:targetApi="m">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="success"
android:scheme="piano.id.oauth.{YOUR_AID}" />
</intent-filter>
</activity>
Add to your "log in" activity code into onCreate
for Kotlin:
val uri = intent.data
if (uri.isPianoIdUri()) {
// It's Piano paswordless auth link
uri.parsePianoIdToken { r ->
r.onFailure {
// Auth unsuccessful, process it
}.onSuccess {
// Auth successful, save access token here
}
}
} else {
// App deep link, process as usual
}