Skip to content

Commit

Permalink
update for half done the authenticate not the devise with github
Browse files Browse the repository at this point in the history
  • Loading branch information
hlee committed Jun 23, 2013
1 parent 4cbcc92 commit 8cbb900
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 2 deletions.
10 changes: 10 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
class ApplicationController < ActionController::Base
protect_from_forgery
def after_sign_in_path_for(resource)
current_user_path
end
#def after_sign_out_path_for(resource)
#root_path
#end
#
def after_sign_out_path_for(resource_or_scope)
request.referrer
end
end
14 changes: 14 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class SessionsController < ApplicationController
def create_github
#raise env['omniauth.auth'].to_yaml
user = User.from_omniauth(auth_hash)
session[:user_id] = user.id
redirect_to root_url, notice: "Signed in."
end

protected

def auth_hash
request.env['omniauth.auth']
end
end
16 changes: 15 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,21 @@ class User < ActiveRecord::Base
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
:recoverable, :rememberable, :trackable, :validatable
# attr_accessible :title, :body
attr_accessible :email, :password

def self.from_omniauth(auth)
where(auth.slice("provider", "uid")).first || create_from_omniauth(auth)
end

def self.create_from_omniauth(auth)
user = User.new
user.provider = auth["provider"]
user.uid = auth["uid"]
user.name = auth["info"]["nickname"]
user.email = auth['info']['email']
user.save(validate: false)
user
end
end
10 changes: 10 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
<%= csrf_meta_tags %>
</head>
<body>
<div id="user_header">
<% if current_user %>
Logged in as <%= current_user.email %>.
<%= link_to "Log Out", destroy_user_session_path , :method => :delete%>
<% else %>
<%= link_to "Sign in with github", '/auth/github'%>
<%= link_to "Sign Up", new_user_registration_path %> or
<%= link_to "Log In", new_user_session_path %>
<% end %>
</div>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= yield %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

devise_for :users
match 'auth/failure', to: redirect('/')
match 'auth/:provider/callback', to: 'sessions#create_github'

root to: 'home#index'
# The priority is based upon order of creation:
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20130623183500_add_provider_info_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddProviderInfoToUser < ActiveRecord::Migration
def change
add_column :users, :provider, :string
add_column :users, :uid, :string
add_column :users, :name, :string
end
end
5 changes: 4 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20130623030113) do
ActiveRecord::Schema.define(:version => 20130623183500) do

create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
Expand All @@ -26,6 +26,9 @@
t.string "last_sign_in_ip"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "provider"
t.string "uid"
t.string "name"
end

add_index "users", ["email"], :name => "index_users_on_email", :unique => true
Expand Down

0 comments on commit 8cbb900

Please sign in to comment.