medusajs / medusa

The world's most flexible commerce platform.
https://medusajs.com
MIT License
26.22k stars 2.66k forks source link

[Bug]: Medusa Admin Login Issue #10365

Open ABAGDevelopment opened 2 days ago

ABAGDevelopment commented 2 days ago

Package.json file

{
  "name": "medusa-starter-default",
  "version": "0.0.1",
  "description": "A starter for Medusa projects.",
  "author": "Medusa (https://medusajs.com)",
  "license": "MIT",
  "keywords": [
    "sqlite",
    "postgres",
    "typescript",
    "ecommerce",
    "headless",
    "medusa"
  ],
  "scripts": {
    "build": "medusa build",
    "seed": "medusa exec ./src/scripts/seed.ts",
    "start": "medusa start",
    "dev": "medusa develop",
    "test:integration:http": "TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
    "test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit",
    "test:unit": "TEST_TYPE=unit NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit"
  },
  "dependencies": {
    "@medusajs/admin-sdk": "latest",
    "@medusajs/cli": "latest",
    "@medusajs/framework": "latest",
    "@medusajs/medusa": "latest",
    "@mikro-orm/core": "5.9.7",
    "@mikro-orm/knex": "5.9.7",
    "@mikro-orm/migrations": "5.9.7",
    "@mikro-orm/postgresql": "5.9.7",
    "awilix": "^8.0.1",
    "pg": "^8.13.0"
  },
  "devDependencies": {
    "@medusajs/test-utils": "latest",
    "@mikro-orm/cli": "5.9.7",
    "@swc/core": "1.5.7",
    "@swc/jest": "^0.2.36",
    "@types/jest": "^29.5.13",
    "@types/node": "^20.0.0",
    "@types/react": "^18.3.2",
    "@types/react-dom": "^18.2.25",
    "jest": "^29.7.0",
    "prop-types": "^15.8.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "ts-node": "^10.9.2",
    "typescript": "^5.6.2",
    "vite": "^5.2.11"
  },
  "engines": {
    "node": ">=20"
  }
}

Node.js version

v21

Database and its version

Postgres 16.2

Operating system name and version

Windows 11

Browser name

Chrome

What happended?

Admin User invite and login functionality not working and causing errors

Below is the traceCreating a new user : - Below are the steps

• Invite a user and enter email address • Copy the invite link open in the browser and enter details and click create account, below message appears

Below is the response from medusa API

http: POST /auth/user/emailpass/register ← http://localhost:9000/app/invite?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6Imludml0ZV8wMUpEUk1XM0sxQkJYRks5QlY0TThOWjBSNSIsImVtYWlsIjoiYWRtaW5AeW9wbWFpbC5jb20iLCJpYXQiOjE3MzI3NzI3NjEsImV4cCI6MTgxOTE3Mjc2MSwianRpIjoiZjUxMjZhMWItNGJlZS00ZTNlLWI2NjQtZGIwZGNlNWI2ZTIxIn0.yrq-qWHwZ1Etwp6UusBNGuJwYuUTLKhBSZCWYvxKpVY (200) - 103.347 ms error: The user is already authenticated and cannot accept an invite. MedusaError: The user is already authenticated and cannot accept an invite. at POST (C:\Projects\Medusa\basicsetup\my-medusa-store\node_modules\@medusajs\medusa\src\api\admin\invites\accept\route.ts:15:11) at C:\Projects\Medusa\basicsetup\my-medusa-store\node_modules\@medusajs\utils\src\common\wrap-handler.ts:17:20 at Layer.handle [as handle_request] (C:\Projects\Medusa\basicsetup\my-medusa-store\node_modules\express\lib\router\layer.js:95:5) at next (C:\Projects\Medusa\basicsetup\my-medusa-store\node_modules\express\lib\router\route.js:149:13) at Route.dispatch (C:\Projects\Medusa\basicsetup\my-medusa-store\node_modules\express\lib\router\route.js:119:3) at Layer.handle [as handle_request] (C:\Projects\Medusa\basicsetup\my-medusa-store\node_modules\express\lib\router\layer.js:95:5) at C:\Projects\Medusa\basicsetup\my-medusa-store\node_modules\express\lib\router\index.js:284:15 at Function.process_params (C:\Projects\Medusa\basicsetup\my-medusa-store\node_modules\express\lib\router\index.js:346:12) at next (C:\Projects\Medusa\basicsetup\my-medusa-store\node_modules\express\lib\router\index.js:280:10) at next (C:\Projects\Medusa\basicsetup\my-medusa-store\node_modules\express\lib\router\route.js:141:14) { __isMedusaError: true, type: 'invalid_data', code: undefined, date: 2024-11-28T06:01:52.872Z }

The user is already authenticated and cannot accept an invite. : - User table does not have any entry for the mentioned user

So above message returned from the API is misleading

Attempt 2 with the invite link

http: GET /app/index.css ← http://localhost:9000/app/entry.tsx (200) - 2.296 ms error: Identity with email already exists MedusaError: Identity with email already exists at POST (C:\Projects\Medusa\basicsetup\my-medusa-store\node_modules\@medusajs\medusa\src\api\auth[actor_type][auth_provider]\register\route.ts:52:9) at processTicksAndRejections (node:internal/process/task_queues:95:5) at async C:\Projects\Medusa\basicsetup\my-medusa-store\node_modules\@medusajs\utils\src\common\wrap-handler.ts:17:14 { __isMedusaError: true, type: 'unauthorized', code: undefined, date: 2024-11-28T06:13:24.488Z }

New entry in auth_identity table with app_metadata as null

Now regardless of number of attempts to create admin user, Application will keep throwing Server Error. Trace

Expected behavior

User Should be able to seemlessly login

Actual behavior

Admin User invite and login functionality not working and causing errors

Link to reproduction repo

https://github.com/medusajs/medusa

olivermrbl commented 2 days ago

@ABAGDevelopment, any chance you can provide the exact steps to reproduce the error?

The error you are seeing on the first attempt to accept an invite is thrown in case the HTTP request already carries authentication, potentially from a separate, earlier session. I'd like to understand how you managed to reach this state, as it should generally not be possible in Medusa Admin.

ABAGDevelopment commented 2 days ago

@olivermrbl : - Below are the steps. • Invite a user and enter email address • Copy the invite link open in the browser and enter details and click create account, " UI Message the invite is invalid or expired", Api response in mentioned in the description above

olivermrbl commented 2 days ago

@ABAGDevelopment, can I get you to ensure you are signed out of the dashboard with your other user (the one that invited the new user)? Otherwise, the authentication context from that session will be part of the HTTP request to accept the token.