Skip to content
This repository has been archived by the owner on Mar 9, 2020. It is now read-only.

Commit

Permalink
Implement login/logout flow.
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidS committed Jul 27, 2014
1 parent b61600f commit 0738413
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
class SessionsController < ApplicationController

def create
user = login(params[:email], params[:password], params[:remember_me])
if user
redirect_back_or_to root_url, :notice => "Logged in!"
else
flash.now.alert = "Email or password was invalid"
render :new
end
end

def destroy
logout
redirect_to root_url, :notice => "Logged out!"
end

end
8 changes: 8 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,13 @@

<%= yield %>
<% if current_user %>
Logged in as <%= current_user.email %>.
<%= link_to "Log out", logout_path %>
<% else %>
<%= link_to "Sign up", signup_path %> or
<%= link_to "log in", login_path %>.
<% end %>

</body>
</html>
15 changes: 15 additions & 0 deletions app/views/sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<%= form_tag sessions_path do %>
<div class="field">
<%= label_tag :email %>
<%= text_field_tag :email, params[:email] %>
</div>
<div class="field">
<%= label_tag :password %>
<%= password_field_tag :password %>
</div>
<div class="field">
<%= check_box_tag :remember_me, 1, params[:remember_me] %>
<%= label_tag :remember_me %>
</div>
<div class="actions"><%= submit_tag "Log in" %></div>
<% end %>
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
# You can have the root of your site routed with "root"
root 'welcome#index'

# authentication routes for sorcery
get "logout" => "sessions#destroy", :as => "logout"
get "login" => "sessions#new", :as => "login"
get "signup" => "people#new", :as => "signup"

# Example of regular route:
# get 'products/:id' => 'catalog#view'

Expand All @@ -17,6 +22,7 @@
# resources :products

resources :people
resources :sessions

# Example resource route with options:
# resources :products do
Expand Down

0 comments on commit 0738413

Please sign in to comment.