Открыта бета 1С:ПредприятияОставить заявку
Supertokens logo

Supertokens

Плагин провайдера аутентификации SuperTokens для Medusa v2. Включает аутентификацию через OAuth2/OpenID Connect с SuperTokens в роли провайдера удостоверений.

npm install @antibusy/medusa-supertokens
Категория
Создано
Регионы
Во всех регионах
Сервис
Версия
0.0.4
Последнее обновление
2 месяца назад
Ежемесячные загрузки
Загрузка данных

@patiparnne/medusa-supertokens

A Medusa v2 authentication provider plugin that enables session-based authentication using SuperTokens as the identity provider. Compatible with SuperTokens free tier (no OAuth2 license required).

Features

  • Direct session token verification with SuperTokens Core
  • Works with SuperTokens free tier (no paid features required)
  • Supports all SuperTokens authentication methods (Email/Password, Passwordless/OTP, Social Login)
  • Legacy CRM profile integration (refreshes profile data on every auth)
  • Email verification enforcement (optional)
  • Seamless integration with Medusa's auth module

Architecture Overview

Port Configuration (His & Her Project)

ServicePortDescription
Storefront3000Next.js storefront app
SuperTokens Core3567SuperTokens database/core service
hisher-auth Backend4002SuperTokens backend API with OTP/profile routes
hisher-auth Frontend4003SuperTokens demo UI (optional, for testing)
Medusa Backend9000Medusa e-commerce API

Note: The hisher-auth frontend (port 4003) is a SuperTokens demo UI showing buttons like "Continue to Store", "Call API", "Logout". It's NOT an admin dashboard - it's designed for authenticated users to test the session. You don't need to run it for production.

Prerequisites

  • Medusa v2.x
  • SuperTokens Core running (self-hosted via Docker or managed)
  • SuperTokens Backend API (hisher-auth) running
  • Storefront configured to redirect to hisher-auth for login

Installation

Configuration

1. Configure Medusa

Add the plugin to your :

2. Environment Variables

Add these to your file:

Usage

Authentication Flow

  1. User initiates login: Storefront redirects to hisher-auth login page
  2. User authenticates: Via phone OTP, email/password, or social login (LINE, Google)
  3. Session created: SuperTokens creates a session with access token
  4. Token sent to Medusa: Storefront calls with the token
  5. Profile enriched: Plugin calls on hisher-auth to get user data
  6. Customer created/linked: Medusa creates or retrieves the customer
  7. Medusa JWT returned: Storefront receives Medusa JWT for authenticated requests

API Endpoints

Authenticate with session token:

Or via Authorization header:

Response (success):

Legacy Profile Integration

On every authentication, the plugin calls on hisher-auth to:

  • Verify the SuperTokens JWT token
  • Fetch user info and metadata from SuperTokens
  • Return any legacy CRM profile data stored in SuperTokens metadata

This ensures:

  • Real email addresses from legacy CRM are used (instead of generated phone@domain.com)
  • Customer first_name and last_name are populated
  • Legacy member_id is preserved for reference

Options Reference

OptionTypeRequiredDefaultDescription
stringYes-SuperTokens Core URL (e.g., http://localhost:3567)
stringYes-hisher-auth Backend API URL (e.g., http://localhost:4002)
stringNoAPI base path for SuperTokens endpoints
stringNo-SuperTokens Core API key (for direct Core access)
stringNo-Redirect URL after login (storefront callback)
booleanNoRequire email verification before allowing auth

Troubleshooting

"Invalid or expired session token"

  • Ensure SuperTokens Core is running on port 3567
  • Ensure hisher-auth backend is running on port 4002
  • Check that the access token hasn't expired
  • Verify the API key matches (if configured)

"Failed to retrieve user information"

  • The user ID from the session doesn't exist in SuperTokens
  • Check SuperTokens Core logs for errors
  • Check hisher-auth backend logs

Customer created with generated email instead of real email

  • Legacy profile data is fetched during OTP login flow
  • Ensure the user logged in via phone OTP (which fetches legacy profile)
  • Check that returns metadata with email

Connection refused errors

  • Verify is correct and SuperTokens Core is running
  • Verify is correct and hisher-auth backend is running
  • Check network/firewall settings

License

MIT