Skip to content

Commit ebc882a

Browse files
committed
added phonenumber identifier in Auth API
1 parent bc505ca commit ebc882a

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

src/main/java/com/auth0/client/auth/AuthAPI.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public class AuthAPI {
6868
private static final String PATH_PASSWORDLESS = "passwordless";
6969
private static final String PATH_START = "start";
7070
private static final String KEY_ORGANIZATION = "organization";
71+
private static final String KEY_PHONE_NUMBER = "phone_number";
7172

7273
private final Auth0HttpClient client;
7374
private final String clientId;
@@ -485,6 +486,41 @@ public Request<Void> resetPassword(String clientId, String email, String connect
485486
return request;
486487
}
487488

489+
/**
490+
* Creates a sign up request with the given credentials, phone number and database connection.
491+
* "Requires Username" option must be turned on in the Connection's configuration first.
492+
* i.e.:
493+
* <pre>
494+
* {@code
495+
* try {
496+
* Map<String, String> fields = new HashMap<String, String>();
497+
* fields.put("age", "25);
498+
* fields.put("city", "Buenos Aires");
499+
* authAPI.signUp("me@auth0.com", "myself", new char[]{'s','e','c','r','e','t'}, "db-connection", "1234567890")
500+
* .setCustomFields(fields)
501+
* .execute();
502+
* } catch (Auth0Exception e) {
503+
* //Something happened
504+
* }
505+
* }
506+
* </pre>
507+
*
508+
* @see <a href="https://auth0.com/docs/api/authentication#signup">Signup API docs</a>
509+
* @param email the desired user's email.
510+
* @param username the desired user's username.
511+
* @param password the desired user's password.
512+
* @param connection the database connection where the user is going to be created.
513+
* @param phoneNumber the desired users's phone number.
514+
* @return a Request to configure and execute.
515+
*/
516+
public SignUpRequest signUp(String email, String username, char[] password, String connection, String phoneNumber) {
517+
Asserts.assertNotNull(phoneNumber, "phone number");
518+
519+
SignUpRequest request = this.signUp(email, username, password, connection);
520+
request.addParameter(KEY_PHONE_NUMBER, phoneNumber);
521+
return request;
522+
}
523+
488524
/**
489525
* Creates a sign up request with the given credentials and database connection.
490526
* "Requires Username" option must be turned on in the Connection's configuration first.

src/main/java/com/auth0/json/auth/CreatedUser.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public class CreatedUser {
2222
private String username;
2323
@JsonProperty("email_verified")
2424
private Boolean emailVerified;
25+
@JsonProperty("phone_number")
26+
private String phoneNumber;
2527

2628
@JsonProperty("_id")
2729
@JsonAlias({"_id", "id", "user_id"})
@@ -44,4 +46,8 @@ public Boolean isEmailVerified() {
4446
return emailVerified;
4547
}
4648

49+
@JsonProperty("phone_number")
50+
public String getPhoneNumber() {
51+
return phoneNumber;
52+
}
4753
}

src/test/java/com/auth0/client/auth/AuthAPITest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,13 @@ public void shouldThrowOnUsernameSignUpWithNullConnection() {
400400
"'connection' cannot be null!");
401401
}
402402

403+
@Test
404+
public void shouldThrowOnUsernameAndPhoneNumberSignUpWithNullPhoneNumber() {
405+
verifyThrows(IllegalArgumentException.class,
406+
() -> api.signUp("me@auth0.com", "me", new char[]{'p','4','5','5','w','0','r','d'}, "my-connection", null),
407+
"'phone number' cannot be null!");
408+
}
409+
403410
@Test
404411
public void shouldHaveNotStrongPasswordWithDetailedDescription() throws Exception {
405412
ObjectMapper mapper = new ObjectMapper();
@@ -424,6 +431,34 @@ public void shouldHaveNotStrongPasswordWithShortDetailedDescription() throws Exc
424431
assertThat(ex.getDescription(), is(expectedDescription));
425432
}
426433

434+
@Test
435+
public void shouldCreateSignUpRequestWithUsernameAndPhoneNumber() throws Exception {
436+
SignUpRequest request = api.signUp("me@auth0.com", "me", new char[]{'p','4','5','5','w','0','r','d'}, "db-connection", "1234567890");
437+
assertThat(request, is(notNullValue()));
438+
439+
server.jsonResponse(AUTH_SIGN_UP_USERNAME, 200);
440+
CreatedUser response = request.execute().getBody();
441+
RecordedRequest recordedRequest = server.takeRequest();
442+
443+
assertThat(recordedRequest, hasMethodAndPath(HttpMethod.POST, "/dbconnections/signup"));
444+
assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
445+
446+
Map<String, Object> body = bodyFromRequest(recordedRequest);
447+
assertThat(body, hasEntry("email", "me@auth0.com"));
448+
assertThat(body, hasEntry("username", "me"));
449+
assertThat(body, hasEntry("password", "p455w0rd"));
450+
assertThat(body, hasEntry("connection", "db-connection"));
451+
assertThat(body, hasEntry("client_id", CLIENT_ID));
452+
assertThat(body, hasEntry("phone_number", "1234567890"));
453+
454+
assertThat(response, is(notNullValue()));
455+
assertThat(response.getUserId(), is("58457fe6b27"));
456+
assertThat(response.getEmail(), is("me@auth0.com"));
457+
assertThat(response.isEmailVerified(), is(false));
458+
assertThat(response.getUsername(), is("me"));
459+
assertThat(response.getPhoneNumber(), is("1234567890"));
460+
}
461+
427462
@Test
428463
public void shouldCreateSignUpRequestWithUsername() throws Exception {
429464
@SuppressWarnings("deprecation")

src/test/resources/auth/sign_up_username.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"_id": "58457fe6b27",
33
"email_verified": false,
44
"email": "me@auth0.com",
5-
"username": "me"
6-
}
5+
"username": "me",
6+
"phone_number": "1234567890"
7+
}

0 commit comments

Comments
 (0)