Search code examples
javaangularjsfacebookspring-socialspring-social-facebook

How to logout from Facebook using Spring Social


I am having trouble trying to implement a logout button using the Facebook API in Spring Social.

To logout, do I have to call a URL like I did in the login button below?

I was able to implement the login calling the URL '/connect/facebook' as follows:

<form action="/connect/facebook" method="POST">
    <input type="hidden" name="scope" value="read_stream,email" />
    <md-button type="submit">
        <i class="fa fa-facebook-square"></i> Login
    </md-button>
</form>

Also I extended the ConnectController to redirect the user to the home page:

@Controller
public class FacebookController extends ConnectController {

    @Inject
    public FacebookController(ConnectionFactoryLocator connectionFactoryLocator, ConnectionRepository connectionRepository) {
        super(connectionFactoryLocator, connectionRepository);
    }

    @Override
    protected String connectedView(String providerId){
        return "redirect:/";
    }

}

I am using AngularJS to fetch the user, if it is connected, below is my service responsible for it:

function login() {
    var request = $http({
        method: "GET",
        url: "/home/user"
    });

    return (request.then(handleSuccess, handleError));
}

And the rest controller in the backend to find or register the user:

@RestController
public class LoginController {

    @Autowired
    private Facebook facebook;

    @Autowired
    private UsrService usrService;

    @RequestMapping(value = "/home/user", method = RequestMethod.GET)
    public Usr findUser() {
        Usr user;

        if (!facebook.isAuthorized()) {
            return null;
        }

        String name = facebook.userOperations().getUserProfile().getName();
        String email = facebook.userOperations().getUserProfile().getEmail();
        byte[] avatar = facebook.userOperations().getUserProfileImage(ImageType.SQUARE);

        user = usrService.find(email);

        // saves the new user
        if (user == null) {
            user = new Usr(email, name, avatar, 0);
            user.setName(name);
            user.setAvatar(avatar);
            user.setLikes(0);
            usrService.save(user);
        }

        return user;
    }
}

I couldn't find any solution close to the way I implemented the login, also I checked the spring-social samples on github, but their implementations are quite different then mine.

Thanks.


Solution

  • For those who might've been with same problem then I did:

    I gave another look at spring social documentation and found out that I just needed to make a DELETE request at the '/connect/facebook'.

    This is found at section 4.3.4