Skip to content

Commit

Permalink
integrating omniauth-coursera into our devise setup
Browse files Browse the repository at this point in the history
  • Loading branch information
kisonecat committed Dec 23, 2012
1 parent a889995 commit df8e0bd
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 18 deletions.
3 changes: 1 addition & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ gem 'less-rails'
# use Devise for login
gem 'devise'
gem 'omniauth'
gem 'oauth2'
gem 'omniauth-google-oauth2'
gem 'omniauth-oauth'

group :development do
gem 'capistrano'
Expand Down
33 changes: 22 additions & 11 deletions app/controllers/users/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def google_oauth2
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.find_for_google_oauth2(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
def coursera
# FIXME: this needs to be worked out

# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.find_for_coursera(request.env["omniauth.auth"], current_user)

if @user.persisted?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Coursera"
sign_in_and_redirect @user, :event => :authentication
else
session["devise.coursera_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
protected
def handle_unverified_request
true
end
end
3 changes: 2 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def apply_omniauth(auth)
authentications.build(:provider => auth['provider'], :uid => auth['uid'], :token => auth['credentials']['token'])
end

def self.find_for_google_oauth2(access_token, signed_in_resource=nil)
def self.find_for_coursera(access_token, signed_in_resource=nil)
# FIXME this needs to grab the realname from the raw_info in omniauth-coursera
data = access_token.info
user = User.where(:email => data["email"]).first

Expand Down
5 changes: 3 additions & 2 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,9 @@
# ==> OmniAuth
# Add a new OmniAuth provider. Check the wiki for more information on setting
# up on your models and hooks.
require "omniauth-google-oauth2"
config.omniauth :google_oauth2, '274300643461.apps.googleusercontent.com', 'qb9d9IhDExhM6Hu3eV7wZsUk', { :access_type => "offline", :approval_prompt => "" }

require "omniauth-coursera"
config.omniauth :coursera, 'db127b155312155233e6e5da331adfed38f96e4f', 'f4e61cf7794c5c703b8aa714d6cc4acca42c76ee'

# ==> Warden configuration
# If you want to use other strategies, that are not supported by Devise, or
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Rails.application.config.middleware.use OmniAuth::Builder do
provider :developer unless Rails.env.production?
# provider :developer unless Rails.env.production?
end
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@
root :to => 'home#index'

# After user authenticates with provider, user needs to go to omniauth call
match '/auth/:provider/callback' => 'authentications#create'
# match '/auth/:provider/callback' => 'authentications#create'

devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

match '/api/v1/user/exercises/:exercise/problems/:problem/attempt' => 'score#attempt',
:constraints => { :problem => /\d+/ }, :via => :post
Expand Down

0 comments on commit df8e0bd

Please sign in to comment.