From 049e9b706622e4015c2db072398e82acdded5159 Mon Sep 17 00:00:00 2001 From: Mike Slemmer Date: Mon, 12 Nov 2012 12:12:32 -0800 Subject: [PATCH 1/3] Omniauth routes --- Gemfile | 3 +++ app/controllers/sessions_controller.rb | 14 +++++++++++++- config/initializers/omniauth.rb | 3 +++ config/routes.rb | 3 +++ 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 config/initializers/omniauth.rb diff --git a/Gemfile b/Gemfile index f5a1d3c16..71847c56f 100644 --- a/Gemfile +++ b/Gemfile @@ -25,6 +25,9 @@ gem 'eventmachine' gem 'amqp' gem 'logging-rails', :require => 'logging/rails' gem 'tire' +gem 'rb-readline' +gem 'omniauth' +gem 'omniauth-facebook' group :development, :test do gem 'rspec-rails', '2.11.0' diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 61f5c8078..bf7d1f207 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -4,6 +4,9 @@ class SessionsController < ApplicationController def new end +# oauth information: +# http://net.tutsplus.com/tutorials/ruby/how-to-use-omniauth-to-authenticate-your-users/ + def create user = User.find_by_email(params[:session][:email]) if user && user.authenticate(params[:session][:password]) @@ -15,8 +18,17 @@ class SessionsController < ApplicationController end end + def create_oauth + auth_hash = request.env['omniauth.auth'] + render :text => auth_hash.inspect + end + def destroy sign_out redirect_to root_url end -end \ No newline at end of file + + def failure + + end +end diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb new file mode 100644 index 000000000..00f8f05e0 --- /dev/null +++ b/config/initializers/omniauth.rb @@ -0,0 +1,3 @@ +Rails.application.config.middleware.use OmniAuth::Builder do + provider :facebook, '468555793186398', '546a5b253972f3e2e8b36d9a3dd5a06e' +end diff --git a/config/routes.rb b/config/routes.rb index c861a0482..be8e01d07 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -16,6 +16,9 @@ SampleApp::Application.routes.draw do match '/signup', to: 'users#new' match '/signin', to: 'sessions#new' match '/signout', to: 'sessions#destroy', via: :delete + # oauth + match '/auth/:provider/callback', :to => 'sessions#create_oauth' + match '/auth/failure', :to => 'sessions#failure' match '/help', to: 'static_pages#help' match '/about', to: 'static_pages#about' From 6b520b0a2b8692095c384797d901455e8493307b Mon Sep 17 00:00:00 2001 From: Mike Slemmer Date: Wed, 14 Nov 2012 22:18:37 -0800 Subject: [PATCH 2/3] facebook integration work --- app/controllers/sessions_controller.rb | 26 +++++++++++++++++++++++--- app/controllers/users_controller.rb | 4 +--- app/helpers/sessions_helper.rb | 4 ++++ app/models/.gitkeep | 0 app/views/users/new.html.erb | 8 +++++++- 5 files changed, 35 insertions(+), 7 deletions(-) delete mode 100644 app/models/.gitkeep diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index bf7d1f207..a3e5c2fb5 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -10,8 +10,7 @@ class SessionsController < ApplicationController def create user = User.find_by_email(params[:session][:email]) if user && user.authenticate(params[:session][:password]) - sign_in user - redirect_back_or music_sessions_url + complete_sign_in user else flash.now[:error] = 'Invalid email/password combination' render 'new' @@ -20,7 +19,28 @@ class SessionsController < ApplicationController def create_oauth auth_hash = request.env['omniauth.auth'] - render :text => auth_hash.inspect + authorization = UserAuthorization.find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"]) + if authorization + # Sign in for a user who has already registered. + complete_sign_in authorization.user + else + # Sign up for a completely new user. + # First/last name: auth_hash["info"]["first_name"] and auth_hash["info"]["last_name"] + # token: auth_hash["credentials"]["token"] -- "expires_at" + # + # For debugging - to see what all is there: + # render :text => auth_hash.to_yaml + user = User.new :name => auth_hash["info"]["name"], :email => auth_hash["info"]["email"] + user.authorizations.build :provider => auth_hash["provider"], :uid => auth_hash["uid"], + :token => auth_hash["credentials"]["token"], :token_expiration => auth_hash["credentials"]["expires_at"] + user.save + complete_sign_in user + end + end + + def complete_sign_in(user) + sign_in user + redirect_back_or music_sessions_url end def destroy diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 46988a7f2..d82a4a5f1 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -19,9 +19,7 @@ class UsersController < ApplicationController def create @user = User.new(params[:jam_ruby_user]) if @user.save - sign_in @user - flash[:success] = "Welcome to Jamkazam!" - redirect_to @user + sign_in @user, :new => true else render 'new' end diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb index 3625479c7..11f8e7831 100644 --- a/app/helpers/sessions_helper.rb +++ b/app/helpers/sessions_helper.rb @@ -1,6 +1,10 @@ module SessionsHelper def sign_in(user) + if (:new was set to true) + flash[:success] = "Welcome to Jamkazam!" + redirect_to user + end cookies.permanent[:remember_token] = user.remember_token self.current_user = user end diff --git a/app/models/.gitkeep b/app/models/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb index 30f15fb1a..556ef44cc 100644 --- a/app/views/users/new.html.erb +++ b/app/views/users/new.html.erb @@ -1,6 +1,12 @@ <% provide(:title, 'Sign up') %>

