Skip to content

Commit

Permalink
updated readme, added examples, changed view path variable name
Browse files Browse the repository at this point in the history
  • Loading branch information
Max committed Apr 18, 2010
1 parent b143f57 commit 2cf578e
Show file tree
Hide file tree
Showing 13 changed files with 377 additions and 4 deletions.
25 changes: 25 additions & 0 deletions 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
55 changes: 55 additions & 0 deletions 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'}
!

42 changes: 42 additions & 0 deletions 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')
31 changes: 31 additions & 0 deletions 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
21 changes: 21 additions & 0 deletions 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')
9 changes: 9 additions & 0 deletions 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
30 changes: 30 additions & 0 deletions 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')
22 changes: 22 additions & 0 deletions 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
16 changes: 16 additions & 0 deletions 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
62 changes: 62 additions & 0 deletions 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/

4 changes: 2 additions & 2 deletions lib/sinatra-authentication.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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|
Expand Down
50 changes: 50 additions & 0 deletions readme.markdown
Expand Up @@ -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

0 comments on commit 2cf578e

Please sign in to comment.