Hello World from OSS Silicon Valley


HowToUse/RubyOnRails/4.2/Omniauth/1.2


#contents

*Prerequisite [#o1198db9]
-Ubuntu Server installation (You can refer [[HowToUse/UbuntuServer/14.04]])
-Ruby On Rails installation (You can refer [[HowToUse/RubyOnRails/4.1]])

*Install&Setup [#sd9ea3c6]
**Obtain Client ID and Client Secret from GitHub [#c868a7c1]
:Step.1|
Access GitHub Developper page, and click "Register new application".
[https://github.com/settings/developers]

#ref(GetClientID_fig1.png,,500x266,)

:Step.2|
Input information on the new application, and click "Register Application" button.
In this sample case, you can set "callback URL" as below.

http://<host name>/auth/github/callback

#ref(GetClientID_fig2.png,,500x266,)

:Step.3|
Then you will get "Client ID", and "Client Secret" code for GitHub API.

#ref(GetClientID_fig3.png,,500x266,)


**Setup Client ID and Client Secret to your Rails project [#h72c99b9]
:Step.1|
Edit Gemfile

 $ vi Gemfile

 gem 'omniauth'
 gem 'omniauth-github'

(You can see sample from [[here:https://github.com/nasebanal/nb-socialcoding/blob/master/Gemfile]].)

:Step.2|
Install gem files

 $ bundle install

:Step.3|
Setup "Client ID" and "Client Secret" to environment variable.

 $ vi ~/.bashrc

 export GITHUB_KEY="<Client ID>"
 export GITHUB_SECRET="<Client Secret>"

*HowToUse [#d87e7fe8]
:Step.1|
Add omniauth.rb

 $ vi config/initializers/omniauth.rb

 Rails.application.config.middleware.use OmniAuth::Builder do
  provider :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET']
 end

(You can see sample from [[here:https://github.com/nasebanal/nb-socialcoding/blob/master/config/initializers/omniauth.rb]].)

:Step.2|
Add User model.

 $ rails g model user provider:string uid:string screen_name:string name:string
 $ rake db:migrate

:Step.3|
Add method for User model.

 class User < ActiveRecord::Base
   def self.create_with_omniauth(auth)
     create! do |user|
       user.provider = auth['provider']
       user.uid = auth['uid']
       user.screen_name = auth['info']['nickname']
       user.name = auth['info']['name']
     end
   end
 end

(You can see sample from [[here:https://github.com/nasebanal/nb-socialcoding/blob/master/app/models/user.rb]].)

:Step.4|
Create necessary controllers.

 $ rails g controller welcome index
 $ rails g controller sessions

:Step.5|
Edit routes.

 $ vi config/routes.rb

 get 'welcome/index'
 ...
 root 'welcome#index'
 get '/auth/:provider/callback', :to => 'sessions#callback'
 post '/auth/:provider/callback', :to => 'sessions#callback'
 get '/auth/:provider/callback', :to => 'sessions#create'
 post '/auth/:provider/callback', :to => 'sessions#create'
 get '/logout' => 'sessions#destroy', :as => :logout

(You can see sample from [[here:https://github.com/nasebanal/nb-socialcoding/blob/master/config/routes.rb]].)
(You can see sample from [[here:https://github.com/nasebanal/nb-platform4biz/blob/master/config/routes.rb]].)

You can check the configuration result from the following command.

 $ rake routes


:Step.6|
Edit Application controller.

 $ vi app/controller/application_controller.rb

 class ApplicationController < ActionController::Base
  protect_from_forgery
 
   def login_required
     if session[:user_id]
       @current_user = User.find(session[:user_id])
     else
       redirect_to root_path
     end 
   end
 
   helper_method :current_user
 
   private
   def current_user
     @current_user ||= User.find(session[:user_id]) if session[:user_id]
   end
 end

(You can see sample from [[here:https://github.com/nasebanal/nb-socialcoding/blob/master/app/controllers/base_controller.rb]].)
(You can see sample from [[here:https://github.com/nasebanal/nb-platform4biz/blob/master/app/controllers/application_controller.rb]].)

:Step.7|
Edit Session controller.

 $ vi app/controllers/session_controller.rb

 class SessionsController < ApplicationController
   def callback
   def create
     auth = request.env['omniauth.auth']
     user =  User.find_by_provider_and_uid(auth['provider'], auth['uid']) || User.create_with_omniauth(auth)
     session[:user_id] = user.id
     redirect_to root_path
   end
 
   def destroy
     session[:user_id] = nil
     redirect_to root_path
   end
 end

(You can see sample from [[here:https://github.com/nasebanal/nb-socialcoding/blob/master/app/controllers/session_controller.rb]].)
(You can see sample from [[here:https://github.com/nasebanal/nb-platform4biz/blob/master/app/controllers/session_controller.rb]].)


:Step.8|
Edit controller file which should have authentication.

 class XXXController < BaseController
  before_action :login_required

(You can see sample from [[here:https://github.com/nasebanal/nb-platform4biz/blob/master/app/controllers/events_controller.rb]].)

:Step.9|
Edit view file.

 $ vi app/view/layout/application.rb

 <% if current_user %>
   <%= current_user.name %> <%= link_to 'Logout', logout_path %>
 <% else %>
   <%= link_to 'Login', '/auth/github' %>
 <% end %>

(You can see sample from [[here:https://github.com/nasebanal/nb-platform4biz/blob/master/app/views/layout/application.html.rb]].)


*Author [#s17e9cc9]
S.Yatsuzuka