Getting Facebook login running on a mobile hybrid app is half the battle. The other half shares credentials with the backend. I just finished implementing this project against flex, so I decided to share what worked.
Let the user paste their username and password into the Ionic and POST app on their server, and then use those to authenticate the user on facebook and get a token for him. Obviously, this completely ignores the purpose of OAuth, but I think it will work.
That would be a very bad idea (as you pointed out, this violates OAuth principles), and in fact it will not work. There is no endpoint where you can programmatically pass Facebook your username and password and get a response in return (legally and without scrapers). Instead, a token with a user interaction is required to obtain a token, regardless of whether it was executed on the interface or on the backend. Consider the case of two-factor authentication on Facebook , where the user must receive and enter the code sent to their mobile phone.
Use Facebooks Javascript authentication, which returns a token to the application. Then I can send the token to my server to save it for later use.
Yes, thatβs how it should be done. This is called cross-client authentication. Facebook has a page that explains authentication tokens , which is conceptually useful and discusses many different scenarios, but unfortunately does not contain many useful snippets of code.
You can directly transfer the access token to the backend as part of the login process. The backend can then confirm the token. Assuming you use the standard Flask-Security and Flask-Social on the backend, you can wrap the login window on the flash social network to authenticate the user using the token passed from the interface. You can find sample code in this context: https://gist.github.com/lrettig/7ca94ba45961207a7bd3
Also note that this token usually only lasts a couple of hours. If you need to use the backend to use the Facebook SDK on behalf of the user on an ongoing basis, you need to replace it with a long-term token .
Another note that confused me a bit: I noticed that after authentication with Facebook on the interface, I was given an access token, while using the Python SDK on the backend, I was instead transferred the code. to exchange for a token before any request can be executed. I'm not sure why the difference is, but codes and tokens are also described in Facebook docs.