Skip to content

Instantly share code, notes, and snippets.

@Haumer
Created August 21, 2020 16:59
Show Gist options
  • Select an option

  • Save Haumer/74128810ff8721c14c7452440f777346 to your computer and use it in GitHub Desktop.

Select an option

Save Haumer/74128810ff8721c14c7452440f777346 to your computer and use it in GitHub Desktop.

OmniAuth in Rails

Rails Template:

rails new \
  --database postgresql \
  --webpack \
  -m https://raw.githubusercontent.com/lewagon/rails-templates/master/devise.rb \
  github-omniauth

In the Gemfile:

gem 'omniauth-github', github: 'omniauth/omniauth-github', branch: 'master'

In .env:

APP_ID=YOUR_APP_ID
APP_SECRET=YOUR_APP_SECRET
# app/config/routes
Rails.application.routes.draw do
devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
# [...]
end
# app/models/user.b
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:omniauthable, :omniauth_providers => [:github]
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0, 20]
# user.githubname = auth.info.nickname.downcase # assuming the user model has agithubnickname
# user.avatar_url = auth.info.image # assuming the user model has an image
user.save
end
end
end
# config/initializers/devise.rb
Devise.setup do |config|
# [...]
# ==> OmniAuth
# Add a new OmniAuth provider. Check the wiki for more information on setting
# up on your models and hooks.
config.omniauth :github, ENV['APP_ID'], ENV['APP_SECRET'], scope: 'user,public_repo'
# [...]
# When using OmniAuth, Devise cannot automatically set OmniAuth path,
# so you need to do it manually. For the users scope, it would be:
config.omniauth_path_prefix = '/my_engine/users/auth'
# [...]
end
# app/controllers/users/omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def github
@user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted?
sign_in_and_redirect @user, event: :authentication
set_flash_message(:notice, :success, kind: "Github") if is_navigational_format?
else
session["devise.github_data"] = request.env["omniauth.auth"].except(:extra)
redirect_to new_user_registration_url
end
end
def failure
redirect_to root_path
end
end
<!-- app/views/home.html.erb -->
<%= link_to user_github_omniauth_authorize_path, class: "github-link home-login-cta" do %>
<button class="github-login">
<i class="fab fa-github github-social"></i>
Sign in with GitHub
</button>
<% end %>
<style>
.github-login {
display:block;
width:100%;
max-width:680px;
margin:20px auto;
height:50px;
cursor:pointer;
font-size:14px;
font-family: 'Montserrat', sans-serif;
border-radius:8px;
border:none;
line-height:40px;
background:#25282d;
color:white;
box-shadow:0 0 0 rgba(#25282d,.0);
transition:.2s linear;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment