socialite 0.1.0.pre.3 → 0.1.0.pre.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,14 @@
1
1
  module Socialite
2
- class SessionsController < ApplicationController
2
+ class SessionsController < SocialiteController
3
3
  unloadable
4
4
 
5
+ before_filter :ensure_user, only: [:destroy]
6
+
7
+ # Login Page
5
8
  def new
6
- # Login Page
9
+ if current_user
10
+ redirect_to main_app.root_path, :alert => 'You are already registered.'
11
+ end
7
12
  end
8
13
 
9
14
  def create
@@ -17,20 +22,20 @@ module Socialite
17
22
  # account. But we found the identity and the user associated with it
18
23
  # is the current user. So the identity is already associated with
19
24
  # this user. So let's display an error message.
20
- redirect_to main_app.root_url, :notice => "You have already linked this account"
25
+ redirect_to after_link_path, :notice => "You have already linked this account"
21
26
  else
22
27
  # The identity is not associated with the current_user so lets
23
28
  # associate the identity.
24
29
  @identity.user = current_user
25
30
  @identity.save
26
- redirect_to main_app.root_url, :notice => "Account successfully authenticated"
31
+ redirect_to after_link_path, :notice => "Account successfully authenticated"
27
32
  end
28
33
  else # User is not logged in, this is a new signin
29
34
  if @identity.user.present?
30
35
  # The identity we found had a user associated with it so let's
31
36
  # just log them in here
32
37
  self.current_user = @identity.user
33
- redirect_to main_app.root_url, :notice => "Signed in!"
38
+ redirect_to after_login_path, :notice => "Signed in!"
34
39
  else
35
40
  # The authentication has no user assigned and there is no user signed in
36
41
  # Our decision here is to create a new account for the user
@@ -42,14 +47,14 @@ module Socialite
42
47
  # So we just load it up
43
48
  else
44
49
  # otherwise we have to create a user with the auth hash
45
- Socialite.user_class.create_from_omniauth(auth)
50
+ Socialite.user_class.find_or_create_from_omniauth(auth)
46
51
  # NOTE: we will handle the different types of data we get back
47
52
  # from providers at the model level in create_from_omniauth
48
53
  end
49
54
  # We can now link the authentication with the user and log him in
50
55
  user.identities << @identity
51
56
  self.current_user = user
52
- redirect_to main_app.root_path, notice: "Welcome to The app!"
57
+ redirect_to after_signup_path, notice: "Welcome to the app!"
53
58
 
54
59
  # No user associated with the identity so we need to create a new one
55
60
  # redirect_to new_user_url, :notice => "Please finish registering"
@@ -58,12 +63,12 @@ module Socialite
58
63
  end
59
64
 
60
65
  def destroy
61
- self.current_user = nil
62
- redirect_to main_app.root_url, :notice => 'Signed out!'
66
+ logout!
67
+ redirect_to(main_app.root_url, :notice => 'Signed out!')
63
68
  end
64
69
 
65
70
  def failure
66
- redirect_to main_app.root_url, :alert => "Authentication failed, please try again."
71
+ redirect_to after_failure_path, :alert => "Authentication failed, please try again."
67
72
  end
68
73
  end
69
74
  end
@@ -0,0 +1,9 @@
1
+ module Socialite
2
+ class SocialiteController < ::ApplicationController
3
+ layout 'socialite/socialite'
4
+
5
+ # Override before ensure_user if it's set in the parent app's
6
+ # ApplicationController.
7
+ skip_before_filter :ensure_user
8
+ end
9
+ end
@@ -1,8 +1,11 @@
1
1
  module Socialite
2
- class UsersController < ApplicationController
2
+ class UsersController < SocialiteController
3
3
  unloadable
4
4
 
5
5
  def new
6
+ if current_user
7
+ redirect_to main_app.root_path, notice: 'You are already registered.'
8
+ end
6
9
  @user = env['omniauth.identity'] ||= Socialite.user_class.new
7
10
  end
8
11
  end
@@ -7,5 +7,3 @@
7
7
  = csrf_meta_tags
8
8
  %body
9
9
  = yield
10
-
11
-
@@ -8,7 +8,25 @@ module Socialite
8
8
  end
9
9
 
10
10
  def current_user
