Magic SMS for Android

Magic SMS for Android

New to Magic?

Create a fully-functional Magic auth demo in minutes.

Build a demo

#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.

#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:4.0.0'
    implementation 'org.web3j:core:4.8.8-android' 
}

repositories {
    mavenCentral()
}

Sync the project with new dependencies settings.

important

0.x has been deprecated. Please follow the new instantiation method to access the latest feature

#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.

class MagicDemoApp : Application() {
    lateinit var magic: Magic

    override fun onCreate() {
        magic = Magic(this, "YOUR_PUBLISHABLE_KEY")
        super.onCreate()
    }
}
open class MagicActivity: AppCompatActivity() {

    lateinit var magic: Magic

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        magic = (applicationContext as MagicDemoApp).magic
    }
}

Or, just simply instantiate Magic in your login activity and use Fragments to render the contents in the same activity.

open class LoginActivity: AppCompatActivity() {

    lateinit var magic: Magic

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        magic = Magic(this, "YOUR_PUBLISHABLE_KEY")
    }
}

#Make a request

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.

open class MagicActivity: AppCompatActivity() {

    lateinit var magic: Magic

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        magic = (applicationContext as MagicDemoApp).magic

        val smsButton: Button = findViewById<Button>(R.id.sms_login_btn)
        smsButton.setOnClickListener {
            loginWithSMS(it)
        }
    }

   private fun loginWithSMS(v: View) {
        val phoneNumber = findViewById<EditText>(R.id.phone_number_input)
        val configuration = LoginWithSMSConfiguration(phoneNumber.text.toString())
        val result = magic.auth.loginWithSMS(this, configuration)
        result.whenComplete { token: DIDToken?, error: Throwable? ->
            if (error != null) {
                Log.d("error", error.localizedMessage)
            }
            if (token != null && !token.hasError()) {
                Log.d("login", token.result)
                startLoggedInActivity()
            } else {
                Log.d("login", "Unable to login")
            }
        }
    }
}

For the full implementation of the Response class, please check here.

Server-side SDKs?

Integrate Magic with your existing backend.

Did you find what you were looking for?

Did you find what you were looking for?