// Mark current user as actual
userTracker.addUserId(userId, ApplicationUtils.getSession().getId());
}
public boolean authenticateWithFacebook() {
JSONObject userInfo = fBean.getUserInfo();
try {
String pictureUrl = userInfo.getJSONObject("picture").getJSONObject("data").getString("url");
userBean.setFbPhotoUrl(pictureUrl);
String facebookId = userInfo.getString("id");
if (!userBean.isLoggedIn()) { // user is not logged in
user = userBean.facebookLogIn(facebookId); // try logging with Facebook
if (user != null) {
addToTracker(user.getId());
return true;
}
// Facebook id was not found, creating new account
User newUser = new User();
newUser.setFbId(facebookId);
newUser.setFirstName(userInfo.getString("first_name"));
newUser.setSecondName(userInfo.getString("last_name"));
newUser.setEmail(userInfo.getString("email"));
String username = userInfo.has("username") ? userInfo.getString("username") : userInfo.getString("first_name");
newUser.setLogin(username);
String sex = userInfo.getString("gender");
newUser.setSex(sex.equals("male") ? Sex.MALE : Sex.FEMALE);
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
newUser.setBirthDate(sdf.parse(userInfo.getString("birthday")));
// random password, the user will not be using this to log in
newUser.setPasswordHash(HashUtils.hash("facebook" + System.currentTimeMillis()));
userAction.register(newUser);