[logaling-commit] logaling/logaling-server [master] Add authentication with omniauth

Zurück zum Archiv-Index

SHIMADA Koji null+****@clear*****
Thu Aug 23 12:11:04 JST 2012


SHIMADA Koji	2012-08-23 12:11:04 +0900 (Thu, 23 Aug 2012)

  New Revision: 7cb99ddb1764d9f6e14fd5dd81ff91822f75f283
  https://github.com/logaling/logaling-server/commit/7cb99ddb1764d9f6e14fd5dd81ff91822f75f283

  Merged 111235b: Merge branch 'add-user-authentication' into merge-add-user-authentication

  Log:
    Add authentication with omniauth
    
    enable only developer provider

  Added files:
    app/controllers/sessions_controller.rb
    app/models/user.rb
    config/initializers/omniauth.rb
    db/migrate/20120823022542_create_users.rb
  Modified files:
    app/controllers/application_controller.rb
    app/views/layouts/application.html.haml
    config/routes.rb
    db/schema.rb

  Modified: app/controllers/application_controller.rb (+10 -0)
===================================================================
--- app/controllers/application_controller.rb    2012-08-23 10:45:57 +0900 (e8065d9)
+++ app/controllers/application_controller.rb    2012-08-23 12:11:04 +0900 (3fd079b)
@@ -1,3 +1,13 @@
 class ApplicationController < ActionController::Base
   protect_from_forgery
+  helper_method :current_user, :signed_in?
+
+  private
+  def current_user
+    @current_user ||= User.find(session[:user_id]) if session[:user_id]
+  end
+
+  def signed_in?
+    !!current_user
+  end
 end

  Added: app/controllers/sessions_controller.rb (+13 -0) 100644
===================================================================
--- /dev/null
+++ app/controllers/sessions_controller.rb    2012-08-23 12:11:04 +0900 (84ebb35)
@@ -0,0 +1,13 @@
+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_url, :notice => "Signed out!"
+  end
+end

  Added: app/models/user.rb (+13 -0) 100644
===================================================================
--- /dev/null
+++ app/models/user.rb    2012-08-23 12:11:04 +0900 (f7ff2d1)
@@ -0,0 +1,13 @@
+class User < ActiveRecord::Base
+  class << self
+    def create_with_omniauth!(auth)
+      create! do |user|
+        user.provider = auth[:provider]
+        user.uid      = auth[:uid]
+        user.name     = auth[:info][:name]
+      end
+    end
+  end
+
+  attr_accessible :name, :provider, :uid
+end

  Modified: app/views/layouts/application.html.haml (+8 -0)
===================================================================
--- app/views/layouts/application.html.haml    2012-08-23 10:45:57 +0900 (93a66f7)
+++ app/views/layouts/application.html.haml    2012-08-23 12:11:04 +0900 (234e5f1)
@@ -27,6 +27,14 @@
           = form_tag search_path, :method => :get, :class => 'navbar-search pull-left' do
             = text_field_tag :query, @query, :class => 'search-query', :placeholder => 'Search'
             = submit_tag 'Search', style: 'display: none'
+          %ul.nav.pull-right
+            - if signed_in?
+              %li= link_to current_user.name, "#"
+              %li.divider-vertical
+              %li= link_to "Sign out", signout_path
+            - else
+              - if Rails.env.development?
+                %li= link_to "Sign in", "/auth/developer"
 
     .container
 

  Added: config/initializers/omniauth.rb (+3 -0) 100644
===================================================================
--- /dev/null
+++ config/initializers/omniauth.rb    2012-08-23 12:11:04 +0900 (2982fd0)
@@ -0,0 +1,3 @@
+Rails.application.config.middleware.use OmniAuth::Builder do
+  provider :developer unless Rails.env.production?
+end

  Modified: config/routes.rb (+4 -0)
===================================================================
--- config/routes.rb    2012-08-23 10:45:57 +0900 (b340802)
+++ config/routes.rb    2012-08-23 12:11:04 +0900 (fbcdd3e)
@@ -12,6 +12,10 @@ LogalingServer::Application.routes.draw do
               :only => :show
   end
 
+  match '/auth/:provider/callback', to: 'sessions#create'
+  match '/auth/failure', to: 'sessions#failure'
+  match "/signout" => "sessions#destroy", :as => :signout
+
   # The priority is based upon order of creation:
   # first created -> highest priority.
 

  Added: db/migrate/20120823022542_create_users.rb (+11 -0) 100644
===================================================================
--- /dev/null
+++ db/migrate/20120823022542_create_users.rb    2012-08-23 12:11:04 +0900 (cb4a56e)
@@ -0,0 +1,11 @@
+class CreateUsers < ActiveRecord::Migration
+  def change
+    create_table :users do |t|
+      t.string :provider
+      t.string :uid
+      t.string :name
+
+      t.timestamps
+    end
+  end
+end

  Modified: db/schema.rb (+9 -1)
===================================================================
--- db/schema.rb    2012-08-23 10:45:57 +0900 (ed7b24e)
+++ db/schema.rb    2012-08-23 12:11:04 +0900 (ff3ce6b)
@@ -11,7 +11,7 @@
 #
 # It's strongly recommended to check this file into your version control system.
 
-ActiveRecord::Schema.define(:version => 20120220070940) do
+ActiveRecord::Schema.define(:version => 20120823022542) do
 
   create_table "github_projects", :force => true do |t|
     t.string   "owner"
@@ -20,4 +20,12 @@ ActiveRecord::Schema.define(:version => 20120220070940) do
     t.datetime "updated_at", :null => false
   end
 
+  create_table "users", :force => true do |t|
+    t.string   "provider"
+    t.string   "uid"
+    t.string   "name"
+    t.datetime "created_at", :null => false
+    t.datetime "updated_at", :null => false
+  end
+
 end
-------------- next part --------------
An HTML attachment was scrubbed...
Download 



More information about the logaling-commit mailing list
Zurück zum Archiv-Index