Skip to main content
stars
Book with padlock and SSO logo tokens, Cryptr bookmark included.

Magic Link

Magic Links work by integrating a single-use, time-limited Token in the URL of an access link sent by e-mail. This token is associated with the user's account and is verified by Cryptr when the link is accessed, before redirecting the authorized user to your application. With this integration, you can embed Magic Link authentication in your code in just two API requests.

  • Quickstart
  • 15 min

In this guide, we’ll walk you through implementing Magic Link authentication in your application.

Before starting

Create your free Cryptr account now, and you will have the three elements needed for this guide.

  • API Key: You will receive a client_id and a client_secret. Read our guide to learn how to authenticate with these elements to use the Cryptr API.
  • Organization: You will create your first organization, which could be your customer or even yourself for the first test. Learn more about Organization.
  • Redirection: A redirect, also called a redirect_uri, is the address your user will be sent to after successful authentication.

Cryptr Dashboard - Home Page

Enable Magic Link authentication

The Magic Link authentication method is activated by default for all new Organizations. You can manage its activation from your Cryptr Dashboard.

Activate Magic Links for an Organization

You can activate Magic Links directly from the page of one of your Organizations on your Cryptr dashboard.

Cryptr Dashboard - Activate Magic Link Connection

What we’re going to build together

There are three steps to completing a Magic Link Challenge process for an Organization end user (such as an employee of your customer).

  1. From your BackEnd, you need to request a MagicLinkChallenge with the desired redirect_uri. This is the address of your application where you want Cryptr to redirect the user after authentication. This API call is protected by your Cryptr API key, so you must never make it from your FrontEnd.
  2. The MagicLinkChallenge includes a URL containing a token that allows Cryptr to authenticate the end user. This URL is a one-time use, for a limited time only.
  3. After successful Magic Link authentication, the user is redirected to the redirect_uri, with a query params authorization code-named code. This allows you to retrieve the final Json Web Tokens (JWT).

To request a Magic Link challenge, you’ll need

  • The user’s e-mail address
  • The redirect_uri: this is the address of your application (URL) where you want Cryptr to redirect the user after authentication.
You must first authorize redirect_uri

To use Cryptr’s authentication strategies, you must first specify the redirections to be authorized (redirect_uri) from your Cryptr dashboard. If you have not specified a redirection in your Challenge request, the default redirect will be used. Cryptr encourages you to create your first redirection for your development environment (sandbox) when you create your account.

Method 1: Emails sent by Cryptr

curl -X POST '${cryptr_service_url}/api/v2/magic-link-challenge' \
-d user_email="john@misapret.com" \
-d redirect_uri="https//example-of_url.com/welcome-back-user"

This request will send an e-mail to the user. The user must click on the link to authenticate before being redirected to the desired redirect_uri.

Method 2: Send your own emails

curl -X POST '${cryptr_service_url}/api/v2/magic-link-challenge' \
-d user_email="john@misapret.com" \
-d redirect_uri="https//example-of_url.com/welcome-back-user" \
-d send_email="false"

This request will provide you with a link to send by e-mail to the user. As before, the user must click on the link to authenticate before being redirected to the desired redirect_uri.

Specify an Organization

You can also target the appropriate Magic Link connection using the organization’s domain. This is useful if you have several organizations using the same e-mail domains.

Request the Magic Link challenge using an organization’s domain.
curl -X POST '${cryptr_service_url}/api/v2/magic-link-challenge' \
-d user_email="john@misapret.com" \
-d org_domain="misapret" \
-d redirect_uri="https//example-of_url.com/welcome-back-user"

2. Get tokens after successful authentication

Once the user has successfully authenticated via their Magic Link, they will be redirected to the redirect_uri provided beforehand. Cryptr will then supply a code authorization code via query params to retrieve the final tokens: the access_token and the id_token. The latter contains the user data retrieved during authentication.

# ... your user finishes their Magic Link authentication, 

# In your app, the user is redirected to your service,
# via the "redirect_uri" provided when you created the challenge (or the default one),
# with the query parameter "code" that we need to fetch the tokens.

curl -X POST '${cryptr_service_url}/oauth/token' \
-d code={code} \
-d grant_type="authorization_code"

# if result.success is true, then
## 1. you get the result.access_token,
## 2. and result.id_token, which contains signed user data.
# else
## your user is unauthorized
# end

We will respond to this request with the user’s Json Web Token (JWT), an access_token and an id_token. The latter contains the user’s identity data.

What's next

You can also consult our API Reference to perform these actions via API Rest.