Kleber Mota
Kleber Mota

Reputation: 9065

Implementation for Implicit social signup in spring-boot project

By adding the spring-social signin / signup to my current spring-boot project, through the implementation of the classes ConnectionSignUp and SignInAdapter, the application, after the authorization with the social network website, it's requiring the implementation of a mapping for "/signup" in my controller. In this method, I do basically the same what I already have implemented on the ConectionSignUp method. Anyone knows what I can do to avoid this duplicity, and direct the application to my ConnectionSignUp class instead of one extra method on my controller?

my implementation so far includes the following classes:

ConnectionSIgnUp

@Component
public class CustomConnectionSignUp implements ConnectionSignUp {
  @Autowired
  private UsuarioDao account;

  @Autowired
  private JavaMailSender mailSender;

  public String execute(Connection<?> connection) {
    UserProfile profile = connection.fetchUserProfile();

    String user;
    try {
      Usuario novo = new Usuario(profile.getUsername(),UUID.randomUUID().toString().replaceAll("-", ""),null,null,false,true);
      account.insert(novo);
      return novo.getLogin();
    } catch (Exception e) {
      return null;
    }
  }
}

SignInAdapter

@Component
public class CustomSignInAdapter implements SignInAdapter {
  @Autowired
  private SocialUserDetailsService socialUserDetailsService;

  public String signIn(String userId, Connection<?> connection, NativeWebRequest request) {
    SocialUserDetails user = socialUserDetailsService.loadUserByUserId(userId);
    if(user != null)
      SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(user.getUserId(), null, null));
    return null;
  }
}

SocialUserDetailsService

@Service

    public class CustomSocialUserDetailsService implements SocialUserDetailsService {
      @Autowired
      private UsuarioDao account;

      public SocialUserDetails loadUserByUserId(String userId) {
        for(Usuario usuario : account.select())
          if(usuario.getLogin().equals(userId))
            return new SocialUser(usuario.getLogin(), usuario.getSenha(), usuario.isEnabled(), usuario.isAccountNonExpired(), usuario.isCredentialsNonExpired(), usuario.isAccountNonLocked(), usuario.getAuthorities());
        return null;
      }
    }

application.properties

# SPRING SOCIAL (SocialWebAutoConfiguration)
spring.social.auto-connection-views=false

# SPRING SOCIAL FACEBOOK (FacebookAutoConfiguration)
spring.social.facebook.app-id=...
spring.social.facebook.app-secret=...

# SPRING SOCIAL TWITTER (TwitterAutoConfiguration)
spring.social.twitter.app-id=...
spring.social.twitter.app-secret=...

Upvotes: 1

Views: 488

Answers (1)

gnom_7
gnom_7

Reputation: 121

It seems to me that you didn't Inject ConnectionSignUp into UsersConnectionRepository. Therefore Spring tries redirect user to page with sign up form. To perform implicit sign up try to pass your ConnectionSignUp bean to setConnectionSignUp().

Upvotes: 1

Related Questions