diff --git a/src/auth/database.ts b/src/auth/database.ts index 5124f5101f..d19cb305b1 100644 --- a/src/auth/database.ts +++ b/src/auth/database.ts @@ -155,7 +155,18 @@ export class Database extends BaseAuthAPI { initOverrides, ); - return JSONApiResponse.fromResponse(response); + // Transform the response to ensure id field is always available + const jsonResponse = await JSONApiResponse.fromResponse(response); + + if (jsonResponse.data) { + const data = jsonResponse.data as any; + // Map _id or user_id to id + if (!data.id && (data._id || data.user_id)) { + data.id = data._id || data.user_id; + } + } + + return jsonResponse as JSONApiResponse; } /** diff --git a/tests/auth/database.test.ts b/tests/auth/database.test.ts index c893119ba7..7b2373afc0 100644 --- a/tests/auth/database.test.ts +++ b/tests/auth/database.test.ts @@ -35,6 +35,38 @@ describe("Database", () => { }); expect(data).toEqual({ _id: "test-id", + id: "test-id", + email_verified: false, + email, + }); + }); + + it("should signup a user when response param for id is 'user_id'", async () => { + const database = new Database(opts); + const email = "test-email-1@example.com"; + const { data } = await database.signUp({ + email, + password: PASSWORD, + connection: "Username-Password-Authentication", + }); + expect(data).toEqual({ + user_id: "test-id", + id: "test-id", + email_verified: false, + email, + }); + }); + + it("should signup a user when response param for id is 'id'", async () => { + const database = new Database(opts); + const email = "test-email-2@example.com"; + const { data } = await database.signUp({ + email, + password: PASSWORD, + connection: "Username-Password-Authentication", + }); + expect(data).toEqual({ + id: "test-id", email_verified: false, email, }); diff --git a/tests/auth/fixtures/database.json b/tests/auth/fixtures/database.json index 39f84c9a6f..a00a4b7ab5 100644 --- a/tests/auth/fixtures/database.json +++ b/tests/auth/fixtures/database.json @@ -33,6 +33,40 @@ "email": "test-email@example.com" } }, + { + "scope": "https://test-domain.auth0.com", + "method": "POST", + "path": "/dbconnections/signup", + "body": { + "client_id": "test-client-id", + "email": "test-email-1@example.com", + "password": "test-password", + "connection": "Username-Password-Authentication" + }, + "status": 200, + "response": { + "user_id": "test-id", + "email_verified": false, + "email": "test-email-1@example.com" + } + }, + { + "scope": "https://test-domain.auth0.com", + "method": "POST", + "path": "/dbconnections/signup", + "body": { + "client_id": "test-client-id", + "email": "test-email-2@example.com", + "password": "test-password", + "connection": "Username-Password-Authentication" + }, + "status": 200, + "response": { + "id": "test-id", + "email_verified": false, + "email": "test-email-2@example.com" + } + }, { "scope": "https://test-domain.auth0.com", "method": "POST",