● videoAuthentication
Adding Social Login With OmniAuth

Unlock Revisiting Ruby on Rails
Subscribe for full access to every course, or buy this one on its own.
SECTION
Authentication
NEXT UP
Integrating Devise and Spina
COURSE
Rails Revisited
33 lessons
About this lesson
Bumbled around a bit with this video because I didn't RTFM as I should have. It's critical to read through everything, as there are quite a few moving pieces you need to set up.
The Routes
The first thing to do is to let Devise know about our omniauth controller. In routes.rb, add this:
devise_for <span class="hljs-symbol">:users</span>,
<span class="hljs-symbol">controllers:</span> {
<span class="hljs-symbol">sessions:</span> <span class="hljs-string">"users/passwordless"</span>,
<span class="hljs-symbol">omniauth_callbacks:</span> <span class="hljs-string">'users/omniauth_callbacks'</span>
}
The Controller
The controller itself is reasonably straightforward, but I redid the forwarding paths:
<span class="hljs-keyword">class</span> <span class="hljs-title class_">Users::OmniauthCallbacksController</span> < <span class="hljs-title class_ inherited__">Devise::OmniauthCallbacksController</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">github</span>
<span class="hljs-comment"># You need to implement the method below in your model (e.g. app/models/user.rb)</span>
<span class="hljs-variable">@user</span> = <span class="hljs-title class_">User</span>.from_omniauth(request.env[<span class="hljs-string">"omniauth.auth"</span>])
<span class="hljs-keyword">if</span> <span class="hljs-variable">@user</span>.persisted?
sign_in_and_redirect <span class="hljs-variable">@user</span>, <span class="hljs-symbol">event:</span> <span class="hljs-symbol">:authentication</span> <span class="hljs-comment"># this will throw if <span class="hljs-doctag">@user</span> is not activated</span>
set_flash_message(<span class="hljs-symbol">:notice</span>, <span class="hljs-symbol">:success</span>, <span class="hljs-symbol">kind:</span> <span class="hljs-string">"GitHub"</span>) <span class="hljs-keyword">if</span> is_navigational_format?
<span class="hljs-keyword">else</span>
session[<span class="hljs-string">"devise.github_data"</span>] = request.env[<span class="hljs-string">"omniauth.auth"</span>].except(<span class="hljs-symbol">:extra</span>) <span class="hljs-comment"># Removing extra as it can overflow some session stores</span>
redirect_to new_user_session_url
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">failure</span>
redirect_to root_path
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
The Model
The User model needs a way to find the user, and it's imperative you don't double up on emails!
<span class="hljs-keyword">class</span> <span class="hljs-title class_">User</span> < <span class="hljs-title class_ inherited__">ApplicationRecord</span>
<span class="hljs-comment"># Include default devise modules. Others available are:</span>
<span class="hljs-comment"># :confirmable, :lockable, :timeoutable, :trackable and :omniauthable</span>
devise <span class="hljs-symbol">:magic_link_authenticatable</span>, <span class="hljs-symbol">:validatable</span>, <span class="hljs-symbol">:rememberable</span>,
<span class="hljs-symbol">:omniauthable</span>, <span class="hljs-symbol">omniauth_providers:</span> %i[github]
<span class="hljs-keyword">def</span> <span class="hljs-title function_">is_admin?</span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">true</span> <span class="hljs-keyword">if</span> email ==<span class="hljs-string">"robconery@gmail.com"</span>
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">self</span>.from_omniauth(auth)
user = <span class="hljs-title class_">User</span>.find_by(<span class="hljs-symbol">email:</span> auth.info.email)
<span class="hljs-keyword">unless</span> user
user = <span class="hljs-title class_">User</span>.create!(
<span class="hljs-symbol">email:</span> auth.info.email,
<span class="hljs-symbol">provider:</span> auth.provider,
<span class="hljs-symbol">uid:</span> auth.uid,
<span class="hljs-symbol">name:</span> auth.info.name
)
<span class="hljs-keyword">end</span>
user
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
Unlock Revisiting Ruby on Rails
Subscribe for full access to every course, or buy this one on its own.