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

rails new [name] - creates a new project

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

code . -opening up the code editor

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

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.

After saving the changes in Gemfile run:

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.

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

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

About Dependency Injection

Unity/C# Challenge 44: Jumping Pt 2

Reduce Costs with Scheduled EC2 Instance Shutdowns on AWS

Annotation Tool Part 2: Retrospective on the Build

From a human rights activist to a front-end web developer; A brief story of my web development…

How To Pack Python Program Into exe File

Four Guiding Principles to Maximize Your Organization’s Success on AWS: Principle Two

Embracing a Hybrid Approach for Scaling Serverless DevOps

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Stela Capsa

Stela Capsa

More from Medium

Class and Interface Inheritance in Ruby

How to Create a Rails 6 API with Devise-JWT

Sinatra: Simple Backend Development and You

How to use .includes? in Rails 7