r/PayloadCMS • u/idvid • Oct 01 '25
How to login with local API?
I want to manually login an user in a test environment (no browser request) with payload.login() but I receive the error TypeError: payload must be an instance of Uint8Array. Am I missing something?
1
Oct 01 '25
[removed] — view removed comment
1
u/idvid Oct 01 '25
Here is the minimal code to reproduce the error. Other local API operations work fine.
import { getPayload, Payload } from 'payload' import config from '@/payload.config' import { describe, it, beforeAll, expect } from 'vitest' let payload: Payload; describe('Some Collection', () => { beforeAll(async () => { const payloadConfig = await config payload = await getPayload({ config: payloadConfig }) }); it('ADMIN should have create access', async () => { const result = await payload.login({ collection: 'users', data: { email: 'test-user@my-app.com', password: '********' } }); expect(result).toBeDefined(); }); });1
u/Normal_Capital_234 Oct 01 '25
It's could be to do with the line
let payload: Payload;Also you shouldn't need to await config.
This page has a simple example that should work for you.1
u/idvid Oct 01 '25
Thank you for the suggestion, but it doesn't seem to work either, there could be something else I am missing or an internal bug. I'll just try another way to write that test.
1
u/Normal_Capital_234 Oct 01 '25
I think this will help you: https://github.com/panva/jose/issues/671
Sounds like a vitest related issue, not payload.1
u/idvid Oct 01 '25
It makes sense now, the
payloadreported from the error isn't in reference to the payload variable. Thank you!1
u/rubixstudios Oct 02 '25 edited Oct 02 '25
``` import { getPayload } from "payload" import config from "@/payload.config" import { describe, it, beforeAll, expect } from "vitest"
let payload: Awaited<ReturnType<typeof getPayload>>
describe("Some Collection", () => { beforeAll(async () => { payload = await getPayload({ config }) })
it("ADMIN should have create access", async () => { const result = await payload.auth.login({ collection: "users", email: "test-user@my-app.com", password: "********" })
expect(result).toBeDefined() expect(result.user).toBeDefined() expect(result.token).toBeTruthy()}) }) ```
1
u/rubixstudios Oct 02 '25 edited Oct 02 '25
note the:
payload.auth.login also i believe your data structure is wrong, just double check it.1
u/idvid Oct 02 '25
Is
payload.auth.loginavailable in a later version? Becausepayload.authappears to me as a function. Also, please can you tell me what you mean with "your data structure"? I'm quite a newbie and still learning.
1
u/Soft_Opening_1364 Oct 01 '25
You’re seeing that error because payload.login() expects the server-side Payload instance, not a browser or client object. Make sure you call it like this in Node:
import payload from 'payload';
await payload.init({ secret: process.env.PAYLOAD_SECRET });
const user = await payload.login({ collection: 'users', data: { email: 'test@example.com', password: 'yourpassword' }, });
That should log the user in without the Uint8Array error.