Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Custom userProfile for Oauth2 to support login with Keycloak; Include phone to customer when logging in store #155

Merged
merged 2 commits into from
Mar 28, 2024

Conversation

sl1mpshady
Copy link
Contributor

Currently, Oauth2 is allowing user to login with keycloak, but the generated profile returned by Keycloak doesn't follow the Oauth2 User Profile format so it returns Your oauth2 account does not contains any email and cannot be used error.
With this change, we can pass an optional option to oauth2 named parseProfile which should return a normalized json profile that follows the Oauth2 User Profile format

{
    resolve: "medusa-plugin-auth",
    /** @type {import('medusa-plugin-auth').AuthOptions} */
    options: [
      {
        type: "oauth2",
        strict: "none",
        // or "none" or "store" or "admin"
        identifier: "oauth2",
        authorizationURL: OAuth2AuthorizationURL,
        tokenURL: OAuth2TokenURL,
        clientID: OAuth2ClientId,
        clientSecret: OAuth2ClientSecret,
        scope: OAuth2Scope.split(","),
        admin: {
          callbackUrl: `${BACKEND_URL}/admin/auth/oauth2/cb`,
          failureRedirect: `${ADMIN_URL}/login`,
          successRedirect: `${ADMIN_URL}/`,
        },
        store: {
          callbackUrl: `${BACKEND_URL}/store/auth/oauth2/cb`,
          failureRedirect: `${STORE_URL}/login`,
          successRedirect: `${STORE_URL}/`,
        },
        parseProfile: (json) => {
          const profile = {
            provider: "keycloak",
            id: json.sub,
            username: json.preferred_username,
            displayName: json.name,
            email: json.email,
            name: {
              familyName: json.family_name,
              givenName: json.given_name,
            },
            emails: json.email
              ? [
                  {
                    value: json.email,
                  },
                ]
              : [],
            _json: json,
            phoneNumbers: json.phone_number
              ? [
                  {
                    value: json.phone_number ?? "",
                  },
                ]
              : [],
          };

          return profile;
        },
      },
    ],
  }

Copy link

vercel bot commented Mar 26, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
medusa-plugins ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 26, 2024 6:32pm

@adrien2p
Copy link
Owner

adrien2p commented Mar 28, 2024

Thanks a lot for this contribution 🙏

@adrien2p adrien2p merged commit 859adb7 into adrien2p:main Mar 28, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants