Skip to content

Instantly share code, notes, and snippets.

@StasKoval
Created March 28, 2013 20:23
Show Gist options
  • Save StasKoval/5266469 to your computer and use it in GitHub Desktop.
Save StasKoval/5266469 to your computer and use it in GitHub Desktop.
include OmniAuth::Strategy
option :fields, [:name, :email]
option :on_login, nil
option :on_registration, nil
option :on_failed_registration, nil
option :locate_conditions, lambda{|req| {model.auth_key => req['auth_key']} }
def request_phase
if options[:on_login]
options[:on_login].call(self.env)
else
OmniAuth::Form.build(
:title => (options[:title] || "Identity Verification"),
:url => callback_path
) do |f|
f.text_field 'Login', 'auth_key'
f.password_field 'Password', 'password'
f.html "<p align='center'><a href='#{registration_path}'>Create an Identity</a></p>"
end.to_response
end
end
def registration_form
if options[:on_registration]
options[:on_registration].call(self.env)
else
OmniAuth::Form.build(:title => 'Register Identity') do |f|
options[:fields].each do |field|
f.text_field field.to_s.capitalize, field.to_s
end
f.password_field 'Password', 'password'
f.password_field 'Confirm Password', 'password_confirmation'
end.to_response
end
end
def registration_phase
attributes = (options[:fields] + [:password, :password_confirmation]).inject({}){|h,k| h[k] = request[k.to_s]; h}
@developer = model.create(attributes)
if @developer.persisted?
env['PATH_INFO'] = callback_path
callback_phase
else
if options[:on_failed_registration]
self.env['omniauth.developer'] = @developer
options[:on_failed_registration].call(self.env)
else
registration_form
end
end
end
def registration_path
options[:registration_path] || "#{path_prefix}/#{name}/register"
end
uid do
request.params[options.uid_field.to_s]
end
info do
options.fields.inject({}) do |hash, field|
hash[field] = request.params[field.to_s]
hash
end
end
def developer
if options.locate_conditions.is_a? Proc
conditions = instance_exec(request, &options.locate_conditions)
conditions.to_hash
else
conditions = options.locate_conditions.to_hash
end
@developer ||= model.authenticate(conditions, request['password'] )
end
def model
options[:model]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment