Search in sources :

Example 1 with User

use of org.youngmonkeys.example.ezyhttp.login.entity.User in project ezyfox-examples by tvd12.

the class UserService method saveFacebookUserInfo.

@Override
public User saveFacebookUserInfo(com.restfb.types.User userFacebook) {
    User user = new User();
    user.setThirdPartyId(userFacebook.getId());
    user.setEmail(userFacebook.getId());
    user.setFullName(userFacebook.getName());
    user.setAccountType(AccountType.FACEBOOK);
    user.setDeleted(false);
    user.setStatus(UserStatus.REGISTER);
    userRepository.save(user);
    return user;
}
Also used : User(org.youngmonkeys.example.ezyhttp.login.entity.User)

Example 2 with User

use of org.youngmonkeys.example.ezyhttp.login.entity.User in project ezyfox-examples by tvd12.

the class UserService method saveGoogleUserInfo.

@Override
public User saveGoogleUserInfo(Userinfo googleUserInfo) {
    // create new user information
    User user = new User();
    user.setEmail(googleUserInfo.getEmail());
    user.setFullName(googleUserInfo.getName());
    user.setLastName(googleUserInfo.getFamilyName());
    user.setFirstName(googleUserInfo.getGivenName());
    user.setAvatarURL(googleUserInfo.getPicture());
    user.setAccountType(AccountType.GOOGLE);
    user.setDeleted(false);
    user.setStatus(UserStatus.REGISTER);
    userRepository.save(user);
    return user;
}
Also used : User(org.youngmonkeys.example.ezyhttp.login.entity.User)

Example 3 with User

use of org.youngmonkeys.example.ezyhttp.login.entity.User in project ezyfox-examples by tvd12.

the class LoginController method loginWithGoogle.

@DoGet("/google-login-callback")
public Object loginWithGoogle(@RequestParam String code) {
    String googleAccessToken = googleService.getAccessToken(code);
    if (googleAccessToken == null) {
        return Redirect.to("/login-error");
    }
    Userinfo googleUserInfo = googleService.getUserInfoByAccessToken(googleAccessToken);
    if (googleUserInfo == null) {
        return Redirect.to("/login-error");
    }
    boolean userExisted = true;
    User user = userService.getUserInfoByEmail(googleUserInfo.getEmail());
    if (user == null) {
        userExisted = false;
        user = userService.saveGoogleUserInfo(googleUserInfo);
        userDataService.saveGoogleToken(user.getId(), googleAccessToken);
    }
    String accessToken = authenticationService.generateAccessToken(user.getId());
    return userExisted && user.getStatus() == UserStatus.UPDATED ? Redirect.builder().uri("/home").addCookie("accessToken", accessToken).build() : Redirect.builder().uri("/user/update").addCookie("accessToken", accessToken).build();
}
Also used : User(org.youngmonkeys.example.ezyhttp.login.entity.User) Userinfo(com.google.api.services.oauth2.model.Userinfo)

Example 4 with User

use of org.youngmonkeys.example.ezyhttp.login.entity.User in project ezyfox-examples by tvd12.

the class LoginController method loginWithFacebook.

@DoGet("/facebook-login-callback")
public Object loginWithFacebook(@RequestParam String code) {
    String facebookAccessToken = facebookService.getAccessToken(code);
    if (facebookAccessToken == null) {
        return Redirect.to("/login-error");
    }
    com.restfb.types.User userFacebook = facebookService.getUserInfoByAccessToken(facebookAccessToken);
    if (userFacebook == null) {
        return Redirect.to("/login-error");
    }
    boolean userExisted = true;
    User user = userService.getUserByThirdPartyId(userFacebook.getId());
    if (user == null) {
        userExisted = false;
        user = userService.saveFacebookUserInfo(userFacebook);
        userDataService.saveFacebookToken(user.getId(), facebookAccessToken);
    }
    String accessToken = authenticationService.generateAccessToken(user.getId());
    return userExisted && user.getStatus() == UserStatus.UPDATED ? Redirect.builder().uri("/home").addCookie("accessToken", accessToken).build() : Redirect.builder().uri("/user/update").addCookie("accessToken", accessToken).build();
}
Also used : User(org.youngmonkeys.example.ezyhttp.login.entity.User)

Example 5 with User

use of org.youngmonkeys.example.ezyhttp.login.entity.User in project ezyfox-examples by tvd12.

the class UserController method userSavePost.

@DoPost("/user/save")
public Object userSavePost(@UserId long userId, @RequestBody UpdateUserRequest request) {
    User user = userService.getUserById(userId);
    if (user == null) {
        throw new UserNotFoundException("user with id: " + userId + " not found");
    }
    user.setEmail(request.getEmail());
    user.setFullName(request.getFirstName() + " " + request.getLastName());
    user.setFirstName(request.getFirstName());
    user.setLastName(request.getLastName());
    user.setStatus(UserStatus.UPDATED);
    user.setPassword(EzySHA256.cryptUtfToLowercase(request.getPassword()));
    userService.saveUser(user);
    return Redirect.builder().uri("/home").build();
}
Also used : UserNotFoundException(org.youngmonkeys.example.ezyhttp.login.exception.UserNotFoundException) User(org.youngmonkeys.example.ezyhttp.login.entity.User) DoPost(com.tvd12.ezyhttp.server.core.annotation.DoPost)

Aggregations

User (org.youngmonkeys.example.ezyhttp.login.entity.User)5 Userinfo (com.google.api.services.oauth2.model.Userinfo)1 DoPost (com.tvd12.ezyhttp.server.core.annotation.DoPost)1 UserNotFoundException (org.youngmonkeys.example.ezyhttp.login.exception.UserNotFoundException)1