● videoAuthentication
Integrating Devise and Spina

Unlock Revisiting Ruby on Rails
Subscribe for full access to every course, or buy this one on its own.
SECTION
Authentication
NEXT UP
Flexing Make
COURSE
Rails Revisited
33 lessons
About this lesson
Integrating with Devise means creating our own Spina::Auth module, which is pretty straightforward:
<span class="hljs-comment"># frozen_string_literal: true</span>
<span class="hljs-comment"># Used as Spina's authentication module</span>
<span class="hljs-keyword">module</span> <span class="hljs-title class_">BigAuth</span>
<span class="hljs-keyword">extend</span> <span class="hljs-title class_">ActiveSupport::Concern</span>
included <span class="hljs-keyword">do</span>
helper_method <span class="hljs-symbol">:current_spina_user</span>
helper_method <span class="hljs-symbol">:logged_in?</span>
helper_method <span class="hljs-symbol">:logout_path</span>
<span class="hljs-keyword">end</span>
<span class="hljs-comment"># Spina user falls back to devise user session in the case there is one and it is of a superadmin.</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">current_spina_user</span>
<span class="hljs-title class_">Spina</span><span class="hljs-symbol">:</span><span class="hljs-symbol">:Current</span>.user |<span class="hljs-params"></span>|= current_user <span class="hljs-keyword">if</span> current_user.is_admin?
<span class="hljs-keyword">end</span>
<span class="hljs-comment"># Returns falsy unless there is a logged in superadmin</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">logged_in?</span>
<span class="hljs-keyword">return</span> current_spina_user <span class="hljs-keyword">if</span> user_signed_in?
<span class="hljs-literal">false</span>
<span class="hljs-keyword">end</span>
<span class="hljs-comment"># Not used</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">logout_path</span>
spina.admin_logout_path
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">private</span>
<span class="hljs-comment"># Redirects user to sign in if not logged in as a superadmin</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">authenticate</span>
redirect_to <span class="hljs-string">"/login"</span> <span class="hljs-keyword">unless</span> logged_in?
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
<span class="hljs-title class_">We</span> <span class="hljs-keyword">then</span> need to update our <span class="hljs-title class_">User</span> model so we know <span class="hljs-keyword">if</span> the current user is an <span class="hljs-symbol">admin:</span>
<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> <span class="hljs-comment">#add your logic here</span>
<span class="hljs-keyword">end</span>
<span class="hljs-title class_">Finally</span>, we update the configuration <span class="hljs-keyword">in</span> the the <span class="hljs-title class_">Spina</span> <span class="hljs-symbol">initializer:</span>
config.authentication = <span class="hljs-string">"BigAuth"</span>
Unlock Revisiting Ruby on Rails
Subscribe for full access to every course, or buy this one on its own.