#401 Unauthorized for admin routes
50 messages · Page 1 of 1 (latest)
You have to call this endpoint: https://docs.medusajs.com/v1/api/admin#auth_posttoken
POST /admin/auth/token
Hi
I have the same issue with medusa v2 in prod mode
it looks like the logi success
but the admin/users/me saying 401
do you have any idea?
generate a new secret key in medusa admin panel and use the http header
Authorization: Basic generated_token
do you have admin access to medusa? try to generate a new user with new credentials
the user/pass response is 200 so I think the DB connection is fine
with the new user I can not get session
I creted the new user with this :
NODE_ENV=production npx medusa user -e [email protected] -p testing
that is weird, are you running it on local or hosted on a server?
local db or remote?
I guess NODE_ENV should be NODE_ENV=development
is connecting to the db correctly?
if I put wrong user and pass i got error
if I put correct I got 200
and i got session
but the me is saying 401
so I think the db connection is right because reading the user data well
but do you have any idea how to check the db connection is correct or not?
when you run medusa you get an error right away if the db connection fails or the connection to redis
yarn run v1.22.10 $ medusa start {"level":"info","message":"Skipping instrumentation registration. No register function found.","timestamp":"2025-01-09 17:56:05"} redisUrl not found. A fake redis instance will be used. {"level":"info","message":"No link to load from /home/gyurka00/projects/hanco-admin/.medusa/server/src/links. skipped.","timestamp":"2025-01-09 17:56:05"} {"level":"warn","message":"Local Event Bus installed. This is not recommended for production.","timestamp":"2025-01-09 17:56:05"} {"level":"info","message":"Locking module: Using \"in-memory\" as default.","timestamp":"2025-01-09 17:56:05"} {"level":"info","message":"No workflow to load from /home/gyurka00/projects/hanco-admin/.medusa/server/src/workflows. skipped.","timestamp":"2025-01-09 17:56:06"} {"level":"info","message":"No job to load from /home/gyurka00/projects/hanco-admin/.medusa/server/node_modules/@medusajs/medusa/dist/jobs. skipped.","timestamp":"2025-01-09 17:56:07"} {"level":"info","message":"No job to load from /home/gyurka00/projects/hanco-admin/.medusa/server/src/jobs. skipped.","timestamp":"2025-01-09 17:56:07"} Warning: connect.session() MemoryStore is not designed for a production environment, as it will leak memory, and will not scale past a single process. {"activity_id":"01JH5ZX9GV886YZVFH4C539TNC","config":{},"level":"info","message":"Creating server","timestamp":"2025-01-09 17:56:07"} {"activity_id":"01JH5ZX9GV886YZVFH4C539TNC","duration":13,"level":"info","message":"Server is ready on http://localhost:9000","timestamp":"2025-01-09 17:56:07"}
i do not see any error log
this is strange:
redisUrl not found. A fake redis instance will be used.
it doesn't look like you are experiencing any error with postgres connection
in the env I have a redis config
REDIS_URL=redis://localhost:6379
do you have redis enabled on medusa conf
and my local radis is running there
module.exports = defineConfig({
projectConfig: {
databaseUrl: process.env.DATABASE_URL,
redisUrl: process.env.REDIS_URL,
databaseDriverOptions:
{ connection: { ssl: { rejectUnauthorized: false } } },
redisOptions: {
connectTimeout: 10000
},
http: {
storeCors: process.env.STORE_CORS || "http://localhost:8000",
adminCors: process.env.ADMIN_CORS || "http://localhost:9000",
authCors: process.env.AUTH_CORS || "http://localhost:8000,http://localhost:9000",
jwtSecret: process.env.JWT_SECRET || "supersecret",
cookieSecret: process.env.COOKIE_SECRET || "supersecret",
},
}
})
this is medusa-config.ts
add redisUrl: process.env.REDIS_URL,
if you want to read the env variable correctly
the only change is now I do not get the redisUrl not found. A fake redis instance will be used.
but still can not log in
{"level":"http","message":"127.0.0.1 - - [09/Jan/2025:17:05:08 +0000] \"POST /auth/user/emailpass HTTP/1.1\" 200 381 \"http://localhost:9000/app/login\" \"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36\"","timestamp":"2025-01-09 18:05:08"} {"level":"http","message":"127.0.0.1 - - [09/Jan/2025:17:05:08 +0000] \"POST /auth/session HTTP/1.1\" 200 225 \"http://localhost:9000/app/login\" \"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36\"","timestamp":"2025-01-09 18:05:08"} {"level":"http","message":"127.0.0.1 - - [09/Jan/2025:17:05:08 +0000] \"GET /admin/users/me HTTP/1.1\" 401 26 \"http://localhost:9000/app/orders\" \"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36\"","timestamp":"2025-01-09 18:05:08"} {"level":"http","message":"127.0.0.1 - - [09/Jan/2025:17:05:09 +0000] \"GET /admin/users/me HTTP/1.1\" 401 26 \"http://localhost:9000/app/orders\" \"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36\"","timestamp":"2025-01-09 18:05:09"}
/auth/user/emailpass and /auth/session response are 200
/admin/users/me is 401
hmm its looks like need SSL
"For anyone in this thread. If NODE_ENV is set to production the cookie is secure. Hence it needs SSL certificate.
I would crosscheck those points:
Domains should not be on Public Suffix List
SSL certificate on all domains (https)
admin, storefront and backend under the same main domain i.e. mydomain.com, backend.mydomain.com, admin.mydomain.com
COOKIE_SECRET is set
Redis needs to be set up (it acts as express session store)"
https://github.com/medusajs/medusa/issues/2314#issuecomment-1422429232
I will try tomorrow.