Installing and Getting Started with the Magic SDK

Magic Staff · August 10, 2020
Download this example and get started in seconds:
npx make-magic --template hello-world
Installing and Getting Started with the Magic SDK

A Hello World tutorial to quickly get familiar with Magic. 🚀

👉 What you'll build: Hello World demo

#Get Started

#0. Get the template code

To follow along with the tutorial, use our Hello World template in CodeSandBox.

#1. Install Magic SDK

Install the Magic SDK to the Hello World template with a script tag. You can copy and paste the below code to the CodeSandBox editor after the comment:

HTML
01<!-- 1. Install Magic SDK -->
02<script src="https://auth.magic.link/sdk"></script>

Or install Magic SDK with Yarn or NPM.

#2. Initialize Magic Instance

Initialize a Magic instance with a Publishable API Key with this code:

Javascript
01/* 2. Initialize Magic Instance */
02const magic = new Magic('YOUR_LIVE_PUBLISHABLE_API_KEY');

You need to sign up to Magic Dashboard to get your own API keys.

Replace the 'YOUR_LIVE_PUBLISHABLE_API_KEY' string with your "Publishable API Key" from the Magic Dashboard:

#3. Implement Render Function

Let's add the logic on what should display when:

  1. Users are not logged in,
  2. Users are logged in, and
  3. Users have been redirected to your app's "callback" route

Copy and paste this code:

Javascript
01/* 3. Implement Render Function */
02
03
04 const render = async () => {
05    let html = '';
06     /*
07      For this tutorial, all we need to worry about is simply the response we get after a successful OTP login flow
08     */
09    const isLoggedIn = await magic.user.isLoggedIn();
10
11    /* Show login form if user is not logged in */
12    html = `
13        <h1>Please sign up or login</h1>
14        <form onsubmit="handleLogin(event)">
15         <input type="email" name="email" required="required" placeholder="Enter your email" />
16          <button type="submit">Send</button>
17        </form>
18      `;
19
20    if (isLoggedIn) {
21      /* Get user metadata including email */
22      const userMetadata = await magic.user.getMetadata();
23      html = `
24          <h1>Current user: ${userMetadata.email}</h1>
25          <button onclick="handleLogout()">Logout</button>
26        `;
27    }
28
29    document.getElementById('app').innerHTML = html;
30 };

#4. Implement Login Handler

You can now implement user login without needing to write any backend code! Copy and paste this code:

Javascript
01/* 4. Implement Login Handler */
02 const handleLogin = async (e) => {
03    e.preventDefault();
04    const email = new FormData(e.target).get('email');
05     if (email) {
06        /* One-liner login with email OTP 🤯 */
07        await magic.auth.loginWithEmailOTP({ email });
08        render();
09     }
10  };

#5. Implement Logout Handler

To wrap it up, let's implement user logout as well. Copy and paste this code:

Javascript
01/* 5. Implement Logout Handler */
02const handleLogout = async () => {
03  await magic.user.logout();
04  render();
05};

#6. Done

You've completed the tutorial! Here's a working demo of the tutorial we just went over.

#What's next?

#Use Magic with existing tools

#Customize your Magic flow

You can customize the login experience using your own UI instead of Magic's default one and/or customize the magic link email with your brand. Learn how to customize.

Let's make some magic!