Permalink
Browse files

updated readme, added examples, changed view path variable name

  • Loading branch information...
1 parent b143f57 commit 2cf578e7029751737a5c00cc6397512f3d15a5c9 Max committed Apr 18, 2010
View
25 example/dm_extend_app.rb
@@ -0,0 +1,25 @@
+require 'rubygems'
+require 'sinatra'
+require 'haml'
+require 'dm-core'
+require 'rack-flash'
+require 'sinatra-authentication'
+
+class DmUser
+ property :name, String
+end
+
+DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/test.db")
+DataMapper.auto_migrate!
+
+set :sinatra_authentication_view_path, Pathname(__FILE__).dirname.expand_path + "extend_views/"
+use Rack::Session::Cookie, :secret => "heyhihello"
+use Rack::Flash
+
+set :environment, 'development'
+set :public, 'public'
+set :views, 'views'
+
+get '/' do
+ haml "= render_login_logout", :layout => :layout
+end
View
55 example/dm_sinbook.rb
@@ -0,0 +1,55 @@
+require 'rubygems'
+require 'sinatra'
+require 'haml'
+require 'sinbook'
+require 'dm-core'
+require 'sinatra-authentication'
+
+facebook do
+ api_key 'aa2db1b96cb7b57f0c5b1d4d3d8f0a22'
+ secret '21d94ee63969ae3b3f833689838ca00f'
+ app_id 48652736613
+ url 'peoplewithjetpacks.com:4568/'
+ callback 'peoplewithjetpacks.com:4568/'
+end
+
+set :port, 4568
+
+DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/test.db")
+DataMapper.auto_migrate!
+
+use Rack::Session::Cookie, :secret => "heyhihello"
+
+set :environment, 'development'
+set :public, 'public'
+set :views, 'views'
+
+get '/' do
+ haml :main
+end
+
+get '/test' do
+ login_required
+ 'hihihi'
+end
+
+__END__
+
+@@ layout
+%html{:xmlns=>"http://www.w3.org/1999/xhtml", :'xmlns:fb'=>"http://www.facebook.com/2008/fbml"}
+ %head
+ %title Welcome to my Facebook Connect website!
+ %script{:type => 'text/javascript', :src => 'http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US'}
+ %body
+ = render_login_logout
+ = yield
+ :javascript
+ FB.init("#{fb.api_key}", "/receiver")
+
+@@ main
+- if fb[:user]
+ Hi,
+ %fb:profile-pic{:uid => fb[:user]}
+ %fb:name{:uid => fb[:user], :useyou => 'false', :firstnameonly => 'true'}
+ !
+
View
42 example/extend_views/edit.haml
@@ -0,0 +1,42 @@
+#sinatra_authentication
+ #sinatra_authentication_flash= flash[:notice]
+ %h1
+ Edit
+ - if @user.id == current_user.id
+ account
+ - else
+ - if @user.email
+ = @user.email
+ - elsif @user.fb_uid
+ <fb:name uid=#{@user.fb_uid} linked='false' />
+ - else
+ account
+ %form{:action => "/users/#{@user.id}/edit", :method => "post"}
+ .field
+ .label
+ %label{:for => "user_email"} Email
+ %input{ :id => "user_email", :name => "user[email]", :size => 30, :type => "text", :value => @user.email }
+ .field
+ .label
+ %label{:for => "user_password"} New password
+ %input{ :id => "user_password", :name => "user[password]", :size => 30, :type => "password" }
+ .field
+ .label
+ %label{:for => "user_password_confirmation"} Confirm
+ %input{ :id => "user_password_confirmation", :name => "user[password_confirmation]", :size => 30, :type => "password" }
+ -# don't render permission field if admin and editing yourself so you don't shoot yourself in the foot
+ - if current_user.admin? && current_user.id != @user.id
+ .field
+ .label
+ %label{:for => 'permission_level'} Permission level
+ %select{ :id => "permission_level", :name => "user[permission_level]" }
+ %option{:value => -1, :selected => @user.admin?}
+ Admin
+ %option{:value => 1, :selected => @user.permission_level == 1}
+ Authenticated user
+ .buttons
+ %input{ :value => "Update", :type => "submit" }
+ - if Sinatra.const_defined?('FacebookObject')
+ - unless @user.fb_uid
+ |
+ = render_facebook_connect_link('Link account with Facebook')
View
31 example/extend_views/index.haml
@@ -0,0 +1,31 @@
+#sinatra_authentication
+ %h1.page_title Users
+ %table
+ %tr
+ %th
+ - if current_user.admin?
+ %th permission level
+ - @users.each do |user|
+ %tr
+ %td
+ - if user.email
+ = user.email
+ - elsif user.fb_uid
+ <fb:name uid=#{user.fb_uid} />
+ - else
+ "user #{user.id}"
+ - if current_user.admin?
+ %td= user.permission_level
+ %td
+ = user.name
+ %td
+ %a{:href => "/users/#{user.id}"} show
+ - if current_user.admin?
+ %td
+ %a{:href => "/users/#{user.id}/edit"} edit
+ %td
+ -# this doesn't work for tk
+ - if !user.site_admin?
+ %a{:href => "/users/#{user.id}/delete", :onclick => "return confirm('you sure?')"} delete
+ - else
+ site admin
View
21 example/extend_views/login.haml
@@ -0,0 +1,21 @@
+#sinatra_authentication
+ #sinatra_authentication_flash= flash[:notice]
+ %h1.page_title Login
+ %form{:action => "/login", :method => "post"}
+ .field
+ .label
+ %label{:for => "user_email'"} Email
+ %input{:id => "user_email", :name => "email", :size => 30, :type => "text"}
+ .field
+ .label
+ %label{:for => "user_password"} Password
+ %input{:id => "user_password", :name => "password", :size => 30, :type => "password"}
+ .buttons
+ %input{:value => "login", :type => "submit"}
+ %a{:href => "/signup", :class => 'sinatra_authentication_link'}
+ Signup
+ - if Sinatra.const_defined?('FacebookObject')
+ .third_party_signup
+ %h3.section_title One click login:
+ .login_link.facebook_login
+ = render_facebook_connect_link('Login using facebook', :size => 'large')
View
9 example/extend_views/show.haml
@@ -0,0 +1,9 @@
+#sinatra_authentication
+ %h1.page_title
+ - if @user.email
+ = @user.email
+ - elsif @user.fb_uid
+ <fb:name uid=#{@user.fb_uid} linked='false' />
+ - if current_user.admin?
+ %h2 permission level
+ = @user.permission_level
View
30 example/extend_views/signup.haml
@@ -0,0 +1,30 @@
+%h1 This view is overridden
+#sinatra_authentication
+ #sinatra_authentication_flash= flash[:notice]
+ %h1.page_title Signup
+ %form{:action => "/signup", :method => "post"}
+ .field
+ .label
+ %label{:for => "user_email"} Email
+ %input{ :id => "user_email", :name => "user[email]", :size => 30, :type => "text" }
+ .field
+ .label
+ %label{:for => "user_password"} Password
+ %input{ :id => "user_password", :name => "user[password]", :size => 30, :type => "password" }
+ .field
+ .label
+ %label{:for => "user_name"} Name
+ %input{ :id => "user_name", :name => "user[name]", :size => 30, :type => "text" }
+ .field
+ .label
+ %label{:for => "user_password_confirmation"} Confirm Password
+ %input{ :id => "user_password_confirmation", :name => "user[password_confirmation]", :size => 30, :type => "password" }
+ .buttons
+ %input{ :value => "Create account", :type => "submit" }
+ %a{:href => "/login", :class => 'sinatra_authentication_link'}
+ Login
+ - if Sinatra.const_defined?('FacebookObject')
+ .third_party_signup
+ %h3.section_title One click signup:
+ .login_link.facebook_login
+ = render_facebook_connect_link('Signup using facebook', :size => 'large')
View
22 example/mm_app.rb
@@ -0,0 +1,22 @@
+require 'rubygems'
+require 'sinatra/base'
+require 'haml'
+require 'mongo_mapper'
+require 'sinatra-authentication'
+
+logger = Logger.new($stdout)
+MongoMapper.connection = Mongo::Connection.new('db.mongohq.com', 27017, :logger => logger)
+MongoMapper.database = "fdbk"
+MongoMapper.database.authenticate(ENV['mongohq_user'], ENV['mongohq_pass'])
+
+class TestApp < Sinatra::Base
+ use Rack::Session::Cookie, :secret => "heyhihello"
+
+ set :environment, 'development'
+ set :public, 'public'
+ set :views, 'views'
+
+ get '/' do
+ haml "= render_login_logout", :layout => :layout
+ end
+end
View
16 example/tc_app.rb
@@ -0,0 +1,16 @@
+require 'rubygems'
+require 'sinatra'
+require 'haml'
+require 'rufus/tokyo'
+require 'sinatra-authentication'
+
+use Rack::Session::Cookie, :secret => "heyhihello"
+TcUserTable.cabinet_path = File.dirname(__FILE__)
+
+set :environment, 'development'
+set :public, 'public'
+set :views, 'views'
+
+get '/' do
+ haml "= render_login_logout", :layout => :layout
+end
View
62 example/tc_sinbook.rb
@@ -0,0 +1,62 @@
+require 'rubygems'
+require 'haml'
+require 'sinbook'
+require 'rufus/tokyo'
+require 'sinatra'
+require 'sinatra-authentication'
+
+use Rack::Session::Cookie, :secret => "heyhihello"
+TcUserTable.cabinet_path = File.dirname(__FILE__)
+
+facebook do
+ api_key 'aa2db1b96cb7b57f0c5b1d4d3d8f0a22'
+ secret '21d94ee63969ae3b3f833689838ca00f'
+ app_id 48652736613
+ url 'peoplewithjetpacks.com:4568/'
+ callback 'peoplewithjetpacks.com:4568/'
+end
+
+set :port, 4568
+
+get '/' do
+ haml :main
+end
+
+get '/test' do
+ login_required
+ 'hihihi'
+end
+
+__END__
+
+@@ layout
+%html{:xmlns=>"http://www.w3.org/1999/xhtml", :'xmlns:fb'=>"http://www.facebook.com/2008/fbml"}
+ %head
+ %title Welcome to my Facebook Connect website!
+ %script{:type => 'text/javascript', :src => 'http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US'}
+ %script{:type => 'text/javascript', :src => 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'}
+ :javascript
+ $(document).ready(function(){
+ /* test facebook crap works with ajax */
+ $('.sinatra-authentication-login').click(function(){
+ $.get($(this).attr('href'), {}, function(data){
+ $('#test_box').html(data);
+ });
+ return false;
+ });
+ });
+ %body
+ = render_login_logout
+ = yield
+ :javascript
+ FB.init("#{fb.api_key}", "/receiver")
+ #test_box
+
+@@ main
+- if fb[:user]
+ Hi,
+ %fb:profile-pic{:uid => fb[:user]}
+ %fb:name{:uid => fb[:user], :useyou => 'false', :firstnameonly => 'true'}
+ !
+ %br/
+
View
4 lib/sinatra-authentication.rb
@@ -11,7 +11,7 @@ def self.registered(app)
#sinatra 9.1.1 doesn't have multiple view capability anywhere
#so to get around I have to do it totally manually by
#loading the view from this path into a string and rendering it
- set :lil_authentication_view_path, Pathname(__FILE__).dirname.expand_path + "views/"
+ set :sinatra_authentication_view_path, Pathname(__FILE__).dirname.expand_path + "views/"
get '/users' do
login_required
@@ -208,7 +208,7 @@ def use_layout?
#BECAUSE sinatra 9.1.1 can't load views from different paths properly
def get_view_as_string(filename)
- view = options.lil_authentication_view_path + filename
+ view = options.sinatra_authentication_view_path + filename
data = ""
f = File.open(view, "r")
f.each_line do |line|
View
50 readme.markdown
@@ -163,3 +163,53 @@ If they aren't already logged in to the app through the normal login form,
it creates a new user in the database without an email address or password.
They can later add this data by going to "/users/#{current_user.id}/edit",
which will allow them to log in using their email address and password, OR their facebook account.
+
+## OVERRIDING DEFAULT VIEWS
+
+Right now if you're going to override sinatra-authentication's views, you have to override all of them.
+This is something I hope to change in a future release.
+
+To override the default view path do something like this:
+
+ set :sinatra_authentication_view_path, Pathname(__FILE__).dirname.expand_path + "my_views/"
+
+And then the views you'll need to define are:
+
+* show.haml
+* index.haml
+* signup.haml
+* login.haml
+* edit.haml
+
+The signup and edit form fields are named so they pass a hash called 'user' to the server:
+
+ %input{:name => "user[email]", :size => 30, :type => "text", :value => @user.email}
+ %input{:name => "user[password]", :size => 30, :type => "password"}
+ %input{:name => "user[password_confirmation]", :size => 30, :type => "password"}
+
+ %select{:name => "user[permission_level]"}
+ %option{:value => -1, :selected => @user.admin?}
+ Admin
+ %option{:value => 1, :selected => @user.permission_level == 1}
+ Authenticated user
+
+The login form fields just pass a field called email and a field called password:
+
+ %input{:name => "email", :size => 30, :type => "text"}
+ %input{:name => "password", :size => 30, :type => "password"}
+
+To add methods or properties to the User class, you have to access the underlying database user class, like so:
+
+ class DmUser
+ property :name, String
+ property :has_dog, Boolean, :default => false
+ end
+
+The database user classes are named as follows:
+
+* for Datamapper:
+ > DmUser
+* for Rufus Tokyo:
+ > TcUser
+* for Mongomapper:
+ > MmUser
View
14 sinatra-authentication.gemspec
@@ -5,11 +5,11 @@
Gem::Specification.new do |s|
s.name = %q{sinatra-authentication}
- s.version = "0.2.4"
+ s.version = "0.3.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Max Justus Spransy"]
- s.date = %q{2010-01-14}
+ s.date = %q{2010-04-17}
s.description = %q{Simple authentication plugin for sinatra.}
s.email = %q{maxjustus@gmail.com}
s.extra_rdoc_files = [
@@ -38,7 +38,13 @@ Gem::Specification.new do |s|
"sinatra-authentication.gemspec",
"test/datamapper_test.rb",
"test/lib/dm_app.rb",
+ "test/lib/dm_extend_app.rb",
"test/lib/dm_sinbook.rb",
+ "test/lib/extend_views/edit.haml",
+ "test/lib/extend_views/index.haml",
+ "test/lib/extend_views/login.haml",
+ "test/lib/extend_views/show.haml",
+ "test/lib/extend_views/signup.haml",
"test/lib/helper.rb",
"test/lib/mm_app.rb",
"test/lib/tc_app.rb",
@@ -58,6 +64,7 @@ Gem::Specification.new do |s|
"test/lib/mm_app.rb",
"test/lib/tc_sinbook.rb",
"test/lib/helper.rb",
+ "test/lib/dm_extend_app.rb",
"test/lib/dm_app.rb",
"test/datamapper_test.rb",
"test/mongomapper_test.rb",
@@ -76,13 +83,15 @@ Gem::Specification.new do |s|
s.add_runtime_dependency(%q<dm-timestamps>, [">= 0"])
s.add_runtime_dependency(%q<rufus-tokyo>, [">= 0"])
s.add_runtime_dependency(%q<sinbook>, [">= 0"])
+ s.add_runtime_dependency(%q<rack-flash>, [">= 0"])
else
s.add_dependency(%q<sinatra>, [">= 0"])
s.add_dependency(%q<dm-core>, [">= 0"])
s.add_dependency(%q<dm-validations>, [">= 0"])
s.add_dependency(%q<dm-timestamps>, [">= 0"])
s.add_dependency(%q<rufus-tokyo>, [">= 0"])
s.add_dependency(%q<sinbook>, [">= 0"])
+ s.add_dependency(%q<rack-flash>, [">= 0"])
end
else
s.add_dependency(%q<sinatra>, [">= 0"])
@@ -91,6 +100,7 @@ Gem::Specification.new do |s|
s.add_dependency(%q<dm-timestamps>, [">= 0"])
s.add_dependency(%q<rufus-tokyo>, [">= 0"])
s.add_dependency(%q<sinbook>, [">= 0"])
+ s.add_dependency(%q<rack-flash>, [">= 0"])
end
end

0 comments on commit 2cf578e

Please sign in to comment.