These integration guides are not official documentation and the Strapi Support Team will not provide assistance with them.
Why Use Magic?
Key benefits of using Magic with Strapi include:
- Enhanced User Experience - Users no longer need to create or remember complex passwords, reducing friction during signup and login. This shift enhances user satisfaction and streamlines the onboarding process. To understand the fundamental differences in authentication methods, consider the topic of authentication vs authorization.
- Improved Security - Magic's authentication uses secure, time-sensitive tokens that offer stronger protection than password-based systems. For additional security measures, you can explore various Strapi authentication plugins to further safeguard your application.
- Simplified Implementation - Magic's SDK makes passwordless authentication straightforward to implement with Strapi. To maximize efficiency, you might want to enhance Strapi with plugins that boost productivity and simplify development.
- Multiple Authentication Methods - Beyond magic links, Magic supports email OTPs, WebAuthn, and various OAuth integrations. If you're looking to implement social logins, check out how to set up social authentication with Strapi.
Need the technical nitty-gritty? Check the Magic documentation for the full scoop.
Why Use Strapi?
Strapi is the leading open-source headless CMS offering features, like customizable APIs, role-based permissions, multilingual support, etc. It simplifies content management and integrates effortlessly with modern frontend frameworks.
Explore the Strapi documentation for more details.
Strapi 5 Highlights
The out-of-the-box Strapi features allow you to get up and running in no time: 1. Single types: Create one-off pages that have a unique content structure. 2. Draft and Publish: Reduce the risk of publishing errors and streamline collaboration. 3. 100% TypeScript Support: Enjoy type safety & easy maintainability 4. Customizable API: With Strapi, you can just hop in your code editor and edit the code to fit your API to your needs. 5. Integrations: Strapi supports integrations with Cloudinary, SendGrid, Algolia, and others. 6. Editor interface: The editor allows you to pull in dynamic blocks of content. 7. Authentication: Secure and authorize access to your API with JWT or providers. 8. RBAC: Help maximize operational efficiency, reduce dev team support work, and safeguard against unauthorized access or configuration modifications. 9. i18n: Manage content in multiple languages. Easily query the different locales through the API. 10. Plugins: Customize and extend Strapi using plugins.
Learn more about Strapi 5 feature.
See Strapi in action with an interactive demo
Setup Strapi 5 Headless CMS
We are going to start by setting up our Strapi 5 project with the following command:
🖐️ Note: make sure that you have created a new directory for your project.
You can find the full documentation for Strapi 5 here.
Install Strapi
npx create-strapi-app@latest server
You will be asked to choose if you would like to use Strapi Cloud we will choose to skip for now.
Strapi v5.6.0 🚀 Let's create your new project
We can't find any auth credentials in your Strapi config.
Create a free account on Strapi Cloud and benefit from:
- ✦ Blazing-fast ✦ deployment for your projects
- ✦ Exclusive ✦ access to resources to make your project successful
- An ✦ Awesome ✦ community and full enjoyment of Strapi's ecosystem
Start your 14-day free trial now!
? Please log in or sign up.
Login/Sign up
❯ Skip
After that, you will be asked how you would like to set up your project. We will choose the following options:
? Do you want to use the default database (sqlite) ? Yes
? Start with an example structure & data? Yes <-- make sure you say yes
? Start with Typescript? Yes
? Install dependencies with npm? Yes
? Initialize a git repository? Yes
Once everything is set up and all the dependencies are installed, you can start your Strapi server with the following command:
cd server
npm run develop
You will be greeted with the Admin Create Account screen.
Go ahead and create your first Strapi user. All of this is local so you can use whatever you want.
Once you have created your user, you will be redirected to the Strapi Dashboard screen.
Publish Article Entries
Since we created our app with the example data, you should be able to navigate to your Article collection and see the data that was created for us.
Now, let's make sure that all of the data is published. If not, you can select all items via the checkbox and then click the Publish button.
Enable API Access
Once all your articles are published, we will expose our Strapi API for the Articles Collection. This can be done in Settings -> Users & Permissions plugin -> Roles -> Public -> Article.
You should have find
and findOne
selected. If not, go ahead and select them.
Test API
Now, if we make a GET
request to http://localhost:1337/api/articles
, we should see the following data for our articles.
🖐️ Note: The article covers (images) are not returned. This is because the REST API by default does not populate any relations, media fields, components, or dynamic zones.. Learn more about REST API: Population & Field Selection.
So, let's get the article covers by using the populate=*
parameter: http://localhost:1337/api/articles?populate=*
Getting Started With Magic
Integrating Magic with Strapi for passwordless authentication creates a hassle-free, secure login experience. Here's your roadmap to integrating Strapi.
Configuring Your Strapi Environment
Make sure your Strapi setup meets these basics:
- Install Node.js (Active LTS versions v18 or v20 are suitable). Understanding the Node.js benefits for Strapi can help you leverage its full potential.
- Have a package manager like npm or yarn ready.
- Create a
.env
file in your Strapi project root:
1MAGIC_SECRET_KEY=your_magic_secret_key_here
2MAGIC_PUBLIC_KEY=your_magic_public_key_here
Grab these keys from the Magic dashboard after creating an account and setting up your application.
Installing and Adjusting Settings
Add the necessary packages and set up Strapi:
- Add the Magic SDK package:
npm install @magic-sdk/admin
# or
yarn add @magic-sdk/admin
- Create a new file at
./config/plugins.js
:
1module.exports = ({ env }) => ({
2 "users-permissions": {
3 config: {
4 providers: {
5 magic: {
6 enabled: true,
7 icon: "magic",
8 key: env("MAGIC_PUBLIC_KEY"),
9 secret: env("MAGIC_SECRET_KEY"),
10 callback: "/auth/magic/callback",
11 scope: ["email"],
12 },
13 },
14 },
15 },
16});
- After installing the Magic plugin, navigate to the PLUGINS section in the sidebar menu of your Strapi admin panel, and configure it by entering the necessary API keys.
Code Implementation
Set up the Magic authentication flow:
- Create
./extensions/users-permissions/strapi-server.js
:
1const { Magic } = require('@magic-sdk/admin');
2
3module.exports = (plugin) => {
4 plugin.services['providers'].register('magic', ({ purest }) => ({
5 async getProfile(ctx) {
6 const magic = new Magic(process.env.MAGIC_SECRET_KEY);
7 const didToken = ctx.query.id_token;
8
9 await magic.token.validate(didToken);
10 const metadata = await magic.users.getMetadataByToken(didToken);
11
12 return {
13 email: metadata.email,
14 username: metadata.email,
15 };
16 },
17 }));
18
19 return plugin;
20};
- Create
POST
Request to Magic
1import { Magic } from "magic-sdk";
2const magic = new Magic("YOUR_MAGIC_PUBLIC_KEY");
3const login = async (email) => {
4 const didToken = await magic.auth.loginWithMagicLink({ email });
5 const response = await fetch(`${STRAPI_URL}/auth/magic/callback`, {
6 method: "POST",
7 headers: { "Content-Type": "application/json" },
8 body: JSON.stringify({ id_token: didToken }),
9 });
10 const data = await response.json();
11 localStorage.setItem("token", data.jwt);
12};
This setup creates a complete authentication flow where:
- Magic verifies the user's email through a magic link.
- The resulting DID token goes to Strapi.
- Strapi validates the token and issues its JWT.
- Your frontend stores this JWT for authenticating API requests.
You've now successfully integrated Magic with Strapi, giving your users a modern login experience while tapping into Strapi's content management powers. By leveraging these Strapi developer benefits, you can enhance your application's functionality and deliver a superior experience to your users.
Strapi Open Office Hours
If you have any questions about Strapi 5 or just would like to stop by and say hi, you can join us at Strapi's Discord Open Office Hours Monday through Friday at 12:30 pm - 1:30 pm CST: Strapi Discord Open Office Hours
For more details, visit the Strapi documentation and Magic documentation.