Magic Links
On this page, we dive into Magic Links and how you can use them to allow users a passwordless experience when they use your product.
Magic Links work in the same way to Account Activation Links, but instead of activating an account, they allow users to log in to your product. If you want to automatically verify and log a user into their account, you should use Magic Links, otherwise, you should use Account Activation Links.
Creating a Magic Link
Similarily to Account Activation Links, you create a Magic Link by sending a POST request to https://api.devrift.co
. The only difference is that the api_type
parameter must be set to magic_create
.
Below we show you how to do this using our PHP SDK.
// Import the DevRIFT SDK
use DevRift\DevRift;
// Import the Magic Make class
use DevRift\Magic\Make;
// snip...
// Set your Secret Key
DevRIFT::setApiKey("sk_test_foobar...");
// Create a Magic Link with YOUR_USERS_EMAIL
Make::magic(YOUR_USERS_EMAIL);
Verifying a Magic Link
Similarily to Account Activation Links, you verify a Magic Link by sending a POST request to https://api.devrift.co
. The only difference is that the api_type
parameter must be set to magic_verify
.
Below we show you how to do this using our PHP SDK.
// Import the DevRIFT SDK
use DevRift\DevRift;
// Import the Magic Verify class
use DevRift\Magic\Verify;
// snip...
// Set your Secret Key
DevRIFT::setApiKey("sk_test_foobar...");
// Verifies the Magic Link and returns the email address
$email = Verify::magic();
Our PHP SDK collects the GET Request Data vl
and sr
. You must make sure that your application does not interfere with these variables.
If you interfere with these variables, you will not be able to verify the Magic Link.
Checking a Magic Link
Another reason to use Magic Links is to check if the Magic Link is valid, you can then use this validity property to show a message to the user. For example, you could ask them whether they would like to continue on the device they opened the link on, or if they'd rather log on to their account on the device the link was sent from.
To check a Magic Link, you send a POST request to https://api.devrift.co
. The only difference is that the api_type
parameter must be set to magic_check
.
Below we show you how to do this using our PHP SDK.
// Import the DevRIFT SDK
use DevRift\DevRift;
// Import the Magic Check class
use DevRift\Magic\Check;
// snip...
// Set your Secret Key
DevRIFT::setApiKey("sk_test_foobar...");
// Checks the Magic Link and returns the email address
$email = Check::magic();
If the Magic Link is invalid, the email
property will be null
. You should check if this is the case.