diff --git a/config/warden.rb b/config/warden.rb new file mode 100644 index 0000000..40d530e --- /dev/null +++ b/config/warden.rb @@ -0,0 +1,20 @@ +require 'warden' + +Warden::Manager.serialize_into_session{|user| user.id } +Warden::Manager.serialize_from_session{|id| User[id] } + +Warden::Strategies.add(:password) do + def valid? + params["email"] || params["password"] + end + + def authenticate! + user = User.first(email: params["email"]) + + if user && user.authenticate(params["password"]) + success! user + else + fail! "Could not log in" + end + end +end diff --git a/db/seeds.rb b/db/seeds.rb index 1400809..7e7d9f0 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,2 +1,7 @@ +unless env.production? + DB[:communities].truncate +end + + Community.create name: "New Community", description: "This is a nice community" diff --git a/routes/communities.rb b/routes/communities.rb index f9a95fa..3336c76 100644 --- a/routes/communities.rb +++ b/routes/communities.rb @@ -10,8 +10,8 @@ class Yogurt end r.post do - attributes = %w[name description private] - @community = Community.new.set_fields(r.params, attributes) + fields = %w[name description private] + @community = Community.new.set_fields(r.params, fields) if @community.save r.redirect '/communities' @@ -24,28 +24,30 @@ class Yogurt r.get 'new' do @community = Community.new - r.get { :new } + :new end - r.on ':id' do |id| + r.on :id do |id| @community = Community.with_pk!(id.to_i) r.get('edit') { :edit } - r.put do - attributes = %w[name description private] + r.is do + r.put do + fields = %w[name description private] - if @community.update_fields(r.params, attributes) - r.redirect '/communities' - else - :edit + if @community.update_fields(r.params, fields) + r.redirect '/communities' + else + :edit + end end - end - r.delete do - @community.delete + r.delete do + @community.delete - r.redirect '/communities' + r.redirect '/communities' + end end end end