Permalink
Browse files

Authentication controller completed

  • Loading branch information...
nmanzi committed Oct 7, 2011
2 parents 2b2bb5c + ac4d91c commit 2c24550f6f283ad6e0620e8174e3097f6b0c4a6a
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
@@ -0,0 +1,3 @@
+// Place all the styles related to the sessions controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -0,0 +1,30 @@
+class SessionsController < ApplicationController
+ def new
+ if session[:authenticated]
+ redirect_to root_url, :flash => {:info => "Already logged in, you fool."}
+ end
+ end
+
+ def create
+ if authenticate(params[:username], params[:password])
+ session[:authenticated] = true
+ redirect_to root_url, :flash => {:success => "Logged in!"}
+ else
+ flash.now[:error] = "Invalid user/pass."
+ render "new"
+ end
+ end
+
+ def destroy
+ session[:authenticated] = nil
+ redirect_to root_url, :flash => {:success => "Logged out!"}
+ end
+
+ private
+
+ def authenticate(username, password)
+ if username == "admin" && password == "admin"
+ return true
+ end
+ end
+end
@@ -0,0 +1,2 @@
+module SessionsHelper
+end
@@ -0,0 +1,22 @@
+<div class="page-header"><h1>Login <small>or die.</small></h1></div>
+
+<%= form_tag sessions_path do %>
+ <div class="clearfix">
+ <%= label_tag :username, "Username" %>
+ <div class="input">
+ <%= text_field_tag :username, params[:username], :class => "span7" %>
+ </div>
+ </div>
+
+ <div class="clearfix">
+ <%= label_tag :password, "Password" %>
+ <div class="input">
+ <%= text_field_tag :password %>
+ </div>
+ </div>
+
+ <div class="actions">
+ <input type="submit" class="btn primary" value="Login">
+ <%= link_to 'Cancel', root_url, :class => "btn danger" %>
+ </div>
+<% end -%>
View
@@ -2,6 +2,9 @@
Subbysub::Application.routes.draw do
resources :links
+ resources :sessions
+ get 'login' => 'sessions#new', :as => 'login'
+ get 'logout' => 'sessions#destroy', :as => 'logout'
constraints(Subdomain) do
match '/' => 'links#show'
end
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class SessionsControllerTest < ActionController::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class SessionsHelperTest < ActionView::TestCase
+end

0 comments on commit 2c24550

Please sign in to comment.