Hello World from OSS Silicon Valley


HowToUse/RubyOnRails/4.2/Omniauth/1.2

_ Prerequisite

_ Install&Setup

_ Obtain Client ID and Client Secret from GitHub

Step.1
Access GitHub Developper page, and click "Register new application". [https://github.com/settings/developers]
GetClientID_fig1.png
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

GetClientID_fig2.png
Step.3
Then you will get "Client ID", and "Client Secret" code for GitHub API.
GetClientID_fig3.png

_ Setup Client ID and Client Secret to your Rails project

Step.1
Edit Gemfile
$ vi Gemfile
gem 'omniauth'
gem 'omniauth-github'

(You can see sample from here.)

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

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.)

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.)

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#create'
post '/auth/:provider/callback', :to => 'sessions#create'
get '/logout' => 'sessions#destroy', :as => :logout

(You can see sample from here.)

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.)

Step.7
Edit Session controller.
$ vi app/controllers/session_controller.rb
class SessionsController < ApplicationController
  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.)

Step.8
Edit controller file which should have authentication.
class XXXController < BaseController
 before_action :login_required

(You can see sample from here.)

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.)

_ Author

S.Yatsuzuka

 
Attach file: fileGetClientID_fig3.png 164 download [Information] fileGetClientID_fig2.png 166 download [Information] fileGetClientID_fig1.png 163 download [Information]
Last-modified: 2016-06-25 (Sat) 23:26:37 (2853d)