feat(User): better quick signup and phone number validation
This commit is contained in:
@@ -72,6 +72,11 @@
|
|||||||
<artifactId>password4j</artifactId>
|
<artifactId>password4j</artifactId>
|
||||||
<version>1.8.2</version>
|
<version>1.8.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.googlecode.libphonenumber</groupId>
|
||||||
|
<artifactId>libphonenumber</artifactId>
|
||||||
|
<version>8.13.31</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package com.drinkool;
|
package com.drinkool;
|
||||||
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
import com.drinkool.dtos.*;
|
import com.drinkool.dtos.*;
|
||||||
import com.drinkool.models.Customer;
|
import com.drinkool.models.Customer;
|
||||||
|
|
||||||
@@ -28,7 +26,7 @@ public class RestAPI {
|
|||||||
public Response quickSignup(QuickSignup input) {
|
public Response quickSignup(QuickSignup input) {
|
||||||
Customer newCustomer = new Customer();
|
Customer newCustomer = new Customer();
|
||||||
|
|
||||||
newCustomer.signup(input.phone, UUID.randomUUID().toString().substring(0, 8));
|
newCustomer.signup(input.phone);
|
||||||
|
|
||||||
return Response.status(Response.Status.CREATED).build();
|
return Response.status(Response.Status.CREATED).build();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,11 @@ package com.drinkool.models;
|
|||||||
import io.quarkus.hibernate.orm.panache.PanacheEntity;
|
import io.quarkus.hibernate.orm.panache.PanacheEntity;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import jakarta.transaction.*;
|
import jakarta.transaction.*;
|
||||||
|
import jakarta.ws.rs.BadRequestException;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import com.drinkool.utils.PhoneValidator;
|
||||||
import com.password4j.Password;
|
import com.password4j.Password;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@@ -23,14 +28,20 @@ public abstract class User extends PanacheEntity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void signup(String phone, String rawPassword) {
|
public void signup(String phone) {
|
||||||
this.signup(phone, phone, rawPassword);
|
this.signup(phone, phone, UUID.randomUUID().toString().substring(0, 32));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void signup(String name, String phone, String rawPassword) {
|
public void signup(String name, String phone, String rawPassword) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.phone = phone;
|
|
||||||
|
if (!PhoneValidator.isValidInternationalPhone(phone)) {
|
||||||
|
throw new BadRequestException("InvalidPhoneNumber");
|
||||||
|
} else {
|
||||||
|
this.phone = phone;
|
||||||
|
}
|
||||||
|
|
||||||
this.hashedPassword = Password.hash(rawPassword).withArgon2().getResult();
|
this.hashedPassword = Password.hash(rawPassword).withArgon2().getResult();
|
||||||
|
|
||||||
this.persist();
|
this.persist();
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package com.drinkool.utils;
|
||||||
|
|
||||||
|
import com.google.i18n.phonenumbers.*;
|
||||||
|
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
|
||||||
|
|
||||||
|
public class PhoneValidator {
|
||||||
|
private static final PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
|
||||||
|
|
||||||
|
public static boolean isValidInternationalPhone(String phone) {
|
||||||
|
try {
|
||||||
|
PhoneNumber numberProto = phoneUtil.parse(phone, "VN");
|
||||||
|
|
||||||
|
return phoneUtil.isValidNumber(numberProto);
|
||||||
|
} catch (NumberParseException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user