Sign up

+
+
+ +
+
+
<%= form_for(@user) do |f| %> @@ -20,4 +26,4 @@ <%= f.submit "Create my account", class: "btn btn-large btn-primary" %> <% end %>
-
\ No newline at end of file + From 96c16eb1e58acff7393b6c8ddd31d0f0e45700d5 Mon Sep 17 00:00:00 2001 From: Mike Slemmer Date: Thu, 15 Nov 2012 01:30:30 -0800 Subject: [PATCH 3/3] now oauth is working --- app/controllers/sessions_controller.rb | 9 +++++---- app/helpers/users_helper.rb | 2 +- app/views/clients/index.html.erb | 4 ++-- app/views/sessions/new.html.erb | 8 +++++++- app/views/users/new.html.erb | 9 ++++++--- 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 31b960b73..e5e5f6b6f 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -4,8 +4,6 @@ class SessionsController < ApplicationController def new end -# oauth information: -# http://net.tutsplus.com/tutorials/ruby/how-to-use-omniauth-to-authenticate-your-users/ def create user = User.authenticate(params[:session][:email], params[:session][:password]) @@ -18,6 +16,8 @@ class SessionsController < ApplicationController end end +# OAuth docs +# http://net.tutsplus.com/tutorials/ruby/how-to-use-omniauth-to-authenticate-your-users/ def create_oauth auth_hash = request.env['omniauth.auth'] authorization = UserAuthorization.find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"]) @@ -49,9 +49,10 @@ class SessionsController < ApplicationController nil, # @country nil, nil) - user.user_authorizations.build :provider => auth_hash[:provider], :uid => auth_hash[:uid], - :token => auth_hash[:credentials][:token], :token_expiration => auth_hash[:credentials][:expires_at] + auth = user.user_authorizations.build :provider => auth_hash[:provider], :uid => auth_hash[:uid], + :token => auth_hash[:credentials][:token], :token_expiration => Time.at(auth_hash[:credentials][:expires_at]) user.save + auth.save complete_sign_in user end end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index cf892e07f..b3edd0a32 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -7,6 +7,6 @@ module UsersHelper hclass = options[:hclass] gravatar = gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}" - image_tag(gravatar_url, alt: user.name, class: "#{hclass}") + image_tag(gravatar_url, alt: "#{user.first_name} #{user.last_name}", class: "#{hclass}") end end diff --git a/app/views/clients/index.html.erb b/app/views/clients/index.html.erb index debe69e50..b52d70c06 100644 --- a/app/views/clients/index.html.erb +++ b/app/views/clients/index.html.erb @@ -21,7 +21,7 @@
  • - {name} + {first_name} {last_name} {subtext}
  • @@ -30,7 +30,7 @@
    <%= gravatar_for current_user, size: 52, hclass: "avatar medium" %>
    -

    <%= current_user.name %>

    +

    <%= "#{current_user.first_name} #{current_user.last_name}" %>

    <%= image_tag "down_arrow.png", :class=> "profile-toggle" %>
    • Profile
    • diff --git a/app/views/sessions/new.html.erb b/app/views/sessions/new.html.erb index 861efb8d8..744d3bfe2 100644 --- a/app/views/sessions/new.html.erb +++ b/app/views/sessions/new.html.erb @@ -1,6 +1,12 @@ <% provide(:title, "Sign in") %>

      Sign in

      +
      +
      + +
      +
      +
      <%= form_for(:session, url: sessions_path) do |f| %> @@ -16,4 +22,4 @@

      New user? <%= link_to "Sign up now!", signup_path %>

      -
      \ No newline at end of file +
    diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb index 69ba8413c..e57b20430 100644 --- a/app/views/users/new.html.erb +++ b/app/views/users/new.html.erb @@ -3,7 +3,7 @@
    - +
    @@ -11,8 +11,11 @@
    <%= form_for(@user) do |f| %> <%= render 'shared/error_messages', object: f.object %> - <%= f.label :name %> - <%= f.text_field :name %> + <%= f.label :first_name %> + <%= f.text_field :first_name %> + + <%= f.label :last_name %> + <%= f.text_field :last_name %> <%= f.label :email %> <%= f.text_field :email %>