Skip to content

Commit

Permalink
add on_validation option
Browse files Browse the repository at this point in the history
  • Loading branch information
Freeza91 committed Mar 18, 2016
1 parent 7a7e86d commit 3fa0187
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
23 changes: 19 additions & 4 deletions lib/omniauth/strategies/identity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Identity
option :on_login, nil
option :on_registration, nil
option :on_failed_registration, nil
option :on_validation, nil
option :locate_conditions, lambda{|req| {model.auth_key => req['auth_key']} }

def request_phase
Expand Down Expand Up @@ -60,13 +61,27 @@ def registration_form

def registration_phase
attributes = (options[:fields] + [:password, :password_confirmation]).inject({}){|h,k| h[k] = request[k.to_s]; h}
@identity = model.create(attributes)
if @identity.persisted?
env['PATH_INFO'] = callback_path
@identity = model.new(attributes)
self.env['omniauth.identity'] = @identity

if options[:on_validation]
unless options[:on_validation].call(env: self.env)
failed_registration =
if options[:on_failed_registration]
options[:on_failed_registration].call(self.env)
else
registration_form
end

return failed_registration
end
end

if @identity.save && @identity.persisted?
self.env['PATH_INFO'] = callback_path
callback_phase
else
if options[:on_failed_registration]
self.env['omniauth.identity'] = @identity
options[:on_failed_registration].call(self.env)
else
registration_form
Expand Down
23 changes: 22 additions & 1 deletion spec/omniauth/strategies/identity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,32 @@ def set_app!(identity_options = {})
post '/auth/identity/register', properties
auth_hash['uid'].should == 'abc'
end

context 'custom validation stage' do

context 'validation method returns true' do
it 'should register the user normally' do
set_app!(:on_validation => lambda{|env| true})

post '/auth/identity/register', properties
identity_hash.should_not be_nil
end
end

context 'validation method returns false' do
it 'should not register the user' do
set_app!(:on_validation => lambda{|env| false})

post '/auth/identity/register', properties
identity_hash.should be_nil
end
end
end
end

context 'with invalid identity' do
let(:properties) { {
:name => 'Awesome Dude',
:name => 'Awesome Dude',
:email => '[email protected]',
:password => 'NOT',
:password_confirmation => 'MATCHING'
Expand Down

0 comments on commit 3fa0187

Please sign in to comment.