Unity API reference

Unity API reference

note

It's recommended to apply this plugin in Unity 2021.3.3f1 or newer. Developers may build 2d Unity mobile Apps both in iOS and Android.

WebGL is currently not supported, but you may use magic-js in a JS context to enable passwordless authentication flow.

#Overview

The Magic SDK for Unity is your entry-point to secure, passwordless authentication for your mobile app. This guide will cover some important topics for getting started with the Magic Unity SDK and to make the most of Magic's features.

#Getting Started

The Magic class is the entry-point to the Magic SDK. It must be instantiated with a Magic publishable key.

#Constructor

#Magic

Parameter

Type

Definition

apiKey

String

Your publishable API Key retrieved from the Magic Dashboard.

network

EthNetwork

A representation of the connected Ethereum network (one of: mainnet or goerli).

locale?

String

Customize the language of Magic's modal, email and confirmation screen. See Localization for more.

config?

CustomNodeConfiguration

A custom Ethereum 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.

macCanvas?

GameObject

Your application’s primary canvas. Must be non-null for macOS and macOS editor support.

#Initialization

C#
01using MagicSDK;
02
03public class MagicUnity : MonoBehaviour
04{
05    void Start()
06    {
07        Magic magic = new Magic("PUBLISHABLE_API_KEY");
08        Magic.Instance = magic;
09    }
10}

#Auth Module

The Auth Module and its members are accessible on the Magic SDK instance by the auth property.

C#
01using MagicSDK;
02
03magic.Auth;
04magic.Auth.LoginWithEmailOTP;

#LoginWithEmailOTP

Authenticate a user passwordlessly using an email one-time code sent to the specified user's email address.

#Public Methods

Methods
async Task<string> LoginWithEmailOtp(string email)
  • email The user email to log in with

#Returns

  • DIDToken: Task<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.

#User Module

The User Module and its members are accessible on the Magic SDK instance by the user property.

C#
01import MagicSDK
02
03var magic = Magic.Instance
04
05magic.User
06magic.User.GetIdToken
07magic.User.GenerateIdToken
08magic.User.GetMetadata
09magic.User.UpdateEmail
10magic.User.IsLoggedIn
11magic.User.Logout

#UpdateEmail

Initiates the update email flow that allows a user to change to a new email

#Public Methods

Methods
public async Task<bool> UpdateEmail()
  • email The user email to update with
  • showUI If true, show an out-of-the-box pending UI while the request is in flight

#Returns

  • Task<Boolean>: The Completable resolves with a true boolean value if update email is successful and rejects with a specific error code if the request fails.

#GetIdToken

Generates a Decentralized Id Token which acts as a proof of authentication to resource servers.

#Public Methods

Methods
public async Task<string> GetIdToken(int lifespan = 900)
  • lifespan?: will set the lifespan of the generated token. Defaults to 900s (15 mins)

#Returns

  • GetIdTokenResponse: Task<String>: The Completable resolves with a true boolean value if update email is successful and rejects with a specific error code if the request fails.

#GenerateIdToken

Generates a Decentralized Id Token with optional serialized data.

#Public Methods

Methods
public async Task<string> GenerateIdToken(int lifespan = 900, string attachment = "none")
  • lifespan : will set the lifespan of the generated token. Defaults to 900s (15 mins)
  • attachment : will set a signature of serialized data in the generated token. Defaults to "none"

#Returns

  • Task<String>: Base64-encoded string representation of a JSON tuple representing [proof, claim]

#GetMetadata

Retrieves information for the authenticated user.

#Public Methods

Methods
public async Task<UserMetadata> GetMetadata()

#Returns

  • Task<UserMetadata>: The UserMetadata containing the issuer, email and cryptographic public address of the authenticated user
C#
01[Serializable]
02    public sealed class UserMetadata
03    {
04        [CanBeNull] public string issuer;
05        [CanBeNull] public string publicAddress;
06        [CanBeNull] public string email;
07    }
  • issuer : 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 : Email address of the authenticated user
  • publicAddress: The authenticated user's public address (a.k.a.: public key). Currently, this value is associated with the Ethereum blockchain.

#IsLoggedIn

Checks if a user is currently logged in to the Magic SDK.

#Public Methods

Methods
public async Task<bool> IsLoggedIn()

#Returns

  • Task<Boolean>

#Logout

Logs out the currently authenticated Magic user

#Public Methods

Methods
public async Task<bool> Logout()

#Returns

  • Task<Boolean>

#Resources