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.Getting Started
TheMagic
class is the entry-point to the Magic SDK. It must be instantiated with a Magic publishable key. The minimum Android version supported by the Android Magic SDK is version 24.
Installation
Magic SDK is available inmavenCentral
. Simply add the following to your build.gradle
and settings.gradle
, respectively.
Kotlin
Kotlin
Constructor
Magic()
Parameter | Type | Definition |
context | Context | Android Context object |
apiKey | String | Your publishable API Key retrieved from the Magic Dashboard. |
ethNetwork? | EthNetwork | An enum with values Mainnet and Goerli . Use customNodeConfiguration for other network options.Defaults to Ethereum Mainnet. |
customNodeConfiguration? | CustomNodeConfiguration | A custom node configuration with the following shape: rpcUrl (String): A URL pointing to your custom Ethereum Node. chainId? (Number): Some Node infrastructures require you to pass an explicit chain ID. If you are aware that your Node requires this configuration, pass it here as an integer. |
locale? | String | Customize the language of Magic’s modal, email and confirmation screen. See Localization for more. |
Initialization
Kotlin
Auth Module
The Auth Module and its members are accessible on the Magic SDK instance by theauth
property.
loginWithSMS
Authenticate a user passwordlessly using a one-time code sent to the specified phone number. List of Currently Blocked Country Codes Argumentscontext
(Context): Global information about application environmentconfiguration
(LoginWithSMSConfiguration):phoneNumber
(str): The user phone number to log in with
DIDToken: Response<String>()
- The function resolves upon authentication request success and rejects with a specific error code if the request fails. The resolved value is a Decentralized ID token with a default 15-minute lifespan.
Kotlin
loginWithEmailOTP
Authenticate a user passwordlessly using an email one-time code sent to the specified user’s email address.
Arguments
context
(Context): Global information about application environmentconfiguration
(LoginWithEmailOTPConfiguration):email
(str): The user email to log in with
DIDToken: Response<String>()
- The function resolves upon authentication request success and rejects with a specific error code if the request fails. The resolved value is a Decentralized ID token with a default 15-minute lifespan.
Kotlin
Wallet Module
The Wallet Module and its members are accessible on the Magic SDK instance by thewallet
property.
connectWithUI
Renders a simple login form UI to collect the user’s email address and authenticate them passwordlessly using a one-time passcode (OTP) sent to their email address they input.
Arguments
context
(Context): Global information about application environment
- A
promiEvent
which returns anString[]
when resolved: An array of user accounts that are connected, with the first element being the current public address of the user. You can read more on PromiEvents here.
Kotlin
showUI
Displays the fully navigable wallet to the user that adheres to the toggled configurations on your developer dashboard’s Widget UI tab. This is only supported for users who login with email or Google. User must be signed in for this method to return or else it will throw an error.
Arguments
context
(Context): Global information about application environment
Promise
which resolves when the user closes the window
.on()
handler to catch the disconnect
event emitted when the user logs out from the wallet widget.
Example
Kotlin
User Module
The User Module and its members are accessible on the Magic SDK instance by theuser
property.
updateEmail
Initiates the update email flow that allows a user to change to a new email.
Arguments
-
context
(Context): Global information about application environment -
configuration
(UpdateEmailConfiguration):email
(str): The user email to update withshowUI
(boolean): Iftrue
, show an out-of-the-box pending UI while the request is in flight
UpdateEmailResponse: Response<Boolean>()
- TheCompletable
resolves with a true boolean value if update email is successful and rejects with a specific error code if the request fails
Kotlin
getIdToken
Generates a Decentralized Id Token which acts as proof of authentication to resource
servers.
Arguments
context
(Context): Global information about application environmentconfiguration
(GetIdTokenConfiguration):lifespan
(number): will set the lifespan of the generated token. Defaults to 900s (15 mins).
PromiEvent<string>
- Base64-encoded string representation of a JSON tuple representing[proof, claim]
Kotlin
generateIdToken
Generates a Decentralized Id Token with optional serialized data.
Arguments
configuration
(GenerateIdTokenConfiguration):lifespan
(number): will set the lifespan of the generated token. Defaults to 900s (15 mins).attachment
(str): will set a signature of serialized data in the generated token. Defaults to"none"
.
GenerateIdTokenResponse: Response<String>()
- Base64-encoded string representation of a JSON tuple representing[proof, claim]
Kotlin
getMetadata
Retrieves information for the authenticated user.
Arguments
- None.
GetMetadataResponse: Response<UserMetadataResponse>()
- TheCompletable
containing the issuer, email, phone number, and cryptographic public address of the authenticated user.issuer
(str): The Decentralized ID of the user. In server-side use-cases, we recommend this value to be used as the user ID in your own tables.email
(str): Email address of the authenticated userphoneNumber
(str): Phone number of the authenticated userpublicAddress
(str): The authenticated user’s public address (a.k.a.: public key). Currently, this value is associated with the Ethereum blockchain.
Kotlin
isLoggedIn
Checks if a user is currently logged in to the Magic SDK.
Arguments
- None
IsLoggedInResponse: Response<Boolean>()
: A boolean value indicating if a user is logged in
Kotlin
logout
Logs out the currently authenticated Magic user.
Arguments
- None
LogoutResponse: Response<Boolean>()
: A boolean value indicating if a user has been logged out.
Kotlin
showSettings
Displays an iframe with the current user’s settings. Allows for users to update their email address, enable multi-factor authentication, and add a recovery factor.
Access to MFA and account recovery require paid add-ons.
page?
(String): Optional argument to deeplink to a specific page ('mfa' | 'update-email' | 'recovery'
)
Promise
which resolves when the user closes the window
Kotlin
updatePhoneNumber
Initiates the update phone number flow that allows a user to change their phone number.
Arguments
-
context
(Context): Global information about application environment -
configuration
:phoneNumber
(str): The user phone number to update withshowUI
(boolean): Iftrue
, show an out-of-the-box pending UI while the request is in flight
PromiEvent<boolean>
: The promise resolves with a true boolean value if update email is successful and rejects with a specific error code if the request fails
Kotlin
recoverAccount
Initiates the account recovery flow that allows a user to recover their account using their email address.
Arguments
email
(str): The user email address to recover account with
PromiEvent<boolean>
: The promise resolves with a true boolean value if the account recovery is successful and rejects with a specific error code if the request fails
Kotlin
revealPrivateKey
Displays an iframe revealing the current user’s private key. Allows for users to take their private key to another wallet. Neither Magic nor the developer can see this key; only the end user can.
Arguments
- None
Promise
which resolves when the user closes the window
Kotlin