Module: Sinatra::Doorman::Base

Defined in:
lib/doorman/base.rb

Defined Under Namespace

Modules: Helpers

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/doorman/base.rb', line 58

def self.registered(app)
  app.helpers Helpers

  app.use Warden::Manager do |manager|
    manager.failure_app = lambda { |env|
      env['x-rack.flash'][:error] = Messages[:auth_required] if defined?(Rack::Flash)
      [302, { 'Location' => '/login' }, ['']] 
    }
  end

  Warden::Strategies.add(:password, PasswordStrategy) 

  app.get '/signup/?' do
    redirect '/home' if authenticated?
    haml :signup
  end

  app.post '/signup' do
    redirect '/home' if authenticated?

    user = User.new(params[:user])
    
    unless user.save
      notify :error, user.errors.first
      redirect back
    end

    notify :success, :signup_success
    notify :success, 'Signed up: ' + user.confirm_token
    Pony.mail(
      :to => user.email, 
      :from => "no-reply@#{env['SERVER_NAME']}", 
      :body => token_link('confirm', user))
    redirect "/"
  end

  app.get '/confirm/:token/?' do
    redirect '/home' if authenticated?

    if params[:token].nil? || params[:token].empty?
      notify :error, :confirm_no_token
      redirect '/'
    end

    user = User.first(:confirm_token => params[:token])
    if user.nil?
      notify :error, :confirm_no_user
    else
      user.confirm_email!
      notify :success, :confirm_success
    end
    redirect '/login'
  end

  app.get '/login/?' do
    redirect '/home' if authenticated?
    haml :login
  end

  app.post '/login' do
    env['warden'].authenticate(:password)
    redirect '/home' if authenticated?
    notify :error, env['warden'].message
    redirect back
  end

  app.get '/logout/?' do
    env['warden'].logout(:default)
    notify :success, :logout_success
    redirect '/login'
  end
end