In our current project we were asked to implement facebook signup/login functionality using Oauth . We did some research and decided to use the Oauth Plugin and here are the steps we followed :

Step 1 : Install the Oauth plugin

We opened BuildConfig.groovy and added the following dependency inside plugins section :

 Step 2 : Sign up as facebook developer and create a new application

You can sign up for facebook developer account from the this link. After successful sign up all you need to do is to create a new application. You can create a new application from here.

  • Click on “Create New App” button
  • Give a name to your application and after finishing the form you will be taken to a page which shows your App ID and App Secret
  • On this page click on “Website with Facebook Login” and enter the Site URL. Let’s assume your website is jftblogpost.com and if you are running your grails application on your local machine then you have to edit your host file and map 127.0.0.1 to your website URL using the following :
  • You can check

    this link

    to find out how to modify your host file.

  • Till now you are done mapping your website to localhost but the Site URL in facebook app is still not done. If your grails application is running on root context then you need to add the following in Site URL : 


    But if you are app is not running in the root context then you need to add the app name in the Site URL and the value will look like this

  • Save these changes and now let’s get back to your Grails application

 Step 3 : Add Config values in Config.groovy

 

 Step 4 : Add facebook link in your sign up page

This is the easy part. You just need to add a new tag to your sign up page. It is mentioned in the official docs that you need to add the following tag in your GSP :

Now if you try to sign up using facebook you will be taken to facebook but as soon as you return back to your Grails application after authentication you will get an error. Because there is no handler for facebook response. Let’s create a controller which handles the response from facebook.

 Step 5 : Add handler for facebook response

You may have noticed that we entered Site URL in facebook app which contains the following oauth/facebook/callback . In simple words we have to create a controller with name oauth and an action facebook inside the controller. You are free to modify the controller/action name as per your convenience. This is how my controller/action looks like :

 

This is it. Hope it helps you in some way.