SlideShare a Scribd company logo
1 of 46
Download to read offline
Creating a Facebook Clone - Part XXI
@Service
public class UserService {
@Autowired
private UserRepository users;
@Autowired
private MediaRepository medias;
@Autowired
private NotificationService notifications;
@Autowired
private ShadowUserRepository shadows;
@Autowired
private PasswordEncoder encoder;
private Properties apiKeys;
private Properties getApiKeys() {
UserService
@Service
public class UserService {
@Autowired
private UserRepository users;
@Autowired
private MediaRepository medias;
@Autowired
private NotificationService notifications;
@Autowired
private ShadowUserRepository shadows;
@Autowired
private PasswordEncoder encoder;
private Properties apiKeys;
private Properties getApiKeys() {
UserService
@Service
public class UserService {
@Autowired
private UserRepository users;
@Autowired
private MediaRepository medias;
@Autowired
private NotificationService notifications;
@Autowired
private ShadowUserRepository shadows;
@Autowired
private PasswordEncoder encoder;
private Properties apiKeys;
private Properties getApiKeys() {
UserService
.create();
message.getSid();
}
public UserDAO login(String email, String phone, String password)
throws LoginException {
List<User> userList;
if (email != null && !email.isEmpty()) {
userList = users.findByEmailIgnoreCase(email);
} else {
userList = users.findByPhone(phone);
}
if (userList != null && userList.size() == 1) {
User u = userList.get(0);
if (encoder.matches(password, u.getPassword())) {
return u.getLoginDAO();
}
throw new LoginException("Password authentication error!");
}
throw new LoginException("User not found!");
}
private String createVerificationCode(int length) {
StringBuilder k = new StringBuilder();
UserService
.create();
message.getSid();
}
public UserDAO login(String email, String phone, String password)
throws LoginException {
List<User> userList;
if (email != null && !email.isEmpty()) {
userList = users.findByEmailIgnoreCase(email);
} else {
userList = users.findByPhone(phone);
}
if (userList != null && userList.size() == 1) {
User u = userList.get(0);
if (encoder.matches(password, u.getPassword())) {
return u.getLoginDAO();
}
throw new LoginException("Password authentication error!");
}
throw new LoginException("User not found!");
}
private String createVerificationCode(int length) {
StringBuilder k = new StringBuilder();
UserService
.create();
message.getSid();
}
public UserDAO login(String email, String phone, String password)
throws LoginException {
List<User> userList;
if (email != null && !email.isEmpty()) {
userList = users.findByEmailIgnoreCase(email);
} else {
userList = users.findByPhone(phone);
}
if (userList != null && userList.size() == 1) {
User u = userList.get(0);
if (encoder.matches(password, u.getPassword())) {
return u.getLoginDAO();
}
throw new LoginException("Password authentication error!");
}
throw new LoginException("User not found!");
}
private String createVerificationCode(int length) {
StringBuilder k = new StringBuilder();
UserService
.create();
message.getSid();
}
public UserDAO login(String email, String phone, String password)
throws LoginException {
List<User> userList;
if (email != null && !email.isEmpty()) {
userList = users.findByEmailIgnoreCase(email);
} else {
userList = users.findByPhone(phone);
}
if (userList != null && userList.size() == 1) {
User u = userList.get(0);
if (encoder.matches(password, u.getPassword())) {
return u.getLoginDAO();
}
throw new LoginException("Password authentication error!");
}
throw new LoginException("User not found!");
}
private String createVerificationCode(int length) {
StringBuilder k = new StringBuilder();
UserService
.create();
message.getSid();
}
public UserDAO login(String email, String phone, String password)
throws LoginException {
List<User> userList;
if (email != null && !email.isEmpty()) {
userList = users.findByEmailIgnoreCase(email);
} else {
userList = users.findByPhone(phone);
}
if (userList != null && userList.size() == 1) {
User u = userList.get(0);
if (encoder.matches(password, u.getPassword())) {
return u.getLoginDAO();
}
throw new LoginException("Password authentication error!");
}
throw new LoginException("User not found!");
}
private String createVerificationCode(int length) {
StringBuilder k = new StringBuilder();
UserService
public class LoginException extends Exception {
public LoginException(String errorMessage) {
super(errorMessage);
}
}
User u = users.findByAuthtoken(auth).get(0);
return u.getLoginDAO();
}
public UserDAO signup(UserDAO user) throws SignupException,
UnirestException {
List<ShadowUser> shadowList = null;
if (user.getEmail() != null) {
List<User> u = users.findByEmailIgnoreCase(user.getEmail());
if (u != null && u.size() > 0) {
throw new SignupException(
"The email is already registered!");
}
shadowList = shadows.findByEmail(user.getEmail());
}
if (user.getPhone() != null) {
List<User> u = users.findByPhone(user.getPhone());
if (u != null && u.size() > 0) {
throw new SignupException(
"The phone number is already registered!");
}
shadowList = shadows.findByPhone(user.getPhone());
}
UserService
User u = users.findByAuthtoken(auth).get(0);
return u.getLoginDAO();
}
public UserDAO signup(UserDAO user) throws SignupException,
UnirestException {
List<ShadowUser> shadowList = null;
if (user.getEmail() != null) {
List<User> u = users.findByEmailIgnoreCase(user.getEmail());
if (u != null && u.size() > 0) {
throw new SignupException(
"The email is already registered!");
}
shadowList = shadows.findByEmail(user.getEmail());
}
if (user.getPhone() != null) {
List<User> u = users.findByPhone(user.getPhone());
if (u != null && u.size() > 0) {
throw new SignupException(
"The phone number is already registered!");
}
shadowList = shadows.findByPhone(user.getPhone());
}
UserService
User u = users.findByAuthtoken(auth).get(0);
return u.getLoginDAO();
}
public UserDAO signup(UserDAO user) throws SignupException,
UnirestException {
List<ShadowUser> shadowList = null;
if (user.getEmail() != null) {
List<User> u = users.findByEmailIgnoreCase(user.getEmail());
if (u != null && u.size() > 0) {
throw new SignupException(
"The email is already registered!");
}
shadowList = shadows.findByEmail(user.getEmail());
}
if (user.getPhone() != null) {
List<User> u = users.findByPhone(user.getPhone());
if (u != null && u.size() > 0) {
throw new SignupException(
"The phone number is already registered!");
}
shadowList = shadows.findByPhone(user.getPhone());
}
UserService
User u = users.findByAuthtoken(auth).get(0);
return u.getLoginDAO();
}
public UserDAO signup(UserDAO user) throws SignupException,
UnirestException {
List<ShadowUser> shadowList = null;
if (user.getEmail() != null) {
List<User> u = users.findByEmailIgnoreCase(user.getEmail());
if (u != null && u.size() > 0) {
throw new SignupException(
"The email is already registered!");
}
shadowList = shadows.findByEmail(user.getEmail());
}
if (user.getPhone() != null) {
List<User> u = users.findByPhone(user.getPhone());
if (u != null && u.size() > 0) {
throw new SignupException(
"The phone number is already registered!");
}
shadowList = shadows.findByPhone(user.getPhone());
}
UserService
List<User> u = users.findByPhone(user.getPhone());
if (u != null && u.size() > 0) {
throw new SignupException(
"The phone number is already registered!");
}
shadowList = shadows.findByPhone(user.getPhone());
}
User u = new User();
setProps(user, u);
u.setPassword(encoder.encode(user.getPassword()));
u.setAuthtoken(UUID.randomUUID().toString());
u.setVerificationCode(createVerificationCode(4));
if (shadowList != null && !shadowList.isEmpty()) {
Set<User> friendSuggestions = new HashSet<>();
for (ShadowUser s : shadowList) {
friendSuggestions.add(s.getOwner());
}
u.setPeopleYouMayKnow(friendSuggestions);
}
users.save(u);
UserService
List<User> u = users.findByPhone(user.getPhone());
if (u != null && u.size() > 0) {
throw new SignupException(
"The phone number is already registered!");
}
shadowList = shadows.findByPhone(user.getPhone());
}
User u = new User();
setProps(user, u);
u.setPassword(encoder.encode(user.getPassword()));
u.setAuthtoken(UUID.randomUUID().toString());
u.setVerificationCode(createVerificationCode(4));
if (shadowList != null && !shadowList.isEmpty()) {
Set<User> friendSuggestions = new HashSet<>();
for (ShadowUser s : shadowList) {
friendSuggestions.add(s.getOwner());
}
u.setPeopleYouMayKnow(friendSuggestions);
}
users.save(u);
UserService
List<User> u = users.findByPhone(user.getPhone());
if (u != null && u.size() > 0) {
throw new SignupException(
"The phone number is already registered!");
}
shadowList = shadows.findByPhone(user.getPhone());
}
User u = new User();
setProps(user, u);
u.setPassword(encoder.encode(user.getPassword()));
u.setAuthtoken(UUID.randomUUID().toString());
u.setVerificationCode(createVerificationCode(4));
if (shadowList != null && !shadowList.isEmpty()) {
Set<User> friendSuggestions = new HashSet<>();
for (ShadowUser s : shadowList) {
friendSuggestions.add(s.getOwner());
}
u.setPeopleYouMayKnow(friendSuggestions);
}
users.save(u);
UserService
List<User> u = users.findByPhone(user.getPhone());
if (u != null && u.size() > 0) {
throw new SignupException(
"The phone number is already registered!");
}
shadowList = shadows.findByPhone(user.getPhone());
}
User u = new User();
setProps(user, u);
u.setPassword(encoder.encode(user.getPassword()));
u.setAuthtoken(UUID.randomUUID().toString());
u.setVerificationCode(createVerificationCode(4));
if (shadowList != null && !shadowList.isEmpty()) {
Set<User> friendSuggestions = new HashSet<>();
for (ShadowUser s : shadowList) {
friendSuggestions.add(s.getOwner());
}
u.setPeopleYouMayKnow(friendSuggestions);
}
users.save(u);
UserService
List<User> u = users.findByPhone(user.getPhone());
if (u != null && u.size() > 0) {
throw new SignupException(
"The phone number is already registered!");
}
shadowList = shadows.findByPhone(user.getPhone());
}
User u = new User();
setProps(user, u);
u.setPassword(encoder.encode(user.getPassword()));
u.setAuthtoken(UUID.randomUUID().toString());
u.setVerificationCode(createVerificationCode(4));
if (shadowList != null && !shadowList.isEmpty()) {
Set<User> friendSuggestions = new HashSet<>();
for (ShadowUser s : shadowList) {
friendSuggestions.add(s.getOwner());
}
u.setPeopleYouMayKnow(friendSuggestions);
}
users.save(u);
UserService
for (ShadowUser s : shadowList) {
friendSuggestions.add(s.getOwner());
}
u.setPeopleYouMayKnow(friendSuggestions);
}
users.save(u);
if (user.getEmail() != null) {
sendActivationEmail(user.getEmail(),
"Please Activate Your Facebook Clone Account",
"Activation key: " + u.getVerificationCode());
} else {
sendActivationSMS(user.getPhone(), "Activation key: " + u.
getVerificationCode());
}
return u.getLoginDAO();
}
public boolean verifyEmailOrPhone(String authToken, String code,
boolean email) {
User u = users.findByAuthtoken(authToken).get(0);
if (u.getVerificationCode().equals(code)) {
UserService
for (ShadowUser s : shadowList) {
friendSuggestions.add(s.getOwner());
}
u.setPeopleYouMayKnow(friendSuggestions);
}
users.save(u);
if (user.getEmail() != null) {
sendActivationEmail(user.getEmail(),
"Please Activate Your Facebook Clone Account",
"Activation key: " + u.getVerificationCode());
} else {
sendActivationSMS(user.getPhone(), "Activation key: " + u.
getVerificationCode());
}
return u.getLoginDAO();
}
public boolean verifyEmailOrPhone(String authToken, String code,
boolean email) {
User u = users.findByAuthtoken(authToken).get(0);
if (u.getVerificationCode().equals(code)) {
UserService
for (ShadowUser s : shadowList) {
friendSuggestions.add(s.getOwner());
}
u.setPeopleYouMayKnow(friendSuggestions);
}
users.save(u);
if (user.getEmail() != null) {
sendActivationEmail(user.getEmail(),
"Please Activate Your Facebook Clone Account",
"Activation key: " + u.getVerificationCode());
} else {
sendActivationSMS(user.getPhone(), "Activation key: " + u.
getVerificationCode());
}
return u.getLoginDAO();
}
public boolean verifyEmailOrPhone(String authToken, String code,
boolean email) {
User u = users.findByAuthtoken(authToken).get(0);
if (u.getVerificationCode().equals(code)) {
UserService
} else {
u.setVerifiedPhone(u.getPhone());
}
users.save(u);
return true;
}
return false;
}
private void setProps(UserDAO user, User u) {
u.setEmail(user.getEmail());
if (user.getBirthday() != null) {
u.setBirthday(new Date(user.getBirthday()));
}
u.setFamilyName(user.getFamilyName());
u.setFirstName(user.getFirstName());
u.setGender(user.getGender());
u.setPhone(user.getPhone());
}
public void update(UserDAO user) {
User u = users.findByAuthtoken(user.getAuthtoken()).get(0);
setProps(user, u);
if (u.getPassword() != null) {
UserService
} else {
u.setVerifiedPhone(u.getPhone());
}
users.save(u);
return true;
}
return false;
}
private void setProps(UserDAO user, User u) {
u.setEmail(user.getEmail());
if (user.getBirthday() != null) {
u.setBirthday(new Date(user.getBirthday()));
}
u.setFamilyName(user.getFamilyName());
u.setFirstName(user.getFirstName());
u.setGender(user.getGender());
u.setPhone(user.getPhone());
}
public void update(UserDAO user) {
User u = users.findByAuthtoken(user.getAuthtoken()).get(0);
setProps(user, u);
if (u.getPassword() != null) {
UserService
User u = userList.get(0);
if (encoder.matches(password, u.getPassword())) {
return u.getLoginDAO();
}
throw new LoginException("Password authentication error!");
}
throw new LoginException("User not found!");
}
private String createVerificationCode(int length) {
StringBuilder k = new StringBuilder();
Random r = new Random();
for (int iter = 0; iter < length; iter++) {
k.append(r.nextInt(10));
}
return k.toString();
}
public UserDAO refreshUser(String auth) {
User u = users.findByAuthtoken(auth).get(0);
return u.getLoginDAO();
}
public UserDAO signup(UserDAO user) throws SignupException,
UserService
User u = userList.get(0);
if (encoder.matches(password, u.getPassword())) {
return u.getLoginDAO();
}
throw new LoginException("Password authentication error!");
}
throw new LoginException("User not found!");
}
private String createVerificationCode(int length) {
StringBuilder k = new StringBuilder();
Random r = new Random();
for (int iter = 0; iter < length; iter++) {
k.append(r.nextInt(10));
}
return k.toString();
}
public UserDAO refreshUser(String auth) {
User u = users.findByAuthtoken(auth).get(0);
return u.getLoginDAO();
}
public UserDAO signup(UserDAO user) throws SignupException,
UserService
User u = userList.get(0);
if (encoder.matches(password, u.getPassword())) {
return u.getLoginDAO();
}
throw new LoginException("Password authentication error!");
}
throw new LoginException("User not found!");
}
private String createVerificationCode(int length) {
StringBuilder k = new StringBuilder();
Random r = new Random();
for (int iter = 0; iter < length; iter++) {
k.append(r.nextInt(10));
}
return k.toString();
}
public UserDAO refreshUser(String auth) {
User u = users.findByAuthtoken(auth).get(0);
return u.getLoginDAO();
}
public UserDAO signup(UserDAO user) throws SignupException,
UserService
.queryString("to", destination)
.queryString("subject", title)
.queryString("text", body)
.asJson();
return request.getBody();
}
private void sendActivationSMS(String number, String text) {
Properties prop = getApiKeys();
Twilio.init(prop.getProperty("twilio.sid"), prop.getProperty(
"twilio.auth"));
Message message = Message
.creator(new PhoneNumber(number),
new PhoneNumber(prop.getProperty("twilio.phone")),
text)
.create();
message.getSid();
}
public UserDAO login(String email, String phone, String password)
throws LoginException {
List<User> userList;
UserService
.queryString("to", destination)
.queryString("subject", title)
.queryString("text", body)
.asJson();
return request.getBody();
}
private void sendActivationSMS(String number, String text) {
Properties prop = getApiKeys();
Twilio.init(prop.getProperty("twilio.sid"), prop.getProperty(
"twilio.auth"));
Message message = Message
.creator(new PhoneNumber(number),
new PhoneNumber(prop.getProperty("twilio.phone")),
text)
.create();
message.getSid();
}
public UserDAO login(String email, String phone, String password)
throws LoginException {
List<User> userList;
UserService
.queryString("to", destination)
.queryString("subject", title)
.queryString("text", body)
.asJson();
return request.getBody();
}
private void sendActivationSMS(String number, String text) {
Properties prop = getApiKeys();
Twilio.init(prop.getProperty("twilio.sid"), prop.getProperty(
"twilio.auth"));
Message message = Message
.creator(new PhoneNumber(number),
new PhoneNumber(prop.getProperty("twilio.phone")),
text)
.create();
message.getSid();
}
public UserDAO login(String email, String phone, String password)
throws LoginException {
List<User> userList;
UserService
.queryString("to", destination)
.queryString("subject", title)
.queryString("text", body)
.asJson();
return request.getBody();
}
private void sendActivationSMS(String number, String text) {
Properties prop = getApiKeys();
Twilio.init(prop.getProperty("twilio.sid"), prop.getProperty(
"twilio.auth"));
Message message = Message
.creator(new PhoneNumber(number),
new PhoneNumber(prop.getProperty("twilio.phone")),
text)
.create();
message.getSid();
}
public UserDAO login(String email, String phone, String password)
throws LoginException {
List<User> userList;
UserService
@Autowired
private PasswordEncoder encoder;
private Properties apiKeys;
private Properties getApiKeys() {
if (apiKeys == null) {
apiKeys = new Properties();
try (FileInputStream fis = new FileInputStream(
System.getProperty("user.home") + File.separator
+ "fbclone_keys.properties")) {
apiKeys.load(fis);
} catch (IOException err) {
throw new RuntimeException(err);
}
}
return apiKeys;
}
private JsonNode sendActivationEmail(String destination, String title,
String body) throws UnirestException {
Properties prop = getApiKeys();
HttpResponse<JsonNode> request
UserService
@Autowired
private PasswordEncoder encoder;
private Properties apiKeys;
private Properties getApiKeys() {
if (apiKeys == null) {
apiKeys = new Properties();
try (FileInputStream fis = new FileInputStream(
System.getProperty("user.home") + File.separator
+ "fbclone_keys.properties")) {
apiKeys.load(fis);
} catch (IOException err) {
throw new RuntimeException(err);
}
}
return apiKeys;
}
private JsonNode sendActivationEmail(String destination, String title,
String body) throws UnirestException {
Properties prop = getApiKeys();
HttpResponse<JsonNode> request
UserService
@Autowired
private PasswordEncoder encoder;
private Properties apiKeys;
private Properties getApiKeys() {
if (apiKeys == null) {
apiKeys = new Properties();
try (FileInputStream fis = new FileInputStream(
System.getProperty("user.home") + File.separator
+ "fbclone_keys.properties")) {
apiKeys.load(fis);
} catch (IOException err) {
throw new RuntimeException(err);
}
}
return apiKeys;
}
private JsonNode sendActivationEmail(String destination, String title,
String body) throws UnirestException {
Properties prop = getApiKeys();
HttpResponse<JsonNode> request
UserService
@Autowired
private PasswordEncoder encoder;
private Properties apiKeys;
private Properties getApiKeys() {
if (apiKeys == null) {
apiKeys = new Properties();
try (FileInputStream fis = new FileInputStream(
System.getProperty("user.home") + File.separator
+ "fbclone_keys.properties")) {
apiKeys.load(fis);
} catch (IOException err) {
throw new RuntimeException(err);
}
}
return apiKeys;
}
private JsonNode sendActivationEmail(String destination, String title,
String body) throws UnirestException {
Properties prop = getApiKeys();
HttpResponse<JsonNode> request
UserService
© Codename One 2017 all rights reserved
twilio.sid=ACaxdddu338829e10981e60db1fff431adcc36fa
twilio.auth=1d932846ehe76673da6830f14b20dje444
twilio.phone=+1415374893
mail.domain=youremaildomain.com
mail.apikey=key-ffsfjdhijhsdfjh44HHgd246258cbe4ff506365fe8
mail.from=Me <me@youremaildomain.com>
fbclone_keys.properties
apiKeys.load(fis);
} catch (IOException err) {
throw new RuntimeException(err);
}
}
return apiKeys;
}
private JsonNode sendActivationEmail(String destination, String title,
String body) throws UnirestException {
Properties prop = getApiKeys();
HttpResponse<JsonNode> request
= Unirest.post("https://api.mailgun.net/v3/" + prop.
getProperty("mail.domain") + "/messages")
.basicAuth("api", prop.getProperty("mail.apikey"))
.queryString("from", prop.getProperty("mail.from"))
.queryString("to", destination)
.queryString("subject", title)
.queryString("text", body)
.asJson();
return request.getBody();
}
UserService
apiKeys.load(fis);
} catch (IOException err) {
throw new RuntimeException(err);
}
}
return apiKeys;
}
private JsonNode sendActivationEmail(String destination, String title,
String body) throws UnirestException {
Properties prop = getApiKeys();
HttpResponse<JsonNode> request
= Unirest.post("https://api.mailgun.net/v3/" + prop.
getProperty("mail.domain") + "/messages")
.basicAuth("api", prop.getProperty("mail.apikey"))
.queryString("from", prop.getProperty("mail.from"))
.queryString("to", destination)
.queryString("subject", title)
.queryString("text", body)
.asJson();
return request.getBody();
}
UserService
apiKeys.load(fis);
} catch (IOException err) {
throw new RuntimeException(err);
}
}
return apiKeys;
}
private JsonNode sendActivationEmail(String destination, String title,
String body) throws UnirestException {
Properties prop = getApiKeys();
HttpResponse<JsonNode> request
= Unirest.post("https://api.mailgun.net/v3/" + prop.
getProperty("mail.domain") + "/messages")
.basicAuth("api", prop.getProperty("mail.apikey"))
.queryString("from", prop.getProperty("mail.from"))
.queryString("to", destination)
.queryString("subject", title)
.queryString("text", body)
.asJson();
return request.getBody();
}
UserService
if (encoder.matches(password, u.getPassword())) {
return u.getLoginDAO();
}
throw new LoginException("Password authentication error!");
}
throw new LoginException("User not found!");
}
private String createVerificationCode(int length) {
StringBuilder k = new StringBuilder();
Random r = new Random();
for (int iter = 0; iter < length; iter++) {
k.append(r.nextInt(10));
}
return k.toString();
}
public UserDAO refreshUser(String auth) {
User u = users.findByAuthtoken(auth).get(0);
return u.getLoginDAO();
}
public UserDAO signup(UserDAO user) throws SignupException,
UnirestException {
UserService
sendActivationSMS(user.getPhone(), "Activation key: " + u.
getVerificationCode());
}
return u.getLoginDAO();
}
public boolean verifyEmailOrPhone(String authToken, String code,
boolean email) {
User u = users.findByAuthtoken(authToken).get(0);
if (u.getVerificationCode().equals(code)) {
u.setVerificationCode(null);
if (email) {
u.setVerifiedEmailAddress(u.getEmail());
} else {
u.setVerifiedPhone(u.getPhone());
}
users.save(u);
return true;
}
return false;
}
private void setProps(UserDAO user, User u) {
UserService
sendActivationSMS(user.getPhone(), "Activation key: " + u.
getVerificationCode());
}
return u.getLoginDAO();
}
public boolean verifyEmailOrPhone(String authToken, String code,
boolean email) {
User u = users.findByAuthtoken(authToken).get(0);
if (u.getVerificationCode().equals(code)) {
u.setVerificationCode(null);
if (email) {
u.setVerifiedEmailAddress(u.getEmail());
} else {
u.setVerifiedPhone(u.getPhone());
}
users.save(u);
return true;
}
return false;
}
private void setProps(UserDAO user, User u) {
UserService
sendActivationSMS(user.getPhone(), "Activation key: " + u.
getVerificationCode());
}
return u.getLoginDAO();
}
public boolean verifyEmailOrPhone(String authToken, String code,
boolean email) {
User u = users.findByAuthtoken(authToken).get(0);
if (u.getVerificationCode().equals(code)) {
u.setVerificationCode(null);
if (email) {
u.setVerifiedEmailAddress(u.getEmail());
} else {
u.setVerifiedPhone(u.getPhone());
}
users.save(u);
return true;
}
return false;
}
private void setProps(UserDAO user, User u) {
UserService
sendActivationSMS(user.getPhone(), "Activation key: " + u.
getVerificationCode());
}
return u.getLoginDAO();
}
public boolean verifyEmailOrPhone(String authToken, String code,
boolean email) {
User u = users.findByAuthtoken(authToken).get(0);
if (u.getVerificationCode().equals(code)) {
u.setVerificationCode(null);
if (email) {
u.setVerifiedEmailAddress(u.getEmail());
} else {
u.setVerifiedPhone(u.getPhone());
}
users.save(u);
return true;
}
return false;
}
private void setProps(UserDAO user, User u) {
UserService
private void setProps(UserDAO user, User u) {
u.setEmail(user.getEmail());
if (user.getBirthday() != null) {
u.setBirthday(new Date(user.getBirthday()));
}
u.setFamilyName(user.getFamilyName());
u.setFirstName(user.getFirstName());
u.setGender(user.getGender());
u.setPhone(user.getPhone());
}
public void update(UserDAO user) {
User u = users.findByAuthtoken(user.getAuthtoken()).get(0);
setProps(user, u);
if (u.getPassword() != null) {
u.setPassword(encoder.encode(user.getPassword()));
}
users.save(u);
}
public byte[] getAvatar(String userId) {
User u = users.findById(userId).get();
if (u.getAvatar() != null) {
return u.getAvatar().getData();
}
UserService
private void setProps(UserDAO user, User u) {
u.setEmail(user.getEmail());
if (user.getBirthday() != null) {
u.setBirthday(new Date(user.getBirthday()));
}
u.setFamilyName(user.getFamilyName());
u.setFirstName(user.getFirstName());
u.setGender(user.getGender());
u.setPhone(user.getPhone());
}
public void update(UserDAO user) {
User u = users.findByAuthtoken(user.getAuthtoken()).get(0);
setProps(user, u);
if (u.getPassword() != null) {
u.setPassword(encoder.encode(user.getPassword()));
}
users.save(u);
}
public byte[] getAvatar(String userId) {
User u = users.findById(userId).get();
if (u.getAvatar() != null) {
return u.getAvatar().getData();
}
UserService

More Related Content

Similar to Creating a Facebook Clone - Part XXI.pdf

Include- Modularity using design patterns- Fault tolerance and Compone.pdf
Include- Modularity using design patterns- Fault tolerance and Compone.pdfInclude- Modularity using design patterns- Fault tolerance and Compone.pdf
Include- Modularity using design patterns- Fault tolerance and Compone.pdf
RyanF2PLeev
 
AWS에서 자바스크립트 활용 - 서비스와 개발 도구 - AWS Summit Seoul 2017
AWS에서 자바스크립트 활용 - 서비스와 개발 도구 - AWS Summit Seoul 2017AWS에서 자바스크립트 활용 - 서비스와 개발 도구 - AWS Summit Seoul 2017
AWS에서 자바스크립트 활용 - 서비스와 개발 도구 - AWS Summit Seoul 2017
Amazon Web Services Korea
 
Multi client
Multi clientMulti client
Multi client
ganteng8
 

Similar to Creating a Facebook Clone - Part XXI.pdf (20)

Creating a Facebook Clone - Part XXII - Transcript.pdf
Creating a Facebook Clone - Part XXII - Transcript.pdfCreating a Facebook Clone - Part XXII - Transcript.pdf
Creating a Facebook Clone - Part XXII - Transcript.pdf
 
Creating an Uber Clone - Part XIII - Transcript.pdf
Creating an Uber Clone - Part XIII - Transcript.pdfCreating an Uber Clone - Part XIII - Transcript.pdf
Creating an Uber Clone - Part XIII - Transcript.pdf
 
Creating a Facebook Clone - Part XLV.pdf
Creating a Facebook Clone - Part XLV.pdfCreating a Facebook Clone - Part XLV.pdf
Creating a Facebook Clone - Part XLV.pdf
 
Creating a Facebook Clone - Part XLV - Transcript.pdf
Creating a Facebook Clone - Part XLV - Transcript.pdfCreating a Facebook Clone - Part XLV - Transcript.pdf
Creating a Facebook Clone - Part XLV - Transcript.pdf
 
Include- Modularity using design patterns- Fault tolerance and Compone.pdf
Include- Modularity using design patterns- Fault tolerance and Compone.pdfInclude- Modularity using design patterns- Fault tolerance and Compone.pdf
Include- Modularity using design patterns- Fault tolerance and Compone.pdf
 
Creating a Facebook Clone - Part XXV - Transcript.pdf
Creating a Facebook Clone - Part XXV - Transcript.pdfCreating a Facebook Clone - Part XXV - Transcript.pdf
Creating a Facebook Clone - Part XXV - Transcript.pdf
 
Codemotion appengine
Codemotion appengineCodemotion appengine
Codemotion appengine
 
Creating an Uber Clone - Part XXXX.pdf
Creating an Uber Clone - Part XXXX.pdfCreating an Uber Clone - Part XXXX.pdf
Creating an Uber Clone - Part XXXX.pdf
 
Aprimorando sua Aplicação com Ext JS 4 - BrazilJS
Aprimorando sua Aplicação com Ext JS 4 - BrazilJSAprimorando sua Aplicação com Ext JS 4 - BrazilJS
Aprimorando sua Aplicação com Ext JS 4 - BrazilJS
 
Creating a Whatsapp Clone - Part XI.pdf
Creating a Whatsapp Clone - Part XI.pdfCreating a Whatsapp Clone - Part XI.pdf
Creating a Whatsapp Clone - Part XI.pdf
 
Creating an Uber Clone - Part XXXX - Transcript.pdf
Creating an Uber Clone - Part XXXX - Transcript.pdfCreating an Uber Clone - Part XXXX - Transcript.pdf
Creating an Uber Clone - Part XXXX - Transcript.pdf
 
Dependency injection in Scala
Dependency injection in ScalaDependency injection in Scala
Dependency injection in Scala
 
Webauthn Tutorial
Webauthn TutorialWebauthn Tutorial
Webauthn Tutorial
 
AWS에서 자바스크립트 활용 - 서비스와 개발 도구 - AWS Summit Seoul 2017
AWS에서 자바스크립트 활용 - 서비스와 개발 도구 - AWS Summit Seoul 2017AWS에서 자바스크립트 활용 - 서비스와 개발 도구 - AWS Summit Seoul 2017
AWS에서 자바스크립트 활용 - 서비스와 개발 도구 - AWS Summit Seoul 2017
 
Multi client
Multi clientMulti client
Multi client
 
Creating a Facebook Clone - Part XXVIII.pdf
Creating a Facebook Clone - Part XXVIII.pdfCreating a Facebook Clone - Part XXVIII.pdf
Creating a Facebook Clone - Part XXVIII.pdf
 
Creating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdfCreating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdf
 
Solid in practice
Solid in practiceSolid in practice
Solid in practice
 
Lesson_07_Spring_Security_Register_NEW.pdf
Lesson_07_Spring_Security_Register_NEW.pdfLesson_07_Spring_Security_Register_NEW.pdf
Lesson_07_Spring_Security_Register_NEW.pdf
 
Crafting beautiful software
Crafting beautiful softwareCrafting beautiful software
Crafting beautiful software
 

More from ShaiAlmog1

More from ShaiAlmog1 (20)

The Duck Teaches Learn to debug from the masters. Local to production- kill ...
The Duck Teaches  Learn to debug from the masters. Local to production- kill ...The Duck Teaches  Learn to debug from the masters. Local to production- kill ...
The Duck Teaches Learn to debug from the masters. Local to production- kill ...
 
create-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdfcreate-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdf
 
create-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdfcreate-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdf
 
create-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdfcreate-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdf
 
create-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdfcreate-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdf
 
create-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdfcreate-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdf
 
create-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdfcreate-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdf
 
create-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdfcreate-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdf
 
create-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdfcreate-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdf
 
create-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdfcreate-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdf
 
create-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdfcreate-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdf
 
create-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdfcreate-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdf
 
create-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdfcreate-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdf
 
Creating a Whatsapp Clone - Part II.pdf
Creating a Whatsapp Clone - Part II.pdfCreating a Whatsapp Clone - Part II.pdf
Creating a Whatsapp Clone - Part II.pdf
 
Creating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdfCreating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdf
 
Creating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdfCreating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdf
 
Creating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdfCreating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdf
 
Creating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdfCreating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdf
 
Creating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdfCreating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdf
 
Creating a Whatsapp Clone - Part IX.pdf
Creating a Whatsapp Clone - Part IX.pdfCreating a Whatsapp Clone - Part IX.pdf
Creating a Whatsapp Clone - Part IX.pdf
 

Recently uploaded

Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
FIDO Alliance
 
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
Muhammad Subhan
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
FIDO Alliance
 

Recently uploaded (20)

Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties ReimaginedEasier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdf
 
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
 
How to Check GPS Location with a Live Tracker in Pakistan
How to Check GPS Location with a Live Tracker in PakistanHow to Check GPS Location with a Live Tracker in Pakistan
How to Check GPS Location with a Live Tracker in Pakistan
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch Tuesday
 
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptx
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
 
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
“Iamnobody89757” Understanding the Mysterious of Digital Identity.pdf
 
Portal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russePortal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russe
 
Introduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxIntroduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptx
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024
 
AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentation
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps Productivity
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
 

Creating a Facebook Clone - Part XXI.pdf

  • 1. Creating a Facebook Clone - Part XXI
  • 2. @Service public class UserService { @Autowired private UserRepository users; @Autowired private MediaRepository medias; @Autowired private NotificationService notifications; @Autowired private ShadowUserRepository shadows; @Autowired private PasswordEncoder encoder; private Properties apiKeys; private Properties getApiKeys() { UserService
  • 3. @Service public class UserService { @Autowired private UserRepository users; @Autowired private MediaRepository medias; @Autowired private NotificationService notifications; @Autowired private ShadowUserRepository shadows; @Autowired private PasswordEncoder encoder; private Properties apiKeys; private Properties getApiKeys() { UserService
  • 4. @Service public class UserService { @Autowired private UserRepository users; @Autowired private MediaRepository medias; @Autowired private NotificationService notifications; @Autowired private ShadowUserRepository shadows; @Autowired private PasswordEncoder encoder; private Properties apiKeys; private Properties getApiKeys() { UserService
  • 5. .create(); message.getSid(); } public UserDAO login(String email, String phone, String password) throws LoginException { List<User> userList; if (email != null && !email.isEmpty()) { userList = users.findByEmailIgnoreCase(email); } else { userList = users.findByPhone(phone); } if (userList != null && userList.size() == 1) { User u = userList.get(0); if (encoder.matches(password, u.getPassword())) { return u.getLoginDAO(); } throw new LoginException("Password authentication error!"); } throw new LoginException("User not found!"); } private String createVerificationCode(int length) { StringBuilder k = new StringBuilder(); UserService
  • 6. .create(); message.getSid(); } public UserDAO login(String email, String phone, String password) throws LoginException { List<User> userList; if (email != null && !email.isEmpty()) { userList = users.findByEmailIgnoreCase(email); } else { userList = users.findByPhone(phone); } if (userList != null && userList.size() == 1) { User u = userList.get(0); if (encoder.matches(password, u.getPassword())) { return u.getLoginDAO(); } throw new LoginException("Password authentication error!"); } throw new LoginException("User not found!"); } private String createVerificationCode(int length) { StringBuilder k = new StringBuilder(); UserService
  • 7. .create(); message.getSid(); } public UserDAO login(String email, String phone, String password) throws LoginException { List<User> userList; if (email != null && !email.isEmpty()) { userList = users.findByEmailIgnoreCase(email); } else { userList = users.findByPhone(phone); } if (userList != null && userList.size() == 1) { User u = userList.get(0); if (encoder.matches(password, u.getPassword())) { return u.getLoginDAO(); } throw new LoginException("Password authentication error!"); } throw new LoginException("User not found!"); } private String createVerificationCode(int length) { StringBuilder k = new StringBuilder(); UserService
  • 8. .create(); message.getSid(); } public UserDAO login(String email, String phone, String password) throws LoginException { List<User> userList; if (email != null && !email.isEmpty()) { userList = users.findByEmailIgnoreCase(email); } else { userList = users.findByPhone(phone); } if (userList != null && userList.size() == 1) { User u = userList.get(0); if (encoder.matches(password, u.getPassword())) { return u.getLoginDAO(); } throw new LoginException("Password authentication error!"); } throw new LoginException("User not found!"); } private String createVerificationCode(int length) { StringBuilder k = new StringBuilder(); UserService
  • 9. .create(); message.getSid(); } public UserDAO login(String email, String phone, String password) throws LoginException { List<User> userList; if (email != null && !email.isEmpty()) { userList = users.findByEmailIgnoreCase(email); } else { userList = users.findByPhone(phone); } if (userList != null && userList.size() == 1) { User u = userList.get(0); if (encoder.matches(password, u.getPassword())) { return u.getLoginDAO(); } throw new LoginException("Password authentication error!"); } throw new LoginException("User not found!"); } private String createVerificationCode(int length) { StringBuilder k = new StringBuilder(); UserService public class LoginException extends Exception { public LoginException(String errorMessage) { super(errorMessage); } }
  • 10. User u = users.findByAuthtoken(auth).get(0); return u.getLoginDAO(); } public UserDAO signup(UserDAO user) throws SignupException, UnirestException { List<ShadowUser> shadowList = null; if (user.getEmail() != null) { List<User> u = users.findByEmailIgnoreCase(user.getEmail()); if (u != null && u.size() > 0) { throw new SignupException( "The email is already registered!"); } shadowList = shadows.findByEmail(user.getEmail()); } if (user.getPhone() != null) { List<User> u = users.findByPhone(user.getPhone()); if (u != null && u.size() > 0) { throw new SignupException( "The phone number is already registered!"); } shadowList = shadows.findByPhone(user.getPhone()); } UserService
  • 11. User u = users.findByAuthtoken(auth).get(0); return u.getLoginDAO(); } public UserDAO signup(UserDAO user) throws SignupException, UnirestException { List<ShadowUser> shadowList = null; if (user.getEmail() != null) { List<User> u = users.findByEmailIgnoreCase(user.getEmail()); if (u != null && u.size() > 0) { throw new SignupException( "The email is already registered!"); } shadowList = shadows.findByEmail(user.getEmail()); } if (user.getPhone() != null) { List<User> u = users.findByPhone(user.getPhone()); if (u != null && u.size() > 0) { throw new SignupException( "The phone number is already registered!"); } shadowList = shadows.findByPhone(user.getPhone()); } UserService
  • 12. User u = users.findByAuthtoken(auth).get(0); return u.getLoginDAO(); } public UserDAO signup(UserDAO user) throws SignupException, UnirestException { List<ShadowUser> shadowList = null; if (user.getEmail() != null) { List<User> u = users.findByEmailIgnoreCase(user.getEmail()); if (u != null && u.size() > 0) { throw new SignupException( "The email is already registered!"); } shadowList = shadows.findByEmail(user.getEmail()); } if (user.getPhone() != null) { List<User> u = users.findByPhone(user.getPhone()); if (u != null && u.size() > 0) { throw new SignupException( "The phone number is already registered!"); } shadowList = shadows.findByPhone(user.getPhone()); } UserService
  • 13. User u = users.findByAuthtoken(auth).get(0); return u.getLoginDAO(); } public UserDAO signup(UserDAO user) throws SignupException, UnirestException { List<ShadowUser> shadowList = null; if (user.getEmail() != null) { List<User> u = users.findByEmailIgnoreCase(user.getEmail()); if (u != null && u.size() > 0) { throw new SignupException( "The email is already registered!"); } shadowList = shadows.findByEmail(user.getEmail()); } if (user.getPhone() != null) { List<User> u = users.findByPhone(user.getPhone()); if (u != null && u.size() > 0) { throw new SignupException( "The phone number is already registered!"); } shadowList = shadows.findByPhone(user.getPhone()); } UserService
  • 14. List<User> u = users.findByPhone(user.getPhone()); if (u != null && u.size() > 0) { throw new SignupException( "The phone number is already registered!"); } shadowList = shadows.findByPhone(user.getPhone()); } User u = new User(); setProps(user, u); u.setPassword(encoder.encode(user.getPassword())); u.setAuthtoken(UUID.randomUUID().toString()); u.setVerificationCode(createVerificationCode(4)); if (shadowList != null && !shadowList.isEmpty()) { Set<User> friendSuggestions = new HashSet<>(); for (ShadowUser s : shadowList) { friendSuggestions.add(s.getOwner()); } u.setPeopleYouMayKnow(friendSuggestions); } users.save(u); UserService
  • 15. List<User> u = users.findByPhone(user.getPhone()); if (u != null && u.size() > 0) { throw new SignupException( "The phone number is already registered!"); } shadowList = shadows.findByPhone(user.getPhone()); } User u = new User(); setProps(user, u); u.setPassword(encoder.encode(user.getPassword())); u.setAuthtoken(UUID.randomUUID().toString()); u.setVerificationCode(createVerificationCode(4)); if (shadowList != null && !shadowList.isEmpty()) { Set<User> friendSuggestions = new HashSet<>(); for (ShadowUser s : shadowList) { friendSuggestions.add(s.getOwner()); } u.setPeopleYouMayKnow(friendSuggestions); } users.save(u); UserService
  • 16. List<User> u = users.findByPhone(user.getPhone()); if (u != null && u.size() > 0) { throw new SignupException( "The phone number is already registered!"); } shadowList = shadows.findByPhone(user.getPhone()); } User u = new User(); setProps(user, u); u.setPassword(encoder.encode(user.getPassword())); u.setAuthtoken(UUID.randomUUID().toString()); u.setVerificationCode(createVerificationCode(4)); if (shadowList != null && !shadowList.isEmpty()) { Set<User> friendSuggestions = new HashSet<>(); for (ShadowUser s : shadowList) { friendSuggestions.add(s.getOwner()); } u.setPeopleYouMayKnow(friendSuggestions); } users.save(u); UserService
  • 17. List<User> u = users.findByPhone(user.getPhone()); if (u != null && u.size() > 0) { throw new SignupException( "The phone number is already registered!"); } shadowList = shadows.findByPhone(user.getPhone()); } User u = new User(); setProps(user, u); u.setPassword(encoder.encode(user.getPassword())); u.setAuthtoken(UUID.randomUUID().toString()); u.setVerificationCode(createVerificationCode(4)); if (shadowList != null && !shadowList.isEmpty()) { Set<User> friendSuggestions = new HashSet<>(); for (ShadowUser s : shadowList) { friendSuggestions.add(s.getOwner()); } u.setPeopleYouMayKnow(friendSuggestions); } users.save(u); UserService
  • 18. List<User> u = users.findByPhone(user.getPhone()); if (u != null && u.size() > 0) { throw new SignupException( "The phone number is already registered!"); } shadowList = shadows.findByPhone(user.getPhone()); } User u = new User(); setProps(user, u); u.setPassword(encoder.encode(user.getPassword())); u.setAuthtoken(UUID.randomUUID().toString()); u.setVerificationCode(createVerificationCode(4)); if (shadowList != null && !shadowList.isEmpty()) { Set<User> friendSuggestions = new HashSet<>(); for (ShadowUser s : shadowList) { friendSuggestions.add(s.getOwner()); } u.setPeopleYouMayKnow(friendSuggestions); } users.save(u); UserService
  • 19. for (ShadowUser s : shadowList) { friendSuggestions.add(s.getOwner()); } u.setPeopleYouMayKnow(friendSuggestions); } users.save(u); if (user.getEmail() != null) { sendActivationEmail(user.getEmail(), "Please Activate Your Facebook Clone Account", "Activation key: " + u.getVerificationCode()); } else { sendActivationSMS(user.getPhone(), "Activation key: " + u. getVerificationCode()); } return u.getLoginDAO(); } public boolean verifyEmailOrPhone(String authToken, String code, boolean email) { User u = users.findByAuthtoken(authToken).get(0); if (u.getVerificationCode().equals(code)) { UserService
  • 20. for (ShadowUser s : shadowList) { friendSuggestions.add(s.getOwner()); } u.setPeopleYouMayKnow(friendSuggestions); } users.save(u); if (user.getEmail() != null) { sendActivationEmail(user.getEmail(), "Please Activate Your Facebook Clone Account", "Activation key: " + u.getVerificationCode()); } else { sendActivationSMS(user.getPhone(), "Activation key: " + u. getVerificationCode()); } return u.getLoginDAO(); } public boolean verifyEmailOrPhone(String authToken, String code, boolean email) { User u = users.findByAuthtoken(authToken).get(0); if (u.getVerificationCode().equals(code)) { UserService
  • 21. for (ShadowUser s : shadowList) { friendSuggestions.add(s.getOwner()); } u.setPeopleYouMayKnow(friendSuggestions); } users.save(u); if (user.getEmail() != null) { sendActivationEmail(user.getEmail(), "Please Activate Your Facebook Clone Account", "Activation key: " + u.getVerificationCode()); } else { sendActivationSMS(user.getPhone(), "Activation key: " + u. getVerificationCode()); } return u.getLoginDAO(); } public boolean verifyEmailOrPhone(String authToken, String code, boolean email) { User u = users.findByAuthtoken(authToken).get(0); if (u.getVerificationCode().equals(code)) { UserService
  • 22. } else { u.setVerifiedPhone(u.getPhone()); } users.save(u); return true; } return false; } private void setProps(UserDAO user, User u) { u.setEmail(user.getEmail()); if (user.getBirthday() != null) { u.setBirthday(new Date(user.getBirthday())); } u.setFamilyName(user.getFamilyName()); u.setFirstName(user.getFirstName()); u.setGender(user.getGender()); u.setPhone(user.getPhone()); } public void update(UserDAO user) { User u = users.findByAuthtoken(user.getAuthtoken()).get(0); setProps(user, u); if (u.getPassword() != null) { UserService
  • 23. } else { u.setVerifiedPhone(u.getPhone()); } users.save(u); return true; } return false; } private void setProps(UserDAO user, User u) { u.setEmail(user.getEmail()); if (user.getBirthday() != null) { u.setBirthday(new Date(user.getBirthday())); } u.setFamilyName(user.getFamilyName()); u.setFirstName(user.getFirstName()); u.setGender(user.getGender()); u.setPhone(user.getPhone()); } public void update(UserDAO user) { User u = users.findByAuthtoken(user.getAuthtoken()).get(0); setProps(user, u); if (u.getPassword() != null) { UserService
  • 24. User u = userList.get(0); if (encoder.matches(password, u.getPassword())) { return u.getLoginDAO(); } throw new LoginException("Password authentication error!"); } throw new LoginException("User not found!"); } private String createVerificationCode(int length) { StringBuilder k = new StringBuilder(); Random r = new Random(); for (int iter = 0; iter < length; iter++) { k.append(r.nextInt(10)); } return k.toString(); } public UserDAO refreshUser(String auth) { User u = users.findByAuthtoken(auth).get(0); return u.getLoginDAO(); } public UserDAO signup(UserDAO user) throws SignupException, UserService
  • 25. User u = userList.get(0); if (encoder.matches(password, u.getPassword())) { return u.getLoginDAO(); } throw new LoginException("Password authentication error!"); } throw new LoginException("User not found!"); } private String createVerificationCode(int length) { StringBuilder k = new StringBuilder(); Random r = new Random(); for (int iter = 0; iter < length; iter++) { k.append(r.nextInt(10)); } return k.toString(); } public UserDAO refreshUser(String auth) { User u = users.findByAuthtoken(auth).get(0); return u.getLoginDAO(); } public UserDAO signup(UserDAO user) throws SignupException, UserService
  • 26. User u = userList.get(0); if (encoder.matches(password, u.getPassword())) { return u.getLoginDAO(); } throw new LoginException("Password authentication error!"); } throw new LoginException("User not found!"); } private String createVerificationCode(int length) { StringBuilder k = new StringBuilder(); Random r = new Random(); for (int iter = 0; iter < length; iter++) { k.append(r.nextInt(10)); } return k.toString(); } public UserDAO refreshUser(String auth) { User u = users.findByAuthtoken(auth).get(0); return u.getLoginDAO(); } public UserDAO signup(UserDAO user) throws SignupException, UserService
  • 27. .queryString("to", destination) .queryString("subject", title) .queryString("text", body) .asJson(); return request.getBody(); } private void sendActivationSMS(String number, String text) { Properties prop = getApiKeys(); Twilio.init(prop.getProperty("twilio.sid"), prop.getProperty( "twilio.auth")); Message message = Message .creator(new PhoneNumber(number), new PhoneNumber(prop.getProperty("twilio.phone")), text) .create(); message.getSid(); } public UserDAO login(String email, String phone, String password) throws LoginException { List<User> userList; UserService
  • 28. .queryString("to", destination) .queryString("subject", title) .queryString("text", body) .asJson(); return request.getBody(); } private void sendActivationSMS(String number, String text) { Properties prop = getApiKeys(); Twilio.init(prop.getProperty("twilio.sid"), prop.getProperty( "twilio.auth")); Message message = Message .creator(new PhoneNumber(number), new PhoneNumber(prop.getProperty("twilio.phone")), text) .create(); message.getSid(); } public UserDAO login(String email, String phone, String password) throws LoginException { List<User> userList; UserService
  • 29. .queryString("to", destination) .queryString("subject", title) .queryString("text", body) .asJson(); return request.getBody(); } private void sendActivationSMS(String number, String text) { Properties prop = getApiKeys(); Twilio.init(prop.getProperty("twilio.sid"), prop.getProperty( "twilio.auth")); Message message = Message .creator(new PhoneNumber(number), new PhoneNumber(prop.getProperty("twilio.phone")), text) .create(); message.getSid(); } public UserDAO login(String email, String phone, String password) throws LoginException { List<User> userList; UserService
  • 30. .queryString("to", destination) .queryString("subject", title) .queryString("text", body) .asJson(); return request.getBody(); } private void sendActivationSMS(String number, String text) { Properties prop = getApiKeys(); Twilio.init(prop.getProperty("twilio.sid"), prop.getProperty( "twilio.auth")); Message message = Message .creator(new PhoneNumber(number), new PhoneNumber(prop.getProperty("twilio.phone")), text) .create(); message.getSid(); } public UserDAO login(String email, String phone, String password) throws LoginException { List<User> userList; UserService
  • 31. @Autowired private PasswordEncoder encoder; private Properties apiKeys; private Properties getApiKeys() { if (apiKeys == null) { apiKeys = new Properties(); try (FileInputStream fis = new FileInputStream( System.getProperty("user.home") + File.separator + "fbclone_keys.properties")) { apiKeys.load(fis); } catch (IOException err) { throw new RuntimeException(err); } } return apiKeys; } private JsonNode sendActivationEmail(String destination, String title, String body) throws UnirestException { Properties prop = getApiKeys(); HttpResponse<JsonNode> request UserService
  • 32. @Autowired private PasswordEncoder encoder; private Properties apiKeys; private Properties getApiKeys() { if (apiKeys == null) { apiKeys = new Properties(); try (FileInputStream fis = new FileInputStream( System.getProperty("user.home") + File.separator + "fbclone_keys.properties")) { apiKeys.load(fis); } catch (IOException err) { throw new RuntimeException(err); } } return apiKeys; } private JsonNode sendActivationEmail(String destination, String title, String body) throws UnirestException { Properties prop = getApiKeys(); HttpResponse<JsonNode> request UserService
  • 33. @Autowired private PasswordEncoder encoder; private Properties apiKeys; private Properties getApiKeys() { if (apiKeys == null) { apiKeys = new Properties(); try (FileInputStream fis = new FileInputStream( System.getProperty("user.home") + File.separator + "fbclone_keys.properties")) { apiKeys.load(fis); } catch (IOException err) { throw new RuntimeException(err); } } return apiKeys; } private JsonNode sendActivationEmail(String destination, String title, String body) throws UnirestException { Properties prop = getApiKeys(); HttpResponse<JsonNode> request UserService
  • 34. @Autowired private PasswordEncoder encoder; private Properties apiKeys; private Properties getApiKeys() { if (apiKeys == null) { apiKeys = new Properties(); try (FileInputStream fis = new FileInputStream( System.getProperty("user.home") + File.separator + "fbclone_keys.properties")) { apiKeys.load(fis); } catch (IOException err) { throw new RuntimeException(err); } } return apiKeys; } private JsonNode sendActivationEmail(String destination, String title, String body) throws UnirestException { Properties prop = getApiKeys(); HttpResponse<JsonNode> request UserService
  • 35. © Codename One 2017 all rights reserved
  • 37. apiKeys.load(fis); } catch (IOException err) { throw new RuntimeException(err); } } return apiKeys; } private JsonNode sendActivationEmail(String destination, String title, String body) throws UnirestException { Properties prop = getApiKeys(); HttpResponse<JsonNode> request = Unirest.post("https://api.mailgun.net/v3/" + prop. getProperty("mail.domain") + "/messages") .basicAuth("api", prop.getProperty("mail.apikey")) .queryString("from", prop.getProperty("mail.from")) .queryString("to", destination) .queryString("subject", title) .queryString("text", body) .asJson(); return request.getBody(); } UserService
  • 38. apiKeys.load(fis); } catch (IOException err) { throw new RuntimeException(err); } } return apiKeys; } private JsonNode sendActivationEmail(String destination, String title, String body) throws UnirestException { Properties prop = getApiKeys(); HttpResponse<JsonNode> request = Unirest.post("https://api.mailgun.net/v3/" + prop. getProperty("mail.domain") + "/messages") .basicAuth("api", prop.getProperty("mail.apikey")) .queryString("from", prop.getProperty("mail.from")) .queryString("to", destination) .queryString("subject", title) .queryString("text", body) .asJson(); return request.getBody(); } UserService
  • 39. apiKeys.load(fis); } catch (IOException err) { throw new RuntimeException(err); } } return apiKeys; } private JsonNode sendActivationEmail(String destination, String title, String body) throws UnirestException { Properties prop = getApiKeys(); HttpResponse<JsonNode> request = Unirest.post("https://api.mailgun.net/v3/" + prop. getProperty("mail.domain") + "/messages") .basicAuth("api", prop.getProperty("mail.apikey")) .queryString("from", prop.getProperty("mail.from")) .queryString("to", destination) .queryString("subject", title) .queryString("text", body) .asJson(); return request.getBody(); } UserService
  • 40. if (encoder.matches(password, u.getPassword())) { return u.getLoginDAO(); } throw new LoginException("Password authentication error!"); } throw new LoginException("User not found!"); } private String createVerificationCode(int length) { StringBuilder k = new StringBuilder(); Random r = new Random(); for (int iter = 0; iter < length; iter++) { k.append(r.nextInt(10)); } return k.toString(); } public UserDAO refreshUser(String auth) { User u = users.findByAuthtoken(auth).get(0); return u.getLoginDAO(); } public UserDAO signup(UserDAO user) throws SignupException, UnirestException { UserService
  • 41. sendActivationSMS(user.getPhone(), "Activation key: " + u. getVerificationCode()); } return u.getLoginDAO(); } public boolean verifyEmailOrPhone(String authToken, String code, boolean email) { User u = users.findByAuthtoken(authToken).get(0); if (u.getVerificationCode().equals(code)) { u.setVerificationCode(null); if (email) { u.setVerifiedEmailAddress(u.getEmail()); } else { u.setVerifiedPhone(u.getPhone()); } users.save(u); return true; } return false; } private void setProps(UserDAO user, User u) { UserService
  • 42. sendActivationSMS(user.getPhone(), "Activation key: " + u. getVerificationCode()); } return u.getLoginDAO(); } public boolean verifyEmailOrPhone(String authToken, String code, boolean email) { User u = users.findByAuthtoken(authToken).get(0); if (u.getVerificationCode().equals(code)) { u.setVerificationCode(null); if (email) { u.setVerifiedEmailAddress(u.getEmail()); } else { u.setVerifiedPhone(u.getPhone()); } users.save(u); return true; } return false; } private void setProps(UserDAO user, User u) { UserService
  • 43. sendActivationSMS(user.getPhone(), "Activation key: " + u. getVerificationCode()); } return u.getLoginDAO(); } public boolean verifyEmailOrPhone(String authToken, String code, boolean email) { User u = users.findByAuthtoken(authToken).get(0); if (u.getVerificationCode().equals(code)) { u.setVerificationCode(null); if (email) { u.setVerifiedEmailAddress(u.getEmail()); } else { u.setVerifiedPhone(u.getPhone()); } users.save(u); return true; } return false; } private void setProps(UserDAO user, User u) { UserService
  • 44. sendActivationSMS(user.getPhone(), "Activation key: " + u. getVerificationCode()); } return u.getLoginDAO(); } public boolean verifyEmailOrPhone(String authToken, String code, boolean email) { User u = users.findByAuthtoken(authToken).get(0); if (u.getVerificationCode().equals(code)) { u.setVerificationCode(null); if (email) { u.setVerifiedEmailAddress(u.getEmail()); } else { u.setVerifiedPhone(u.getPhone()); } users.save(u); return true; } return false; } private void setProps(UserDAO user, User u) { UserService
  • 45. private void setProps(UserDAO user, User u) { u.setEmail(user.getEmail()); if (user.getBirthday() != null) { u.setBirthday(new Date(user.getBirthday())); } u.setFamilyName(user.getFamilyName()); u.setFirstName(user.getFirstName()); u.setGender(user.getGender()); u.setPhone(user.getPhone()); } public void update(UserDAO user) { User u = users.findByAuthtoken(user.getAuthtoken()).get(0); setProps(user, u); if (u.getPassword() != null) { u.setPassword(encoder.encode(user.getPassword())); } users.save(u); } public byte[] getAvatar(String userId) { User u = users.findById(userId).get(); if (u.getAvatar() != null) { return u.getAvatar().getData(); } UserService
  • 46. private void setProps(UserDAO user, User u) { u.setEmail(user.getEmail()); if (user.getBirthday() != null) { u.setBirthday(new Date(user.getBirthday())); } u.setFamilyName(user.getFamilyName()); u.setFirstName(user.getFirstName()); u.setGender(user.getGender()); u.setPhone(user.getPhone()); } public void update(UserDAO user) { User u = users.findByAuthtoken(user.getAuthtoken()).get(0); setProps(user, u); if (u.getPassword() != null) { u.setPassword(encoder.encode(user.getPassword())); } users.save(u); } public byte[] getAvatar(String userId) { User u = users.findById(userId).get(); if (u.getAvatar() != null) { return u.getAvatar().getData(); } UserService