11
- @current_user ||= Socialite.user_class.find(session[:user_id])
11
+ @current_user ||= if session.has_key?(:user_id)
12
+ Socialite.user_class.find(session[:user_id])
13
+ end
14
+ rescue ActiveRecord::RecordNotFound
15
+ session[:user_id] = nil
16
+ end
17
+
18
+ def ensure_user
19
+ if defined?(super)
20
+ super
21
+ else
22
+ unless user_signed_in?
23
+ redirect_to login_path, :alert => 'You must be logged in to use this feature.'
24
+ end
25
+ end
26
+ end
27
+
28
+ def logout!
29
+ self.current_user = nil and session.destroy
12
30
  end
13
31
 
14
32
  def user_signed_in?
@@ -19,6 +37,26 @@ module Socialite
19
37
  @current_user = user
20
38
  session[:user_id] = user.nil? ? user : user.id
21
39
  end
40
+
41
+ def after_link_path
42
+ main_app.root_path
43
+ end
44
+
45
+ def after_signup_path
46
+ main_app.root_path
47
+ end
48
+
49
+ def after_failure_path
50
+ main_app.root_path
51
+ end
52
+
53
+ def after_login_path
54
+ main_app.root_path
55
+ end
56
+
57
+ def after_logout_path
58
+ main_app.root_path
59
+ end
22
60
  end
23
61
  end
24
62
  end
@@ -9,7 +9,6 @@ module Socialite
9
9
  include OmniAuth::Identity::SecurePassword
10
10
 
11
11
  included do
12
-
13
12
  attr_accessible :email, :name, :password, :password_confirmation
14
13
 
15
14
  has_secure_password if defined?(BCrypt)
@@ -29,10 +28,23 @@ module Socialite
29
28
  # include OmniAuth::Identity::Model#::ClassMethods
30
29
  # include OmniAuth::Identity::SecurePassword::ClassMethods
31
30
 
31
+ def find_from_omniauth(auth)
32
+ if auth['info']['email']
33
+ find_by_email(auth['info']['email'])
34
+ else
35
+ find_by_email("#{auth['info']['name']}@#{auth['provider']}.com")
36
+ end
37
+ end
38
+
39
+ def find_or_create_from_omniauth(auth)
40
+ find_from_omniauth(auth) || create_from_omniauth(auth)
41
+ end
42
+
32
43
  def create_from_omniauth(auth)
33
44
  create do |user|
34
45
  user.name = auth['info']['name']
35
46
  user.email = auth['info']['email']
47
+ user.email ||= "#{auth['info']['nickname']}@#{auth['provider']}.com"
36
48
  user.password ||= rand(36**10).to_s(36)
37
49
  end
38
50
  end
@@ -1,3 +1,3 @@
1
1
  module Socialite
2
- VERSION = '0.1.0.pre.3'
2
+ VERSION = '0.1.0.pre.4'
3
3
  end
@@ -10,7 +10,7 @@ feature "Facebook Registration", :omniauth => true do
10
10
 
11
11
  scenario "Signing up with Facebook" do
12
12
  visit '/auth/facebook'
13
- page.should have_text 'Welcome to The app!'
13
+ page.should have_text 'Welcome to the app!'
14
14
  end
15
15
  end
16
16
 
@@ -24,7 +24,7 @@ feature "Identity Registration" do
24
24
  fill_in 'password', :with => 'secrets'
25
25
  fill_in 'password_confirmation', :with => 'secrets'
26
26
  click_button 'Sign Up'
27
- page.should have_text 'Welcome to The app!'
27
+ page.should have_text 'Welcome to the app!'
28
28
  page.should have_no_css('.error')
29
29
  page.should have_no_button('Sign Up')
30
30
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe User do
3
+ describe User, :omniauth => true do
4
4
  let(:linked_user) { FactoryGirl.create(:linked_user) }
5
5
 
6
6
  it { should have_many(:identities).dependent(:destroy) }
@@ -11,6 +11,30 @@ describe User do
11
11
 
12
12
  it { should have_db_index(:email).unique(true) }
13
13
 
