Google Auth, SignUp, SignIn and LogOut with Ruby on Rails

Stela Capsa
4 min readJun 22, 2021
$ rails new [name]

rails new [name] - creates a new project

$ cd [name]

cd [name] -change directory to in to the project

$ code .

code . -opening up the code editor

$ rails g User resource username email password_digest uid provider

New created table you can find it in db folder, rails g resource User username email password_digest uid provider -this command will generate our User table

$ rake db:migrate

When running rake db:migrate we have to see the following in your terminal

In Gemfile add next gems to authenticate our user with google sign in. Bcrypt is the gem that helps to make the password secure with a regular sign in.

gem 'bcrypt', '~> 3.1.7'gem 'omniauth'gem 'omniauth-google-oauth2'gem 'dotenv-rails'

After saving the changes in Gemfile run:

$ bundle install

Lets create the key and secret for authentication

https://console.developers.google.com -navigate the link

  1. Create new project

2. When we navigate credential button first we have to configure consent screen, then chose between Internal or External user type, and press create.

3. Next page will lead us to the next step

Where is Application name give it a name than press save.

4. Now create credentials

When creating credentials we have to add a callback URL, in my case I would like to do authentication with google and my server runs on localhost:3000

http://localhost:3000/auth/google_oauth2/callback

5. Back to our code editor, create ENV file, to avoid any mistakes and confusions I recommend to type it in the terminal.

$ touch .env

In this file we have to past our KEY and the SECRET , (this is random data).

GOOGLE_CLIENT_ID=45453663727723828e.apps.googleusercontent.com

GOOGLE_CLIENT_SECRET=53egwed2egy23ye23uhr3hk2

IMPORTANT! don’t forget to add .env file to the gitignore.

6. Create next file: >Config >initializers >omniauth.rb

create omniauth.rb and drop this lines of code

Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, ENV[“GOOGLE_CLIENT_ID”],ENV[“GOOGLE_CLIENT_SECRET”], skip_jwt: true
end

7. In file routes.rb add next route

get “/auth/google_oauth2/callback” => ‘sessions#google’

Here we are done with google authentication For sign up sign in and log out a User follow next steps

Sign Up, Sign In, Log Out

config/routes.rb

app/controllers/session_controller.rb

app/controllers/user_controller.rb

app/controllers/application_controller.rb

models/user.rb

Adds methods to set and authenticate against a BCrypt password.

views/layouts/application.html.erb

This folder represents the nav bar of application.

views/sessions/new.html.erb

Sig In form

views/users/show.html.erb

Show form after user Sign In

views/users/new.html.erb

Sign Up form

views/users/show.html.erb

Show page after user Sing Up

This steps should help you to implement a full functionality of a user registration.

Happy Coding!

Stela Capsa

https://github.com/scapsa0116?tab=repositories

--

--