Github Signup

Follow this tutorial to signup your users with Github. You will be able to gather their profile picture, name and email.

Tutorial for adding github authentiaciton to Lightning Rails

Install Github authentication Gem

The first step will be to install the omniauth Github gem. In our Gemfile, let's add

As usual, we will bundle

As we wish to have our users sign up with Github, we must let devise know. Let's go into the initializers/devise.rb file and add this line:

Create a Github OAuth App

Now that we have done some config, we can see that we are missing the API keys that will connect our app to GitHub. Unlike Google OAuth, Github is very easy to create.

You will see that you have a CLIENT ID but still need to generate a new secret key, so let's do it by clicking "Generate a new client secret".

Add keys to .env

Copy the Secret key and add it to your .env file along with the client ID:

Routes

Now that we have a good config, we'll need to activate new routes, and replace the existing devise_for :user by this line:

Controller

Let's create that new controller we added to the routes, in the terminal run:

And

Now in our brand new controller file, we will add the GitHub OmniAuth logic:

Model

After adding the proper routes and controller action we will need to activate the Omniauth option from devise. In user.rb uncomment and add the following symbols to the devise options:

We will also add the Omniauthable custom method:

Views

Let's add the GitHub signup to the sessions/new file. I like to add it to the login instead of the signup as to not lose the before_action :authenticate_user! auto-redirect function. As our code either updates or creates. You can now ignore the signup page and unlink it from all the buttons on your page.

In Production

To have this code work on production as well as on development, you will need to create a second Github app and follow these steps again:

Now you should have two GitHub apps. One for development and one for production.

The only step left will be to add the production ID and key to Heroku from your terminal: (replace the API key by your actual production API key and ID)

And

Et voila! You have a brand-new GitHub authentication setup.

Your techy users will thank you! ๐Ÿ™


Common Errors ๐Ÿž:

  • Omniauthable is undefined: Did you restart your server after installing the Omniauthable Gem? If not do it an it should get fixed.

  • Must supply API_KEY: Are you adding avatars without a Cloudinary key in your .env file? That's probably the cause, head to Cloudinary, and add your API key in the .env file.

Last updated

Was this helpful?