Basic setup for Devise, and OAuth for Ruby on Rails

Se Min Lee
4 min readAug 16, 2020

--

It’s already time for second project at Flatiron school. We will be using Ruby on rails, and I came across a nice gem to work with. In this blog, I will introduce a gem called Devise, and OmniAuth for Google. For full detail, please visit following website.

  • https://github.com/heartcombo/devise — — This is GitHub of devise gem. It explains thoroughly how to install, and usage of the gem. To briefly introduce the gem, it is made up of 10 modules that builds flexible authentication model. One of the module contains OmniAuthable, which enables to use OmniAuth.
  • https://github.com/zquestz/omniauth-google-oauth2 — — This is GitHub for google OmniAuth. There are multiple OmniAuth available, including facebook, GitHub, twitter, etc., but I included google-oauth2 because it is the only OmniAuth I’ve used in my project. But getting OmniAuth to work is similar for other companies, so it will be easy if this OmniAuth is understood.
  • https://github.com/omniauth/omniauth — — This is introduction of the OmniAuth, so if anyone is interested, feel free to visit and read some documentations.

First

open up the Gemfile. In the file, copy the following gems and bundle install.

gem ‘omniauth’ is optional. gem ‘omniauath-google-oauth2’ automatically includes the ‘omniauth’ gem, but sometimes errors can pop, so I included the gem just in case.

If you wish to use Facebook, Twitter, or other available Omniauth, you can search through developers api website and it will be shown, but generally, it is gem ‘omniauth-facebook’ or gem ‘omniauth-github’.

Second

go to the link https://console.developers.google.com/ . Log in with your gmail account, create new project, and next to the credentials, click the create credentials and choose OAuth Client Id.

It will ask you to configure consent screen, and when you follow the instructions, you will be directed to this page:

On the first URI, write the localhost domain, and on the second URI, copy the typed url. When the first URI was failed to reach, the second URI will be attempted. Once you successfully created the credentials, you will be provided with Client ID and Client Secret.

Copy the credential info and paste it either in .env file, or config/credentials.yml.enc file, which is provided by rails.

To use credentials.yml.enc file, type in the following code in your terminal.

The file will open up, and you can store your info inside. After you save and close it, it will automatically be encrypted, and master-key will be provided to you as config/master.key. To check the credential info, type in “rails credentials:show” in your terminal. The credential info will show up in your terminal.

For further instructions on how to use the keys, please visit the GitHub link above, because they have explained the process very clearly and thoroughly.

Third

Set some the devise by running few codes in your terminal:

The user in second line doesn’t have to be user, use whatever class you decides to name. Once you associated the devise with user, your user model and route should look something like this.

devise in User model shows the 10 modules it is made up of.

This blog was just an introduction to devise gem and OmniAuth. There are more codes required for the app to run, but instructions are very clear in GitHub, so I would advise you to check the links.

In the end, using devise gem and OmniAuth-google looks like this. I had tons of fun messing around with devise and OmniAuth, and I hope everyone has some fun!

Happy coding!

--

--