Last active
November 19, 2025 04:27
-
-
Save leosantosw/8f82dee6f984444ee0a1c46bebed64f8 to your computer and use it in GitHub Desktop.
Implement the FusionAuth authentication provider in Strapi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // src/index.js | |
| 'use strict'; | |
| const axios = require('axios'); | |
| module.exports = { | |
| register({ strapi }) { | |
| strapi | |
| .plugin("users-permissions") | |
| .service("providers-registry") | |
| .add("fusionauth", { | |
| icon: "", | |
| enabled: true, | |
| grantConfig: { | |
| key: "", | |
| secret: "", | |
| callback: `http://localhost:1337/auth/fusionauth/callback`, | |
| scope: "openid profile email", | |
| authorize_url: "http://localhost:9011/oauth2/authorize", | |
| access_url: "http://localhost:9011/oauth2/token", | |
| oauth: 2, | |
| }, | |
| async authCallback({ accessToken }) { | |
| try { | |
| console.log("accessToken: ", accessToken); | |
| const { data } = await axios.get("http://localhost:9011/oauth2/userinfo", { | |
| headers: { | |
| Authorization: `Bearer ${accessToken}`, | |
| }, | |
| }); | |
| const tokenParts = accessToken.split('.'); | |
| const payload = JSON.parse(Buffer.from(tokenParts[1], 'base64').toString()); | |
| console.log('Fusion roles: ', payload.roles); | |
| const strapiRole = await strapi.query('plugin::users-permissions.role').findOne() | |
| console.log('Strapi role: ', strapiRole); | |
| return { | |
| username: data.preferred_username || data.email.split("@")[0], | |
| email: data.email, | |
| birthdate: data.birthdate, | |
| givenName: data.given_name, | |
| familyName: data.family_name, | |
| preferredUsername: data.preferred_username, | |
| }; | |
| } catch (error) { | |
| console.error("Erro no AuthCallback do FusionAuth:", error.response?.data || error.message); | |
| throw new Error(`FusionAuth authentication failed: ${error.message}`); | |
| } | |
| }, | |
| }); | |
| }, | |
| bootstrap(/*{ strapi }*/) {}, | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment