Skip to content
Browse files

Merge branch 'aaron'

  • Loading branch information...
2 parents 1dbc536 + 5bf3a03 commit cb9d3d73c5e21b21824e5901981578b655087698 @aaronbrethorst aaronbrethorst committed Mar 28, 2012
View
1 .rvmrc
@@ -0,0 +1 @@
+rvm use 1.9.3@listopia --create
View
29 app/controllers/sessions_controller.rb
@@ -1,8 +1,8 @@
class SessionsController < ApplicationController
-
+
def new
end
-
+
def create
user = User.find_by_email(params[:session][:email])
if user && user.authenticate(params[:session][:password])
@@ -13,37 +13,34 @@ def create
render 'new'
end
end
-
+
def login_with_facebook
- @oauth = Koala::Facebook::OAuth.new(FACEBOOK_APP_ID, FACEBOOK_APP_SECRET, FACEBOOK_CALLBACK_URL)
- redirect_to @oauth.url_for_oauth_code
+ redirect_to oauth.url_for_oauth_code(:permissions => "email")
end
-
+
def complete_login_with_facebook
@code = params[:code]
- @oauth = Koala::Facebook::OAuth.new(FACEBOOK_APP_ID, FACEBOOK_APP_SECRET, FACEBOOK_CALLBACK_URL)
- @access_token = @oauth.get_access_token(@code)
+ @access_token = oauth.get_access_token(@code)
- @user = load_user_from_facebook_access_token(@access_token)
+ @user = Authentication.load_user_from_facebook_access_token(@access_token)
if @user
- log_in_user(@user)
+ sign_in @user
+ redirect_back_or @user
else
flash.now[:error] = t('facebook-error')
redirect_to signup_path
end
end
-
+
def destroy
sign_out
redirect_to root_path
end
-
+
protected
- def log_in_user(user)
- response.headers[HEADER_AUTH_TOKEN] = @user.remember_token
- render :nothing => true, :status => :created
+ def oauth
+ @oauth ||= Koala::Facebook::OAuth.new(FACEBOOK_APP_ID, FACEBOOK_APP_SECRET, FACEBOOK_CALLBACK_URL)
end
-
end
View
3 app/models/authentication.rb
@@ -34,7 +34,8 @@ def self.load_user_from_facebook_profile(profile)
# otherwise, create the new user and auth objects. make sure they both get created or neither do.
Authentication.transaction do
- user = User.create!(:first_name => profile['name'].split(' ')[0], :last_name => profile['name'].split(' ')[1], :email => profile['email'], :password => SecureRandom.base64(15).tr('+/=lIO0', 'pqrsxyz'))
+ pass = SecureRandom.base64(15).tr('+/=lIO0', 'pqrsxyz')
+ user = User.create!(:first_name => profile['name'].split(' ')[0], :last_name => profile['name'].split(' ')[1], :email => profile['email'], :password => pass, :password_confirmation => pass)
auth = Authentication.create!(:user_id => user.id, :provider => 'facebook', :uid => profile['id'], :data_hash => profile)
end
return user
View
11 db/migrate/20120328222810_create_authentications.rb
@@ -0,0 +1,11 @@
+class CreateAuthentications < ActiveRecord::Migration
+ def change
+ create_table :authentications do |t|
+ t.integer :user_id
+ t.string :uid
+ t.string :provider
+ t.text :data_hash
+ t.timestamps
+ end
+ end
+end
View
11 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120328053931) do
+ActiveRecord::Schema.define(:version => 20120328222810) do
create_table "answers", :force => true do |t|
t.string "name"
@@ -28,6 +28,15 @@
add_index "answers", ["list_id", "created_at"], :name => "index_answers_on_list_id_and_created_at"
+ create_table "authentications", :force => true do |t|
+ t.integer "user_id"
+ t.string "uid"
+ t.string "provider"
+ t.text "data_hash"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
create_table "lists", :force => true do |t|
t.string "name"
t.string "details"
View
11 test/fixtures/authentications.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+# This model initially had no columns defined. If you add columns to the
+# model remove the '{}' from the fixture names and add the columns immediately
+# below each fixture, per the syntax in the comments below
+#
+one: {}
+# column: value
+#
+two: {}
+# column: value
View
7 test/unit/authentication_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class AuthenticationTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end

0 comments on commit cb9d3d7

Please sign in to comment.
Something went wrong with that request. Please try again.