Skip to content

Commit

Permalink
Permitting Google logins
Browse files Browse the repository at this point in the history
  • Loading branch information
kisonecat committed Jun 28, 2013
1 parent 1d82cc3 commit c5a3a84
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ gem 'less-rails'
gem 'devise'
gem 'omniauth'
gem 'omniauth-oauth'
# gem 'omniauth-google-oauth2'
gem 'omniauth-openid'

# use Nokogiri to parse the exercise files
gem 'nokogiri'
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/profile_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def update
return
end
@user = current_user
@user.email = params[:email]
# @user.email = params[:email]
@user.location = params[:location]
@user.gender = params[:gender]
year, month, day = params[:year].to_i, params[:month].to_i, params[:day].to_i
Expand Down
12 changes: 12 additions & 0 deletions app/controllers/users/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ def coursera
end
end

def google
@user = User.find_for_open_id(request.env["omniauth.auth"], current_user)

if @user.persisted?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google"
sign_in_and_redirect @user, :event => :authentication
else
session["devise.google_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end

# This is necessary since Rails 3.0.4
# See https://github.com/intridea/omniauth/issues/185
# and http://www.arailsdemo.com/posts/44
Expand Down
15 changes: 15 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ def self.find_for_coursera(access_token, signed_in_resource=nil)
user
end

def self.find_for_open_id(access_token, signed_in_resource=nil)
data = access_token.info
File.open("dump.txt",'w') do |f|
f.puts data
end

if user = User.where(:email => data["email"]).first
user
else
User.create!(:name => data["name"],
:email => data["email"],
:password => Devise.friendly_token[0,20])
end
end

#################################################################
# some methods for computing simple statistics for badges
def total_hints_requested
Expand Down
3 changes: 2 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
</ul>
</li>
<% else %>
<li class=""><%= link_to raw("<i class='icon-signin'></i> Login"), user_omniauth_authorize_path(:coursera) %></li>
<li class=""><%= link_to raw("<i class='icon-signin'></i> Login via Coursera"), user_omniauth_authorize_path(:coursera) %></li>
<li class=""><%= link_to raw("<i class='icon-signin'></i> Login via Google"), user_omniauth_authorize_path(:google) %></li>
<% end %>
</ul>
</div><!--/.nav-collapse -->
Expand Down
2 changes: 2 additions & 0 deletions app/views/profile/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
<fieldset>
<legend>Edit Profile</legend>

<!-- cannot edit email
<%= label_tag :email %>
<%= text_field_tag :email, @user.email %>
-->

<%= label_tag :location %>
<%= text_field_tag :location, @user.location %>
Expand Down
6 changes: 6 additions & 0 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@

require "omniauth-coursera"
config.omniauth :coursera, 'db127b155312155233e6e5da331adfed38f96e4f', 'f4e61cf7794c5c703b8aa714d6cc4acca42c76ee'
config.omniauth :open_id, :name => 'google', :identifier => 'https://www.google.com/accounts/o8/id'

# require "omniauth-google-oauth2"
# config.omniauth :google_oauth2, "477602199932.apps.googleusercontent.com", "7D754R-1H2TL0IQ37Qw4hPvO"

#, { access_type: "offline", approval_prompt: "" }

# ==> Warden configuration
# If you want to use other strategies, that are not supported by Devise, or
Expand Down
5 changes: 5 additions & 0 deletions config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Rails.application.config.middleware.use OmniAuth::Builder do
# provider :developer unless Rails.env.production?
# provider :google_oauth2, ENV["GOOGLE_KEY"], ENV["GOOGLE_SECRET"]
# {
# :scope => "userinfo.email,userinfo.profile,analytics.readonly",
# :approval_prompt => "auto"
# }
end
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Assessor::Application.routes.draw do

# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
root :to => 'home#index'
Expand Down Expand Up @@ -27,6 +28,8 @@
resources :handouts
resources :videos

get "lectures/" => "lectures#index"

# The priority is based upon order of creation:
# first created -> highest priority.

Expand Down

0 comments on commit c5a3a84

Please sign in to comment.