spring-socialspring-social-facebook

Update Facebook profile with Spring Social


I'm using Facebook login on my site with Spring Social. I'm also creating an user in my database with specific application preferences for each Facebook user. After a user logs in, I want to update his personal information, like name and email, in my db. What will be the best way to do it and how?


Solution

  • I finally did it by implementing ApplicationListener<InteractiveAuthenticationSuccessEvent>, which is called after the user logs in.

    @Override
    public void onApplicationEvent(InteractiveAuthenticationSuccessEvent authEvent) {
        Authentication auth = authEvent.getAuthentication();
        if (auth instanceof SocialAuthenticationToken && auth.getPrincipal() instanceof User) {
            // every time a user authenticates through a social network, then we update his info from there
            User user = (User)auth.getPrincipal();
            // ... update user using ((SocialAuthenticationToken)auth).getConnection()
            // ... and save it
        }
    }
    

    There's also possible to do it by overriding ConnectionRepository#updateConnection.