Magic for Android Apps
Magic for Android Apps
New to Magic?
Create a fully-functional Magic auth demo in minutes.
Reference for the Magic SDK for Android.
#Overview
The Magic SDK for Android is your entry-point to secure, passwordless authentication for your mobile app. This guide will cover some important topics for getting started with Android APIs and to make the most of Magic's features.
Magic can support either server-based or serverless web applications. It is up to the developers to implement the Admin SDK to validate the DID Token.
- View the API documentation below to learn the methods you'll be using
#Installation
Magic SDK is available in mavenCentral
. Simply add the following line to the build.gradle
in your Android project:
dependencies {
implementation 'link.magic:magic-android:1.1.1'
}
repositories {
mavenCentral()
}
Sync the project with new dependencies settings.
0.x has been deprecated. Please follow the new instantiation method to access the latest feature
#Creating an SDK Instance (1.x)
- Extend the application class. Instantiate Magic and assign it to application attribute. In this way, you may access the full Magic object in any activity within the application.
class YourApp : Application() {
val magic = Magic( "YOUR_PUBLISHABLE_KEY")
}
- In any activity where you need Magic login, call
magic.startRelayer(this)
inonCreate()
to enable the Magic relayer first.
open class MagicActivity: AppCompatActivity() {
lateinit var magic: Magic
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
magic = (applicationContext as YourApp).magic
magic.startRelayer(this)
}
}
#Returns CompletableFuture
All Magic functions are asynchronous calls. They will return a CompletableFuture object, which resolves the results in a Response class.
To get the result without blocking the UI thread, call CompletableFuture.whenComplete
to wait for the results asynchronously.
val result = magic.auth.loginWithMagicLink("hello@example.com")
result.whenComplete { response: DIDToken?, error: Throwable? ->
if (error != null) {
//Handle Error
}
if (response != null && !response.hasError()) {
Log.d("Magic", "You're logged in." + response.result)
} else {
Log.d("login", "Not Logged in")
}
}
For the full implementation of Response class, please check here.
#Destroy Relayer
To better support Android devices, we implement a way to destroy the relayer to release memory and improve the performance
Using this method may break the communication to the Magic services and trigger unexpected UI exception. Please use it carefully.
override fun onDestroy() {
magic.destroyRelayer()
super.onDestroy()
}
You may restart the relayer by calling
override fun onCreate(savedInstanceState: Bundle?) {
magic.startRelayer(this)
}