14
+ it { should validate_uniqueness_of(:email).case_insensitive }
15
+
16
+ context 'existing users' do
17
+ let!(:twitter_user) { FactoryGirl.create(:user, email: 'johndoe@twitter.com') }
18
+
19
+ describe 'finding existing instead of creating additional' do
20
+ subject { described_class.find_or_create_from_omniauth(OmniAuth.config.mock_auth[:twitter]) }
21
+
22
+ its(:email) { should eql('johndoe@twitter.com') }
23
+
24
+ it 'retrieved the record; and did not create an additional one' do
25
+ lambda { subject }.should_not change(described_class, :count)
26
+ end
27
+ end
28
+
29
+ describe 'creating user if finder fails' do
30
+ subject { described_class.find_or_create_from_omniauth(OmniAuth.config.mock_auth[:facebook]) }
31
+
32
+ it 'returns a new record' do
33
+ lambda { subject }.should change(described_class, :count).by(1)
34
+ end
35
+ end
36
+ end
37
+
14
38
  context 'with associated identities' do
15
39
  subject { linked_user }
16
40
 
@@ -12,20 +12,47 @@ RSpec.configure do |config|
12
12
  # }
13
13
 
14
14
  OmniAuth.config.add_mock(:facebook, {
15
- 'uid' => '1234',
16
- 'extra' => {
17
- 'user_hash' => {
18
- 'email' => 'foobar@example.com',
19
- 'first_name' => 'John',
20
- 'last_name' => 'Doe',
21
- 'gender' => 'Male'
15
+ :uid => '1234567',
16
+ :info => {
17
+ :nickname => 'jbloggs',
18
+ :email => 'joe@bloggs.com',
19
+ :name => 'Joe Bloggs',
20
+ :first_name => 'Joe',
21
+ :last_name => 'Bloggs',
22
+ :image => 'http://graph.facebook.com/1234567/picture?type=square',
23
+ :urls => { :Facebook => 'http://www.facebook.com/jbloggs' },
24
+ :location => 'Palo Alto, California',
25
+ :verified => true
26
+ },
27
+ :credentials => {
28
+ :token => 'ABCDEF...', # OAuth 2.0 access_token, which you may wish to store
29
+ :expires_at => 1321747205, # when the access token expires (it always will)
30
+ :expires => true # this will always be true
31
+ },
32
+ :extra => {
33
+ :raw_info => {
34
+ :id => '1234567',
35
+ :name => 'Joe Bloggs',
36
+ :first_name => 'Joe',
37
+ :last_name => 'Bloggs',
38
+ :link => 'http://www.facebook.com/jbloggs',
39
+ :username => 'jbloggs',
40
+ :location => { :id => '123456789', :name => 'Palo Alto, California' },
41
+ :gender => 'male',
42
+ :email => 'joe@bloggs.com',
43
+ :timezone => -8,
44
+ :locale => 'en_US',
45
+ :verified => true,
46
+ :updated_time => '2011-11-11T06:21:03+0000'
22
47
  }
23
48
  }
24
49
  })
25
50
 
26
51
  OmniAuth.config.add_mock(:twitter, {
27
- :uid => '12345',
28
- :nickname => 'zapnap'
52
+ :uid => '1337',
53
+ :info => {
54
+ :name => 'johndoe'
55
+ }
29
56
  })
30
57
  end
31
58
 
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: socialite
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: 6
5
- version: 0.1.0.pre.3
5
+ version: 0.1.0.pre.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Justin Smestad
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-12 00:00:00.000000000 Z
12
+ date: 2013-02-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -309,10 +309,10 @@ files:
309
309
  - app/assets/images/socialite/twitter_64.png
310
310
  - app/assets/stylesheets/socialite/socialite.css
311
311
  - app/assets/stylesheets/socialite.css
312
- - app/controllers/socialite/application_controller.rb
313
312
  - app/controllers/socialite/sessions_controller.rb
313
+ - app/controllers/socialite/socialite_controller.rb
314
314
  - app/controllers/socialite/users_controller.rb
315
- - app/views/layouts/socialite/application.html.haml
315
+ - app/views/layouts/socialite/socialite.html.haml
316
316
  - app/views/socialite/identities/_identities.html.haml
317
317
  - app/views/socialite/identities/new.html.haml
318
318
  - app/views/socialite/sessions/new.html.haml
@@ -409,7 +409,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
409
409
  - !ruby/object:Gem::Version
410
410
  segments:
411
411
  - 0
412
- hash: 4151013875851256199
412
+ hash: -4137053781704472879
413
413
  version: '0'
414
414
  required_rubygems_version: !ruby/object:Gem::Requirement
415
415
  none: false
@@ -1,4 +0,0 @@
1
- module Socialite
2
- class ApplicationController < ActionController::Base
3
- end
4
- end