Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle duplicate user sign up #886

Merged
merged 2 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/auth/base-auth-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ export class AuthApiError extends Error {
}
}

function parseErrorBody(body: any): AuthApiErrorResponse {
const rawData = JSON.parse(body);
let data: AuthApiErrorResponse;

if (rawData.error) {
data = rawData as AuthApiErrorResponse;
} else {
data = {
error: rawData.code,
error_description: rawData.description,
};
}

return data;
}

async function parseError(response: Response) {
// Errors typically have a specific format:
// {
Expand All @@ -41,10 +57,10 @@ async function parseError(response: Response) {
// }

const body = await response.text();
let data: AuthApiErrorResponse;

try {
data = JSON.parse(body) as AuthApiErrorResponse;
const data = parseErrorBody(body);

return new AuthApiError(
data.error,
data.error_description,
Expand Down
26 changes: 26 additions & 0 deletions test/auth/database.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import nock from 'nock';
import { beforeAll, afterAll } from '@jest/globals';
import { Database } from '../../src/auth/database';
import { AuthApiError } from '../../src/auth/base-auth-api';

const { back: nockBack } = nock;

const EMAIL = 'test-email@example.com';
const DUPLICATE_EMAIL = 'test-email-duplicate@example.com';
const PASSWORD = 'test-password';

const opts = {
Expand Down Expand Up @@ -48,6 +50,30 @@ describe('Database', () => {
} as any)
).rejects.toThrow('Required parameter requestParameters.connection was null or undefined.');
});

it('should handle duplicate user error', async () => {
const database = new Database(opts);
const email = DUPLICATE_EMAIL;
let error: AuthApiError | null = null;

try {
await database.signUp({
email,
password: PASSWORD,
connection: 'Username-Password-Authentication',
});
} catch (e) {
error = e as AuthApiError;
}

expect(error).toBeDefined();
expect(error).toEqual(
expect.objectContaining({
error: 'invalid_signup',
error_description: 'Invalid sign up',
})
);
});
});

describe('#changePassword', () => {
Expand Down
17 changes: 17 additions & 0 deletions test/auth/fixtures/database.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@
"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-duplicate@example.com",
"password": "test-password",
"connection": "Username-Password-Authentication"
},
"status": 400,
"response": {
"name": "BadRequestError",
"code": "invalid_signup",
"description": "Invalid sign up"
}
},
{
"scope": "https://test-domain.auth0.com",
"method": "POST",
Expand Down