I have a web app with JavaScript front-end which communicates with back-end via REST API (AJAX calls). Front-end have a Sign Up page. When user decides to register, front-end makes REST call to register that user (POST request sent to /users resource URL).
Users can also login/register via Facebook. When someone choose to login via Facebook my app don't know if user is already registered or I should register him. For now I have a simple implementation of that process. When someone logs via Facebook:
- HEAD call is made to check if user exists in users collection (HEAD /users/id)
- If user does not exist, POST call is made to register new user.
What I don't like about that solution is that:
- Two calls need to be made to check if user was registered and then register that user.
- When I make HEAD call to check if user exists and user does not exist I get 404 logged in JS console. It is normal situation when user does not exist (was not registered before), but when someone looks in the console it may seem like something went wrong.
I could skip HEAD call and make one POST call and when it turns out user was registered before I can return 409 from POST REST call. I still don't like that approach because I still would have an error in JS console (409) and second bad thing is that POST register request would be sent everytime Facebook user logs in even if he was registered before.
I've looked into some big websites and they seem to have one AJAX call when signing uin/up via Facebook. Adding two separate paths: sign up via facebook and sign in via Facebook will not resolve the issue since usually when someone choses to login via Facebook every page internally redirects to sign up process and I want to do such thing.
So my question is what is the best way to design such communication from REST APi side?