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:
01dependencies {
02 implementation 'link.magic:magic-android:4.0.0'
03 implementation 'org.web3j:core:4.8.8-android'
04}
05
06repositories {
07 mavenCentral()
08}
Sync the project with new dependencies settings.
#Creating an SDK Instance (4.x)
Since Magic Android SDK 4.x, we have made some improvements in how Auth Relayer is rendered and stays active in an Android App to maintain stable channels with users' wallets.
To develop the best practice, you may instantiate Magic in the `onCreate` method in the extended Application class. This will preload a webview to get ready for future authentication. Then you may assign the Magic object to the application attribute. With this setup, you may access and share the Magic object among all activities in the application.
01class MagicDemoApp : Application() {
02 lateinit var magic: Magic
03
04 override fun onCreate() {
05 magic = Magic(this, "YOUR_PUBLISHABLE_KEY")
06 super.onCreate()
07 }
08}
01open class MagicActivity: AppCompatActivity() {
02
03 lateinit var magic: Magic
04
05 override fun onCreate(savedInstanceState: Bundle?) {
06 super.onCreate(savedInstanceState)
07
08 magic = (applicationContext as MagicDemoApp).magic
09 }
10}
Or, just simply instantiate Magic in your login activity and use Fragments to render the contents in the same activity.
01open class LoginActivity: AppCompatActivity() {
02
03 lateinit var magic: Magic
04
05 override fun onCreate(savedInstanceState: Bundle?) {
06 super.onCreate(savedInstanceState)
07
08 magic = Magic(this, "YOUR_PUBLISHABLE_KEY")
09 }
10}
#Breaking changes
* When instantiating the Magic class, an Application Context is required in the API call to preload the Auth Relayer when starting the app.
01open class LoginActivity: AppCompatActivity() {
02
03 lateinit var magic: Magic
04
05 override fun onCreate(savedInstanceState: Bundle?) {
06 super.onCreate(savedInstanceState)
07
08 magic = Magic(this, "YOUR_PUBLISHABLE_KEY")
09 }
10}
* Authentication APIs that involve UI rendering now require Activity Context.
01val result = magic.auth.loginWithMagicLink(this, configuration)
* StartRelayer(context: Context)
has been removed to improve the developer experience
---
#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.
01open class MagicActivity: AppCompatActivity() {
02
03 override fun onCreate(savedInstanceState: Bundle?) {
04 super.onCreate(savedInstanceState)
05 setContentView(R.layout.activity_main)
06
07 val emailButton: Button = findViewById<Button>(R.id.email_login_btn)
08 emailButton.setOnClickListener{
09 loginWithEmail(it)
10 }
11
12 private fun loginWithEmail(v: View) {
13 val email = findViewById<EditText>(R.id.email_input)
14 val configuration = LoginWithMagicLinkConfiguration(email.text.toString())
15 val result = magic.auth.loginWithMagicLink(this, configuration)
16
17 result.whenComplete { token: DIDToken?, error: Throwable? ->
18 if (error != null) {
19 Log.d("error", error.localizedMessage)
20 }
21 if (token != null && !token.hasError()) {
22 Log.d("login", token.result)
23 startLoggedInActivity()
24 } else {
25 Log.d("login", "Unable to login")
26 }
27 }
28 }
29
30}
For the full implementation of the Response class, please check here.