0
0
Ruby on Railsframework~30 mins

OAuth integration basics in Ruby on Rails - Mini Project: Build & Apply

Choose your learning style9 modes available
OAuth integration basics
📖 Scenario: You are building a simple Rails app that allows users to log in using a third-party service like GitHub via OAuth.This helps users sign in without creating a new password for your app.
🎯 Goal: Create the basic setup for OAuth integration in a Rails app using the omniauth-github gem.You will set up the initial data, configure the middleware, implement the callback route, and complete the user session setup.
📋 What You'll Learn
Create a hash with GitHub OAuth credentials
Add OmniAuth middleware configuration
Implement the callback action to handle OAuth response
Complete the session creation to log in the user
💡 Why This Matters
🌍 Real World
OAuth is widely used to let users sign in to apps using existing accounts from services like GitHub, Google, or Facebook. This saves users from creating new passwords and improves security.
💼 Career
Understanding OAuth integration is important for web developers building modern apps that require user authentication and social login features.
Progress0 / 4 steps
1
DATA SETUP: Create GitHub OAuth credentials hash
Create a hash called github_credentials with keys :client_id and :client_secret set to the exact strings 'GITHUB_CLIENT_ID' and 'GITHUB_CLIENT_SECRET' respectively.
Ruby on Rails
Need a hint?

Use Ruby hash syntax with symbol keys :client_id and :client_secret.

2
CONFIGURATION: Add OmniAuth middleware for GitHub
Add the OmniAuth middleware configuration by calling Rails.application.config.middleware.use OmniAuth::Builder and inside the block configure the provider with :github, github_credentials[:client_id], and github_credentials[:client_secret].
Ruby on Rails
Need a hint?

Use the provider method inside the OmniAuth::Builder block.

3
CORE LOGIC: Implement the OAuth callback action
In the SessionsController, define a method github that extracts the auth hash from request.env['omniauth.auth'] and assigns it to an instance variable @auth.
Ruby on Rails
Need a hint?

Use request.env['omniauth.auth'] to get the OAuth data.

4
COMPLETION: Create user session from OAuth data
Inside the github method, add code to set session[:user_id] to @auth['uid'] and then redirect to the root path using redirect_to root_path.
Ruby on Rails
Need a hint?

Use session[:user_id] = @auth['uid'] to store the user ID.

Use redirect_to root_path to go back to the homepage.