My stack is:
- Ionic 2
- Java Spring
- JWT authentication
and I want to implement a Social login button (Facebook, Google etc.) inside my app with the respective cordova plugins, that logs in the user and validates him on my existing custom server-side API and store his/her data. I wasn’t able to find any good tutorial on how to do this.
I want my user to be persisted in the database with a random password and be able to login from my app.
I was imagining something along the lines of:
(client-side)
FB.login(function(userDetailsAndToken) {
myBackendAPI.socialLogin(userDetailsAndToken).then(function(user) {
//Successfully logged in with Facebook!
this.user = user;
}
}
and in the backend (Java Spring):
@PostMapping("/social/account")
public ResponseEntity socialAccount(@Valid @RequestBody FacebookDTO facebookDTO) {
validateSocialUser(facebookDTO);
//if user exists return invalid etc.
Optional<User> existingUser = userRepository.findOneByEmail(facebookDTO.getEmail());
if (existingUser.isPresent()) {
return ResponseEntity.badRequest();
}
//User doesn't exist. Proceed with login
//map from social to my User entity
User user = socialMapper.toEntity(facebookDTO);
userService
.createUser(user.getLogin(), user.getPassword(),
user.getFirstName(), user.getLastName(),
user.getEmail().toLowerCase(), user.getImageUrl(),
user.getLangKey());
return new ResponseEntity<>(HttpStatus.CREATED);
}
Is this possible and secure? Any good resources/libraries or guidelines on how to achieve that?
3
Answers
Here is a sample demo about working with FB and Google Auth , but i’m not from a Java background so you will be finding only client side solution.
service
let’s implement the logic for the login functionality. Create an
oauth.service.ts
file in theoauth
folder and paste the following code in there:Authentication provider and token interfaces
As we have already mentioned, the
IOathProvider
should include a login() function. Therefore, we should set the following interface that will act as an abstract type/model for theIOathProvider
object. Create anoauth.provider.interface.ts
file in theoauth
folder and include the following lines in it:Facebook and Google authentication services
As a next step, we should implement the services for each one of the authentication providers our app has, i.e.
FacebookOauthProvider
andGoogleOauthProvider
.Install dependencies
Here is when the
ng2-cordova-oauth
library comes handy. We can install it by executing the command:npm install ng2-cordova-oauth --save
Also, our app relies on the
Cordova InAppBrowser plugin
. We are going to install it with:ionic plugin add cordova-plugin-inappbrowser
Do not forget to include
cordova-plugin-inappbrowser
in your package.json file too, so it can be installed with the rest of the plugins anytime you install your project from scratch.Implement the Facebook and Google authentication providers
Let’s create the
facebook-oauth.provider.ts
file under theoauth/facebook/ path
. In this file, include the code in the snippet:Similarly, using the
CordovaOauth
object available by theng2-cordova-oauth library
, we will implement theGoogle authentication provider
with its ownlogin()
function. However, here we pass anotherclientId
from Config that corresponds to the application we configured withGoogle
using theGoogle Developer Console
.Therefore, create a
google-oauth.provider.ts
file and paste the following lines:Full credits to this Article and you can find the Working Code in Github. I haven’t covered the whole Tutorial , only the parts (Google and Facebook) of that tutorial .i.e. What plugin we need to install and how to use using the TypeScript , if you need beyond that then you can refer to that tutorial
To secure data in Database you have to use any of the ‘hashing’ algorithms. I recommend to use HMAC SHA 256 algorithm. Hashing can’t able to do decrypt the data. To do the hashing you need the key, you can make the key as either static or dynamic based on your need. Store the encrypted key in Database by using any of the RSA encryption algorithms and while hashing you can decrypt the key and pass in hashing method.
To do Hashing use apache HmacUtils.
To do encryption and decryption Please use below tutorial RSA Encryption and Decryption
Examples from one of my projects. Java Client for JWT Authentication:
Java backend for social buttons including (Google and Facebook):
Example for JWT only
JHipsterProperties is just layer before simple Properties configuration.
This is generated by JHipster. You could generate monolithic application and see examples how backend and frontend should work to enable social buttons.